本文主要是介绍Java IO FileReader和FileWriter,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
FileReader 是用于读取字符流的类,它继承于InputStreamReader,
FileWriter 是用于写入字符流的类,它继承于OutputStreamWriter
package com.louisgeek.appupdatetool;import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;/*** Created by louisgeek on 2016/8/23.*/
public class FileXXXTest {public static void main(String[] args) throws Exception {// testReader();testWriter();}private static void testReader() throws Exception{File file=new File("d:"+File.separator+"text.txt");FileReader reader = new FileReader(file);char[] chars = new char[1024];int len;// 读取文件内容while ( (len = reader.read(chars)) != -1 ) {System.out.println( new String(chars, 0, len) );}reader.close();}private static void testWriter() throws Exception{File file=new File("d:"+File.separator+"text.txt");FileWriter writer = new FileWriter(file);writer.write("XADASDA");writer.write("中国");writer.close();}
}
这篇关于Java IO FileReader和FileWriter的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!