4-5编程练习???对不对
package com.imooc.file; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class FileDemo2 { public void transWriteByBuf(){ try { FileOutputStream fos=new FileOutputStream("hello.txt"); OutputStreamWriter osw=new OutputStreamWriter(fos); BufferedWriter bfw=new BufferedWriter(osw); // bfw.write("你好吗?"); // bfw.write("我很好!"); bfw.write("你好吗?\n我很好!"); fos.close(); osw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } } public void tranReadByBuf(){ try { FileInputStream fis=new FileInputStream("hello.txt"); InputStreamReader isr=new InputStreamReader(fis); BufferedReader bfd=new BufferedReader(isr); // System.out.println(bfd.read()); // System.out.println(bfd.read()); int n; while((n=bfd.read())!=-1){ System.out.println((char)n); } fis.close(); isr.close(); } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } } } package com.imooc.file; public class FileDeomTest { public static void main(String[] args) { FileDemo2 f2=new FileDemo2(); f2.transWriteByBuf(); f2.tranReadByBuf(); } }
正在回答 回答被采纳积分+1
package com.imooc.file;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class FileDemo2 {
public void transWriteByBuf(){
try {
FileOutputStream fos=new FileOutputStream("hello.txt");
OutputStreamWriter osw=new OutputStreamWriter(fos);
BufferedWriter bfw=new BufferedWriter(osw);
// bfw.write("你好吗?");
// bfw.write("我很好!");
bfw.write("你好吗?\n我很好!");
bfw.close();
fos.close();
osw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
public void tranReadByBuf(){
try {
FileInputStream fis=new FileInputStream("hello.txt");
InputStreamReader isr=new InputStreamReader(fis);
BufferedReader bfd=new BufferedReader(isr);
// System.out.println(bfd.read());
// System.out.println(bfd.read());
String str;
while((str=bfd.readLine())!=null){
System.out.println(str);
}
bfd.close();
fis.close();
isr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}
package com.imooc.file;
public class FileDeomTest {
public static void main(String[] args) {
FileDemo2 f2=new FileDemo2();
f2.transWriteByBuf();
f2.tranReadByBuf();
}
}
package com.imooc.file; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class FileDemo2 { public void transWriteByBuf(){ try { FileOutputStream fos=new FileOutputStream("hello.txt"); OutputStreamWriter osw=new OutputStreamWriter(fos); BufferedWriter bfw=new BufferedWriter(osw); // bfw.write("你好吗?"); // bfw.write("我很好!"); bfw.write("你好吗?\n我很好!"); fos.close(); osw.close(); bfw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } } public void tranReadByBuf(){ try { FileInputStream fis=new FileInputStream("hello.txt"); InputStreamReader isr=new InputStreamReader(fis); BufferedReader bfd=new BufferedReader(isr); // System.out.println(bfd.read()); // System.out.println(bfd.read()); String str; while((str=bfd.readLine())!=null){ System.out.println(str); } fis.close(); isr.close(); } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } } } package com.imooc.file; public class FileDeomTest { public static void main(String[] args) { FileDemo2 f2=new FileDemo2(); f2.transWriteByBuf(); f2.tranReadByBuf(); } }
//修改后的代码
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星