JAVA编程 利用FileOutputStream类向myfile.txt文件写入'0'~'9'和字符串“文件和输入输出流”内容

然后利用FileInputStream类以“逐字节”方式输出其内容。求程序代码,能在eclipse平台运行通过

第1个回答  2011-11-13
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

public class IO {
public static void main(String[] args) throws IOException {
String str = "0,1,2,3,4,5,6,7,8,9 文件和输入输出流";//要写入文件内容
String file="D://myfile.txt";//文件地址(必须存在)
Input(file, str);
Output(file);
}

public static void Output(String file) throws IOException,
FileNotFoundException {

BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(file), "GBK"));

String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}

in.close();
}

public static void Input(String file, String str) throws IOException,
FileNotFoundException {

PrintWriter w = new PrintWriter(new OutputStreamWriter(
new FileOutputStream(file), "gbk"));

w.write(str);
System.out.println("input");
w.close();

}
}本回答被提问者采纳
相似回答