JAVA —— 文件输入输出
import java.io.*; public class FileIO { public static void main(String[] args) { //1.相对路径 File testFile1=new File("test1.txt"); //2.绝对路径 //错误: //File testFile2=new File("E:\Java\test2.txt"); File testFile2=new File("E:/Java/test2.txt"); String s1; StringBuffer sb1=new StringBuffer(); try { BufferedReader reader = new BufferedReader(new FileReader(testFile1)); s1 = reader.readLine(); while(s1!=null){ sb1.append(s1+"\n"); s1=reader.readLine(); } System.out.println(sb1); reader.close(); } catch (IOException e) { e.printStackTrace(); } try { BufferedWriter writer = new BufferedWriter(new FileWriter(testFile2)); writer.write(sb1.toString()); //错误 //忘记清空缓冲,关闭流 writer.flush(); writer.close(); } catch (IOException e) { e.printStackTrace(); } } }