java:I/O 一行一行读取和写入
BufferedReader逐行读取
import java.io.*; class Test { public static void main(String args []){ FileReader reader =null; BufferedReader bufferedReader =null; try{ reader = new FileReader("e://d/from.txt"); bufferedReader = new BufferedReader(reader); String line =null; while(true){ line =bufferedReader.readLine(); if(line ==null){ break; } System.out.println(line); } }catch(Exception e){ System.out.println(e); } finally{ try{ bufferedReader.close(); reader.close(); }catch(Exception e){ System.out.println(e); } } } }
BufferedWriter逐行写入
import java.io.*; class Test { public static void main(String [] args){ FileWriter fw =null; BufferedWriter bw =null; String [] arrs = { "zhang,33", "li,44" }; try{ fw = new FileWriter("e://d/from.txt"); bw = new BufferedWriter(fw); for(String arr:arrs){ bw.write(arr+"\t\n"); } /*等效于 for(int i=0;i<arrs.length;i++){ bw.write(arrs[i]+"\t\n"); } */ }catch(Exception e){ System.out.println(e); } finally{ try{ bw.close(); fw.close(); }catch(Exception e){ System.out.println(e); } } } }
2024还活着,挺好的,向着星辰与深渊,加油,博客园不要倒下啊!