输入输出流——字符流部分
字符流
读数据:BufferedReader步骤:
(1)创建FileReader和BufferedReader对象
FileReader f =new FileReader(文件路径);
BufferedReader read=new BufferedReader(f);
(2)调用readLine()方法,每次拂去一行数据
String s = read.readLine();
(3)关流 (先开的后关)
Read.close();
F.close();
写数据:BufferedWriter
(1)创建FileWriter和 BufferedWriter对象
FileWriter fw = new FileWriter("路径",true);
BufferedWriter bufw = new BufferedWriter(fw);
(2)调用方法写入
Bw.write(String str);//写入
bw.newLine();//换行
(3)刷新缓冲区,关闭流
Bw.flush();//刷新缓冲区
Bw.close();//f关闭流
Fw.close();
练习:将学生信息保存到文件中:
学生信息:
张三 18 男 一班
李四 19 男 二班
操纵的方式不止一种,所以直接写了,可能跟笔记上面的略有不同,事实上,我们要做的显示实现功能,然后反复的写,优化这种事儿水到渠成,一上来就想着优化,或者效率,反而可能连功能都没实现。想问题,要复杂,也不能复杂。
WriterDemo1
package IOPart; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class WriterDemo1 { public static void main(String[] args) { BufferedWriter bufferedWriter = null; try { bufferedWriter = new BufferedWriter(new FileWriter("f:\\tryFile\\WriterOut.txt")); bufferedWriter.write("学生信息:"); bufferedWriter.newLine(); /** * 这里面貌似不能写对象,这样的话,可能要再抽取出来一个方法,传一个对象进去,然后 分别得到每一个的属性,然后 输出。 */ bufferedWriter.write("张三 18 男 一班"); bufferedWriter.newLine(); bufferedWriter.write("李四 19 男 二班"); bufferedWriter.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(bufferedWriter!=null){ try { bufferedWriter.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } System.out.println("字符流写出完成"); } }
readerDemo1
package IOPart; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class ReaderDemo1 { public static void main(String[] args) { BufferedReader bufferedReader = null; try { bufferedReader = new BufferedReader(new FileReader("F:\\tryFile\\WriterOut.txt")); String currentLine = null; while((currentLine=bufferedReader.readLine())!=null){ System.out.println(currentLine); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(bufferedReader!=null){ try { bufferedReader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
copyDemo1
package IOPart; import java.awt.image.BufferedImageFilter; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class CopyFile2 { public static void main(String[] args) { BufferedReader bufferedReader = null; BufferedWriter bufferedWriter = null; try { bufferedReader = new BufferedReader(new FileReader("f:\\tryFile\\WriterOut.txt")); bufferedWriter = new BufferedWriter(new FileWriter("f:\\tryFile\\CharacterCopy.txt")); String currentLine = null; while((currentLine = bufferedReader.readLine())!=null){ bufferedWriter.write(currentLine); bufferedWriter.newLine(); } bufferedWriter.flush(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(bufferedReader!=null){ try { bufferedReader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(bufferedWriter!=null){ try { bufferedWriter.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } System.out.println("复制完成"); } }