与字节流
什么时候用字节流:用来读取图片、视频、音频等等。(字节流可以处理任何文件)
什么时候用字符流:便于读取纯文本。(但字符流底层仍是字节流)
字符输入流
1 2 3 4 5 6 7 8 9 10 11
| public void readFile() throws Exception{ FileReader fr = new FileReader("X:\\xxx.xxx"); char[] car = new char[1024]; int len = 0; while((len=fr.read(car))!=-1){ System.out.println(new String(car,0,len)); } fr.close(); }
|
缓冲输入字符流
1 2 3 4 5 6 7 8 9 10 11 12
| public void readFile() throws Exception{ FileReader fr = new FileReader("X:\\xxx.xxx"); BufferedReader br = new BufferedReader(fr); String line = null; while((line=br.readLine())!=null){ System.out.println(line); } br.close(); fr.close(); }
|
字符输出流
1 2 3 4 5 6 7
| public void writeToFile() throws Exception{ FileWriter fw = new FileWriter("X:\\xxx.xxx"); fw.write("oookkk",true); fw.close(); }
|
缓冲输出字符流
1 2 3 4 5 6 7 8
| piblic void writeToFileFast() throws Exception{ FileWriter fw = new FileWriter("X:\\xxx.xxx"); BufferedWriter bw = new BufferedWriter(fw); bw.write("kkkooo"); bw.newLine(); bw.close(); fw.close(); }
|