案例
读取文件
import java.io.*;public class Main {public static void main(String[] args) throws Exception {String path = "./story.txt";boolean append = true;BufferedWriter bw = new BufferedWriter(new FileWriter(path, append));bw.write("123\n");bw.write(97);bw.newLine();bw.write(new char[]{'G', 'G', 'C', '\n'});bw.write("123\n56789", 0, 9);bw.write(new char[]{'G', 'G', 'C', 'S', '\n'}, 0, 5);bw.close();}}
复制文件
一边读一边写
import java.io.*;public class Main {public static void main(String[] args) throws Exception {String srcPath = "./story.txt";String copypath = "./story_copy.txt";boolean append = false;BufferedReader br = new BufferedReader(new FileReader(srcPath));BufferedWriter bw = new BufferedWriter(new FileWriter(copypath, append));String line;while ((line = br.readLine()) != null) {bw.write(line);bw.newLine();}br.close();bw.close();}}
