Java IO总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lhc_makefunny/article/details/79964451

IO流

抽象基类 节点流 缓冲流 转换流
Inputstream FileInputStream bufferinputstream
outputstream fileoutputstream bufferoutputstream (flush())
reader filereader bufferreader inputstreamreader(将字节流转化为字符流)
writer filerwriter bufferwriter (flush()) outputstramwriter(将字节流转化为字符流)

节点流

字节流

  1. OutputStream(向文件中写数据)
    @Test
    public void write() {
        File file = new File("E:\\log\\log.log");
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(new String("我叫龙海成").getBytes());
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
  1. InputStream(活得指定文件中的内容)
    @Test
    public void getcontent() {
        File file = new File("E:\\log\\hhk.txt");
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            byte[] s = new byte[1024];
            int b;
            while ((b = fileInputStream.read(s)) != -1) {
                String str = new String(s, 0, b);
                System.out.println(str);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
  1. 文件的复制和黏贴实现
@Test
    public void copy() {
        FileInputStream input = null;
        FileOutputStream outputStream = null;
        try {
            input = new FileInputStream("E:\\log\\1.txt");
            outputStream = new FileOutputStream("E:\\log\\2.txt",true);
            byte[] b = new byte[1024];
            int num;
            while ((num = input.read(b)) != -1) {
                outputStream.write(new String(b, 0, num).getBytes());
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
                input.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

字符流

1.filereader:读入文件

    @Test
    public void getread() {
        File file = new File("E:\\log\\2.txt");
        FileReader fileReader = null;
        try {
            fileReader = new FileReader(file);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        char[] c = new char[1024];
        int a;
        try {
            while ((a = fileReader.read(c)) != -1) {
                String s = new String(c, 0, a);
                System.out.println(s);

            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                fileReader.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

2.文件的黏贴复制

@Test
    public void copy() {
        File file1 = new File("E:\\log\\log.log");
        File file2 = new File("E:\\log\\1.txt");
        FileReader reader = null;
        FileWriter writer = null;
        try {
            reader = new FileReader(file2);
            writer = new FileWriter(file1,true);
            char[] ch = new char[1024];
            int a;
            while((a = reader.read(ch)) != -1) {
                writer.write(new String (ch,0,a));
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                writer.close();
                reader.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

缓冲流(速度比节点流更快BUff)

  1. 读取文件
    @Test
    public void readfile() {
        BufferedInputStream bufferedInputStream = null;
        try {
            bufferedInputStream = new BufferedInputStream(new FileInputStream(new File("E:\\log\\1.txt")));
            byte[] b = new byte[1024];
            int a;
            while ((a = bufferedInputStream.read(b)) != -1) {
                String af = new String(b, 0, a);
                System.out.println(af);

            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                bufferedInputStream.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

2.文件的复制与黏贴

@Test
    public void readfile() {
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try {
            bufferedInputStream = new BufferedInputStream(new FileInputStream(new File("E:\\log\\1.txt")));
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File("E:\\log\\hhk.txt")));
            byte[] b = new byte[1024];
            int a;
            while ((a = bufferedInputStream.read(b)) != -1) {
                bufferedOutputStream.write(new String(b, 0, a).getBytes());
                bufferedOutputStream.flush();
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                bufferedOutputStream.close();
                bufferedInputStream.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

3.bufferreader读取文件中的数据

    @Test
    public void bufferreader() {
        File file = new File("E:\\log\\1.txt");
        FileReader reader = null;
        try {
            reader = new FileReader(file);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        BufferedReader readers = new BufferedReader(reader);
        String str;
        try {
            while ((str = readers.readLine()) != null) {
                System.out.println(str);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

转换流(inputstreamreader和outputstreamwrite)

    @Test
    public void getreader() {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        InputStreamReader isr = null;
        OutputStreamWriter osw = null;
        BufferedReader bufferreader = null;
        BufferedWriter bw = null;
        try {
            fis = new FileInputStream(new File("E:\\log\\1.txt"));
            isr = new InputStreamReader(fis, "utf8");
            bufferreader = new BufferedReader(isr);
            fos = new FileOutputStream(new File("E:\\log\\2.txt"));
            osw = new OutputStreamWriter(fos, "utf8");
            bw = new BufferedWriter(osw);
            String str;
            while ((str = bufferreader.readLine()) != null) {
                bw.write(str);
                bw.newLine();
                bw.flush();
            }
            System.out.println("结束");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                bufferreader.close();
                isr.close();
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

标准输入输出流


  • System.in
  • System.out

从键盘读取输入,并在控制台输出
    @Test
    public void getstream() {
        BufferedReader bufferedReader = null;
        bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String str = null;
        while (true) {
            System.out.println("请输入字符串:");
            try {
                str = bufferedReader.readLine();
                if (str.equalsIgnoreCase("e") || str.equalsIgnoreCase("exit")) {
                    System.out.println("程序结束");
                    break;
                }
                String s = str.toUpperCase();
                System.out.println(s);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }

随机文件的读取

RandomAccessFile类

    @Test
    public void accessfile() {
        RandomAccessFile r1 = null;
        RandomAccessFile r2 = null;
        try {
            r1 = new RandomAccessFile(new File("E:\\log\\log.log"), "r");
            r2 = new RandomAccessFile(new File("E:\\log\\2.txt"), "rw");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        byte[] b = new byte[1024];
        int num;
        try {
            while ((num = r1.read(b)) != -1) {
                r2.write(b, 0, num);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (r2 != null) {
                try {
                    r2.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (r1 != null) {
                try {
                    r1.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

在某个点之后插入字符

    @Test
    public void insertchar() {
        RandomAccessFile radomaccessfile = null;
        try {
            radomaccessfile = new RandomAccessFile(new File("E:\\log\\2.txt"), "rw");
            try {
                radomaccessfile.seek(3);
                radomaccessfile.write("软件".getBytes());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (radomaccessfile != null) {
                try {
                    radomaccessfile.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页