package com.qiku.yrc.work21; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Arrays; public class FileWriterAndReader { // 分别使用字符输入输出流FileWriter/FileReader // 文件输入输出流FileInputStream/FileOutputStream // 文件缓冲流BufferedInputStream/BufferedOutputStream // 字符缓冲流BufferedWriter/BufferedReader // 种方式实现 public static void main(String[] args) throws Exception { String path="F:\\idea\\IntelliJ IDEA 2021.2.1\\Qikujavase\\src"; FileWriter fileWriter=new FileWriter("b.TXT",true); System.out.println(fileWriter); File file1 = new File("F:\\idea\\IntelliJ IDEA 2021.2.1\\Qikujavase\\src","a.TXT"); System.out.println(file1); //创建文件 // File file2= new File(file1,"src\\a.doc"); // while (!file2.exists()){ // file2.createNewFile(); // } // System.out.println(file2.exists()); FileReader reader = new FileReader("src\\b.TXT"); int read = reader.read(); System.out.println(read); read = reader.read(); System.out.println(read); read = reader.read(); System.out.println(read);read = reader.read(); System.out.println(read); read = reader.read(); System.out.println(read); read = reader.read(); System.out.println(read);// 依次读取一个 字符 char[] cs = new char[6]; System.out.println(reader.read(cs)); System.out.println(Arrays.toString(cs)); System.out.println(reader.read(cs)); System.out.println(Arrays.toString(cs)); System.out.println(reader.read(cs)); System.out.println(Arrays.toString(cs)); //reader读取了两个字符a,b ,然后将其放在了数组cs下标为1,2,3的位置 reader.read(cs,1,3); System.out.println(Arrays.toString(cs)); reader.close();//关闭流 释放资源 } }
package com.qiku.yrc.work21; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Arrays; public class FileInputAndOutputStream { public static void main(String[] args) throws Exception { FileInputStream fis= new FileInputStream("b.TXT"); for (int i = 0; i < 6; i++) {//一次只能读一个 System.out.print(fis.read()+" "); } System.out.println(); System.out.println("======="); byte[] bytes = new byte[3]; System.out.println(fis.read(bytes,1,2)); System.out.println(Arrays.toString(bytes)); System.out.println(fis.available()); fis.close(); FileOutputStream out = new FileOutputStream("d.txt",true); out.write(97); String str="我欲乘风归去"; byte[] bytes2 = str.getBytes(); out.write(bytes2); System.out.println(bytes.length); out.write(bytes2,0,3);//两个bytes out.flush(); out.close(); //flush()这个函数是清空的意思close()方法关闭读写流 } }
package com.qiku.yrc.work21; import java.io.*; public class BufferWriterAndReader { public static void main(String[] args) throws Exception { FileReader fr= new FileReader("dd.txt"); BufferedReader br = new BufferedReader(fr); FileWriter fw=new FileWriter("dddd.txt"); BufferedWriter bw=new BufferedWriter(fw); bw.write("偶像练习生 蔡徐坤申请出战"); bw.newLine();//换行符 String s=br.readLine(); System.out.println(s); bw.close(); br.close(); } }
package com.qiku.yrc.work21; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; public class BufferedInputAndOutputStream { public static void main(String[] args) throws Exception { FileInputStream fis= new FileInputStream("F:\\idea\\IntelliJ IDEA 2021.2.1\\Qikujavase\\src\\1234.mp4"); BufferedInputStream bis= new BufferedInputStream(fis);//输入缓冲流 FileOutputStream fos= new FileOutputStream("F:\\idea\\IntelliJ IDEA 2021.2.1\\Qikujavase\\src\\1235.mp4"); BufferedOutputStream bos= new BufferedOutputStream(fos);//输出缓冲流 while(true){ int r= bis.read();//读 if (r==-1){ break; }else { bos.write(r);//写 } } System.out.println("复制成功!"); bis.close(); bos.close(); } }