Java教程

Java I/O

本文主要是介绍Java I/O,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

  

 

package ersatz;

import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class Ersatz {
  public static void main(String[] args) {

  }

  @Test
  public void m1() {
    int data = 0;
    FileInputStream fileInputStream = null;
    try {
      fileInputStream = new FileInputStream("d:/n2");
      while ((data = fileInputStream.read()) != -1) {
        System.out.println((char) data + " " + data);
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        assert fileInputStream != null;
        fileInputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

  @Test
  public void m2() {
    byte[] buffer = new byte[2];
    int len = 0;
    FileInputStream fileInputStream = null;
    try {
      fileInputStream = new FileInputStream("d:/n2");
      while ((len = fileInputStream.read(buffer)) != -1) {
        System.out.println(new String(buffer, 0, len));
        for (byte b : buffer) {
          System.out.println(b);
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        assert fileInputStream != null;
        fileInputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

  @Test
  public void m3() throws IOException {
    FileOutputStream fileOutputStream = null;
    try {
      fileOutputStream = new FileOutputStream("d:/n1", true);
//      fileOutputStream.write(97788);
      String s = "uiopvbn发生付款啦";
      fileOutputStream.write(s.getBytes(StandardCharsets.UTF_8), 7, 3);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } finally {
      try {
        assert fileOutputStream != null;
        fileOutputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

  @Test
  public void m4() {
    String src = "d:/a.png";
    String dst = "d:/b.png";
    FileInputStream fileInputStream = null;
    FileOutputStream fileOutputStream = null;
    try {
      fileInputStream = new FileInputStream(src);
      fileOutputStream = new FileOutputStream(dst);
      byte[] buffer = new byte[1024];
      int length = 0;
      while ((length = fileInputStream.read(buffer)) != -1) {
        fileOutputStream.write(buffer, 0, length);
      }
      System.out.println("ok");
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if (fileInputStream != null) fileOutputStream.close();
        if (fileOutputStream != null) fileOutputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

  

FileReader

package ersatz;

import org.junit.jupiter.api.Test;

import java.io.FileReader;
import java.io.IOException;

public class Ersatz {
  public static void main(String[] args) {

  }

  @Test
  public void fileReader1() {
    FileReader fileReader=null;
    int data;
    try {
      fileReader = new FileReader("d:/courgette.log");
      while ((data = fileReader.read()) != -1) {
        System.out.print((char) data);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }finally{
      try {
        if (fileReader != null) {
          fileReader.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

  @Test
  public void fileReader2() {
    FileReader fileReader=null;
    int length=0;
    char[] buffer = new char[8];
    try {
      fileReader = new FileReader("d:/courgette.log");
      while ((length = fileReader.read(buffer)) != -1) {
        System.out.print(new String(buffer, 0, length));
      }
    } catch (IOException e) {
      e.printStackTrace();
    }finally{
      try {
        if (fileReader != null) fileReader.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

  

FileWriter

package ersatz;

import java.io.FileWriter;
import java.io.IOException;

public class Ersatz {
  public static void main(String[] args) {
    FileWriter fileWriter=null;
    char[] chars={'a', '发', '法', 'g'};

    try {
      fileWriter = new FileWriter("d:/n3",false);
      fileWriter.write(98);
      fileWriter.write(chars);
      fileWriter.write("范德萨浪费".toCharArray(),0,3);
      fileWriter.write("微软微软离开",0,2);
    } catch (IOException e) {
      e.printStackTrace();
    }finally{
      try {
        assert fileWriter != null;
        fileWriter.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

  

 

BufferedWriter

public class BufferedWriter_ {
    public static void main(String[] args) throws IOException {
        String filePath = "e:\\ok.txt";
        //创建BufferedWriter
        //说明:
        //1. new FileWriter(filePath, true) 表示以追加的方式写入
        //2. new FileWriter(filePath) , 表示以覆盖的方式写入
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath));
        bufferedWriter.write("hello, 飞洒发生!");
        bufferedWriter.newLine();//插入一个和系统相关的换行
        bufferedWriter.write("hello2, 飞洒发生!");
        bufferedWriter.newLine();
        bufferedWriter.write("hello3, 飞洒发生!");
        bufferedWriter.newLine();

        //说明:关闭外层流即可 , 传入的 new FileWriter(filePath) ,会在底层关闭
        bufferedWriter.close();

    }
}

  

BufferedReader

public class BufferedReader_ {
    public static void main(String[] args) throws Exception {

        String filePath = "e:\\a.java";
        //创建bufferedReader
        BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
        //读取
        String line; //按行读取, 效率高
        //说明
        //1. bufferedReader.readLine() 是按行读取文件
        //2. 当返回null 时,表示文件读取完毕
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }

        //关闭流, 这里注意,只需要关闭 BufferedReader ,因为底层会自动的去关闭 节点流
        //FileReader。
        /*
            public void close() throws IOException {
                synchronized (lock) {
                    if (in == null)
                        return;
                    try {
                        in.close();//in 就是我们传入的 new FileReader(filePath), 关闭了.
                    } finally {
                        in = null;
                        cb = null;
                    }
                }
            }

         */
        bufferedReader.close();

    }
}

  

Buffered copy\

package ersatz;

import java.io.*;

public class Ersatz {
  public static void main(String[] args) throws Exception {
    String src = "d:/a.png";
    String dst = "d:/c.png";
    BufferedInputStream bufferedInputStream=null;
    BufferedOutputStream bufferedOutputStream=null;
    int data;
    try {
      bufferedInputStream = new BufferedInputStream(new FileInputStream(src), 1024);
      bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(dst), 1024);
      while ((data = bufferedInputStream.read()) != -1) {
        bufferedOutputStream.write(data);
      }
      System.out.println("done");
    } catch (IOException e) {
      e.printStackTrace();
    }finally{
      try {
        if (bufferedInputStream != null) bufferedInputStream.close();
        if (bufferedOutputStream != null) bufferedOutputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

  

public class BufferedCopy_ {

    public static void main(String[] args) {

        // 1. BufferedReader 和 BufferedWriter 是安装字符操作
        // 2. 不要去操作 二进制文件[声音,视频,doc, pdf ], 可能造成文件损坏
        // BufferedInputStream
        // BufferedOutputStream
        String srcFilePath = "e:\\a.java";
        String destFilePath = "e:\\a2.java";

        BufferedReader br = null;
        BufferedWriter bw = null;
        String line;
        try {
            br = new BufferedReader(new FileReader(srcFilePath));
            bw = new BufferedWriter(new FileWriter(destFilePath));

            // 说明: readLine 读取一行内容,但是没有换行
            while ((line = br.readLine()) != null) {
                // 每读取一行,就写入
                bw.write(line);
                // 插入一个换行
                bw.newLine();
            }
            System.out.println("拷贝完毕...");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭流
            try {
                if (br != null) {
                    br.close();
                }
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

  

这篇关于Java I/O的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!