JAVA编程 利用FileOutputStream类向myfile.txt文件写入'0'~'9'和字符串“文件和输入输出流”内容
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(); } }
作者:fenglie
专注于JAVAEE开发,热爱开源项目
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。