//Character stream, Reader and Writer //Character output stream Writer //write a string to the file public static void main(String[] args) throws Exception { String path="d:"+File.separator+"T2.txt";//Find the file path File file = new File(path); // Writer fileWriter = new FileWriter(file); Writer fileWriter = new FileWriter(file,true);//true, append a string after it, do not overwrite the previous string // Directly output the string without first converting it to a byte array fileWriter.write("\r\nsuccess"); fileWriter.close(); }
// character input stream Reader public static void main(String[] args) throws Exception { String path = "d:" + File.separator + "T2.txt";//Find the file path File file = new File(path); Reader reader = new FileReader(file); char[] chars = new char[1000];//The read data is stored in this array int read_len = reader.read(chars);////The size of the file read reader.close(); System.out.println(new String(chars, 0, read_len)); }