public class Zuoye { // 文件缓冲流BufferedInputStream/BufferedOutputStream // 字符缓冲流BufferedWriter/BufferedReader // 4种方式实现 public static void main(String[] args) throws IOException { File file = new File("D:\\java\\Lxz JavaSE\\zuoye.txt"); // 使用字符输入输出流FileWriter/FileReader //创建字输出流对象 FileWriter fw = new FileWriter(file); fw.write("窗前明月光 "); fw.write("疑是地上霜 "); fw.write("就会这两句了。。。!"); fw.flush(); fw.close(); FileReader fr = new FileReader(file); char[] a = new char[Math.toIntExact(file.length())]; fr.read(a); // 读取内容到数组 for (int i = 0; i < a.length; i++) { } System.out.println(a); fr.close(); // 文件输入输出流FileInputStream/FileOutputStream FileInputStream fis = new FileInputStream(file); FileOutputStream out = new FileOutputStream("D:\\java\\Lxz JavaSE\\zuoye1.txt"); long start = System.currentTimeMillis(); byte[] bytes = new byte[1024]; while (true) { int r = fis.read(bytes); if (r == -1) { break; } else { out.write(bytes, 0, r); } } long end = System.currentTimeMillis(); System.out.println("复制完毕" + (end - start)); fis.close(); out.close(); } }
2022-03-28
于 2022-03-28 21:24:53 首次发布