4.5练习
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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 ReadWriteTest {
public static void main(String[] args) {
transWriteBybuf();
transReadBybuf();
}
public static void transWriteBybuf() {
// return "你好吗?\n我很好!";
try {
FileInputStream fis=new FileInputStream("helle.txt");
BufferedInputStream bis=new BufferedInputStream(fis);
InputStreamReader isr=new InputStreamReader(bis);
// fis.close();
// bis.close();
// isr.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void transReadBybuf() {
try {
FileOutputStream fos=new FileOutputStream("helle.txt");
BufferedOutputStream bos=new BufferedOutputStream(fos);
OutputStreamWriter osw=new OutputStreamWriter(bos);
osw.write("你好吗?\n我很好!");
osw.flush();
fos.close();
bos.close();
osw.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
创建两个方法分别表示输入,读取 请问字节字符的输入流怎么关闭, readLine()怎么用?
正在回答 回答被采纳积分+1
public static void transReadBybuf() {
try {
FileInputStream fis=new FileInputStream("helle.txt");
InputStreamReader isr=new InputStreamReader(fis);
BufferedReader br=new BufferedReader(isr);
String str="你好吗?\n我很好!";
while(!(str=br.readLine()).equals("")) {
System.out.println(str);
}
br.close();
isr.close();
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void transWriteBybuf() {
try {
FileOutputStream fos=new FileOutputStream("helle.txt");
OutputStreamWriter osw=new OutputStreamWriter(fos);
BufferedWriter bw=new BufferedWriter(osw);
bw.write("你好吗?\n我很好!");
bw.flush();
bw.close();
osw.close();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这是改动后的代码
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 ReadWriteTest {
public static void main(String[] args) {
transWriteBybuf();
transReadBybuf();
}
public static void transReadBybuf() {
try {
FileInputStream fis=new FileInputStream("helle.txt");
InputStreamReader isr=new InputStreamReader(fis);
BufferedReader br=new BufferedReader(isr);
br.readLine();
fis.close();
isr.close();
br.close();
System.out.println();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void transWriteBybuf() {
try {
FileOutputStream fos=new FileOutputStream("helle.txt");
OutputStreamWriter osw=new OutputStreamWriter(fos);
BufferedWriter bw=new BufferedWriter(osw);
bw.write("你好吗?\n我很好!");
bw.flush();
fos.close();
osw.close();
bw.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我修改了下代码, 但还是不明白这题目该怎么处理。··
- 参与学习 人
- 提交作业 1789 份
- 解答问题 2907 个
Android大楼Java起,本阶段是Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始入门Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星