- importjava.io.*;
- publicclassTest11{
- privatestaticBufferedReaderbr=null;
- privatestaticFilefile=null;
- privatestaticBufferedWriterbw=null;
- publicstaticvoidmain(String[]args){
- Stringb=null;
- file=newFile("D:\\abc.txt");
- if(!file.exists()!=false){
- try{
- file.createNewFile();
- }catch(IOExceptione){
- e.printStackTrace();
- }
- }
- try{
- bw=newBufferedWriter(newFileWriter(file));
- FileReaderfr=newFileReader("D:\\Log.txt");
- br=newBufferedReader(fr);
- while((b=br.readLine())!=null){
- System.out.println(b);
- bw.write(b);//输出字符串
- bw.newLine();//换行
- bw.flush();
- }
- }catch(Exceptione){
- e.printStackTrace();
- }finally{
- try{
- br.close();
- //bw.close();
- }catch(IOExceptione){
- e.printStackTrace();
- }
- }
- }
- }
使用BufferedReader和BufferedWriter按行读写文件
最新推荐文章于 2021-03-19 14:13:51 发布