文本内容到集合,反之
file的遍历(例题):
package cn.itcast.demo01; import java.io.File; /* * 练习: 给定一个文件夹, 把文件夹里面的所有子文件都打印出来,包含子文件夹里面的文件 * * 分析: * 1. 创建File对象,绑定给定的文件路径 * 2. 调用方法listFiles,获取所有的内容 * 3. 遍历文件数组,判断是文件还是文件夹 * 4. 如果是文件直接打印出来,如果是文件夹继续遍历 */ public class Demo10FileLoop { public static void main(String[] args) { // * 1. 创建File对象,绑定给定的文件路径 File f = new File("C:\\Users\\sszz\\Desktop\\aaa"); methodFileLoop(f); } public static void methodFileLoop(File f) { // * 2. 调用方法listFiles,获取所有的内容 File[]listf = f.listFiles(); // * 3. 遍历文件数组,判断是文件还是文件夹 for(File file : listf){ // * 4. 如果是文件直接打印出来,如果是文件夹继续遍历 if(file.isDirectory()){ //调用方法,继续遍历 methodFileLoop(file); }else{ System.out.println(file); } } } public static void method2(File f){ File[] listf = f.listFiles(); for (File file : listf) { if(file.isDirectory()){ // method3(file); }else{ System.out.println(file); } } } }
*文本内容存放到集合中,在判断集合中的内容:
package demo01; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Properties; import java.util.Set; public class Test { public static void main(String[] args) throws IOException { ArrayList<Book> list =new ArrayList<>(); Properties p = new Properties(); FileReader fis=new FileReader("book.txt"); p.load(fis); Set<String> set = p.stringPropertyNames(); for (String key : set) { String[] split = key.split(","); Book book=new Book(split[0],Double.parseDouble(split[1]),Integer.parseInt(split[2])); list.add(book); } fis.close(); int sumPrice=0; for (Book book : list) { System.out.println(book.getName()+","+"共计"+(book.getNums()*book.getPrice())+"元"); sumPrice+=book.getNums()*book.getPrice(); } System.out.println("\t\t"+"总金额"+sumPrice); } }
或者:
package demo04; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Properties; import java.util.Scanner; import java.util.Set; public class Test { public static void main(String[] args) throws IOException { Properties p= new Properties(); BufferedReader br = new BufferedReader(new FileReader("player.txt")); p.load(br); br.close(); Map<String,String> map = new HashMap<>(); Set<String> stringPropertyNames = p.stringPropertyNames(); for (String string : stringPropertyNames) { String[] s = string.toString().split(","); map.put(s[0], s[1]); } /*Set<Object> keySet = p.keySet(); Map<String,String> map = new HashMap<>(); for(Object o : keySet){ System.out.println(o); String[] s = o.toString().split(","); map.put(s[0], s[1]); }*/ Set<String> keySet2 = map.keySet(); Scanner sc = new Scanner(System.in); System.out.println("请输入球员名:"); String playerName = sc.nextLine(); if(keySet2.contains(playerName)){ System.out.println(playerName+"所属"+map.get(playerName)); }else{ System.out.println("不存在该球员"); } } }
向文本中存放数据
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("Student.txt")); byte[] bufTitle = "姓名\t语文成绩\t数学成绩\t总分".getBytes(); byte[] buf1 = ("\r\n"+stu1.getName()+"\t"+stu1.getChineseScore()+"\t"+stu1.getMathScore()).getBytes(); bos.write(bufTitle); bos.write(buf1); bos.close();
人生就像一场路途,尝遍酸甜苦辣,方知人生苦甜.