IO操作比较繁琐,容易忘记,再加上NIO的一些基本操作,一段时间内居然混淆,这里做一下记录,以后直接拿来用,忘了方便查阅。还有,这里直接贴代码,运行结果就不放上去了。:open_mouth:
public void test1() throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("请输入一个字符"); char c; c = (char) bufferedReader.read(); System.out.println("你输入的字符为"+c); } 复制代码
public void test2() throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("请输入一行字符"); String str = bufferedReader.readLine(); System.out.println("你输入的字符为" + str); } 复制代码
public void test04() throws IOException { byte[] bytes = {12,21,34,11,21}; FileOutputStream fileOutputStream = new FileOutputStream(new File("")); fileOutputStream.write(bytes); fileOutputStream.close(); }复制代码
public void test05() throws IOException { FileInputStream fileInputStream = new FileInputStream(new File("")); int c; // 读取写入的二进制文件,输出字节数组 while ((c = fileInputStream.read()) != -1) { System.out.print(c); } }复制代码
public void test1() throws IOException { FileReader fileReader = new FileReader(new File("")); BufferedReader bufferedReader = new BufferedReader(fileReader); String str; while ((str = bufferedReader.readLine()) != null) { System.out.println(str); } fileReader.close(); bufferedReader.close(); } public void test2() throws IOException { FileReader fileReader = new FileReader(new File("")); int c; // 读取字符 while ((c = fileReader.read()) != -1) { System.out.print((char) c); } }复制代码
// 开启追加模式 public static void test1() throws IOException { FileWriter fileWriter = new FileWriter(new File("D://1.txt"), true); // 开启追加模式 fileWriter.write("----1----"); fileWriter.write("----2----"); // 不会覆盖文件原本的内容 // fileWriter.write(null); 不能直接写入 null fileWriter.append("----3----"); // 直接追加的内容 fileWriter.append(null); fileWriter.flush(); fileWriter.close(); } // 关闭追加模式 public void test2() throws IOException { FileWriter fileWriter = new FileWriter(new File("").getAbsolutePath()+"/io/test.txt", false); // 关闭追加模式,变为覆盖模式 fileWriter.write("----1----"); fileWriter.write("----2----"); // 覆盖掉前面的内容 fileWriter.append("----3----"); // 追加的内容 fileWriter.flush(); fileWriter.close(); } 复制代码
public void test1() throws IOException { FileOutputStream fileOutputStream = new FileOutputStream(new File("")); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "utf-8"); // 使用 GBK 编码文件 outputStreamWriter.write("----1----"); outputStreamWriter.append("----2----"); // 追加内容 outputStreamWriter.flush(); outputStreamWriter.close(); fileOutputStream.close(); } public void test2() throws IOException { FileInputStream fileInputStream = new FileInputStream(new File("").getAbsolutePath()+"/io/test2.txt"); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "GBK"); // 使用 GBK 解码文件 BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String str; while ((str = bufferedReader.readLine()) != null) { System.out.println(str); } bufferedReader.close(); inputStreamReader.close(); }复制代码
输入和输出使用缓冲流 BufferedInputStream + BufferedInputStream
public void test1() throws IOException { FileInputStream in = new FileInputStream(""); BufferedInputStream inBuffer = new BufferedInputStream(in); FileOutputStream out = new FileOutputStream(""); BufferedOutputStream outBuffer = new BufferedOutputStream(out); int len = 0; byte[] bs = new byte[1024]; while ((len = inBuffer.read(bs)) != -1) { outBuffer.write(bs, 0, len); } in.close(); outBuffer.close(); out.close(); } 复制代码
输入和输出都不使用缓冲流 FileInputStream + FileOutputStream
public void test2() throws IOException { FileInputStream in = new FileInputStream(""); FileOutputStream out = new FileOutputStream(""); int len = 0; byte[] bs = new byte[1024]; while ((len = in.read(bs)) != -1) { out.write(bs, 0, len); } in.close(); out.close(); } 复制代码
按字符拷贝 FileReader + FileWriter
public void test3() throws IOException { File file=new File(""); if(!file.isFile()){ System.out.println("文件不存在,无法拷贝!"); return; } FileReader fr=new FileReader(file); FileWriter fw=new FileWriter(""); char chars[]=new char[512]; int temp=0; while ((temp=fr.read(chars))!=-1){ fw.write(chars,0,temp); } fw.flush(); fw.close(); fr.close(); System.out.println("拷贝成功!"); } } 复制代码
public void test1() throws IOException { FileOutputStream fileOutputStream = new FileOutputStream(""); FileChannel fileChannel = fileOutputStream.getChannel(); // 创建一个缓冲区 ByteBuffer byteBuffer = ByteBuffer.allocate(1024); byteBuffer.put("[内容]".getBytes()); // 对byteBuffer进行flip byteBuffer.flip(); // 将byteBuffer数据写入fileChannel fileChannel.write(byteBuffer); fileOutputStream.close(); }复制代码
public void test1() throws IOException { File file = new File(""); FileInputStream fileInputStream = new FileInputStream(file); // 获得inputStream对应的FileChannel FileChannel fileChannel = fileInputStream.getChannel(); // 创建缓冲区 ByteBuffer byteBuffer = ByteBuffer.allocate((int)file.length()); // 从channel读数据到byteBuffer fileChannel.read(byteBuffer); // 将byteBuffer的字节数据转换成String System.out.println(new String(byteBuffer.array())); fileInputStream.close(); }复制代码
FileChannel 实现
public void test1() throws IOException { // 输入流 FileInputStream fileInputStream = new FileInputStream(""); FileChannel fileChannel1 = fileInputStream.getChannel(); // 输出流 FileOutputStream fileOutputStream = new FileOutputStream(""); FileChannel fileChannel2 = fileOutputStream.getChannel(); ByteBuffer byteBuffer = ByteBuffer.allocate(1024); while (true) { byteBuffer.clear(); int read = fileChannel1.read(byteBuffer); if (read == -1) { break; } // flip一次byteBuffer byteBuffer.flip(); fileChannel2.write(byteBuffer); } fileInputStream.close(); fileOutputStream.close(); }复制代码
transferFrom实现
public void test1() throws IOException { // 输入流 FileInputStream fileInputStream = new FileInputStream(""); FileChannel fileChannel1 = fileInputStream.getChannel(); // 输出流 FileOutputStream fileOutputStream = new FileOutputStream(""); FileChannel fileChannel2 = fileOutputStream.getChannel(); // 用transerFrom完成拷贝 fileChannel2.transferFrom(fileChannel1, 0, fileChannel1.size()); fileInputStream.close(); fileOutputStream.close(); }复制代码