String s=reader.nextLine();
try {// 异常处理
FileWriter toFlie=new FileWriter(file);//通过底层流写入文件
BufferedWriter out=new BufferedWriter(toFlie);//通过缓冲流将字符串写入底层流
out.write(s);
out.close();
toFlie.close();
}
catch(IOException e){
System.out.println(e);
}
}
}
Read.java:
import java.io.*;
public class Read {
public static void main(String[] args) {
File file = new File("File.txt");
try {// 异常处理
FileReader fromFile = new FileReader(file);// 通过底层流读取文件
BufferedReader in = new BufferedReader(fromFile);// 通过缓冲流从底层流读取字符串
System.out.print("文件内容:\n");
String s = in.readLine();// 输出一行字符串
System.out.print(s);
in.close();
fromFile.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
五、测试数据及其结果分析
1正常输入: