Java IO总结

原创 2018年04月16日 18:32:14

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();
                }
            }
        }
    }
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lhc_makefunny/article/details/79964451

java IO流学习总结

Java中的流,可以从不同的角度进行分类。     按流的方向分:输入流和输出流。     按处理数据单位分:字节流和字符流。     按实现功能分:节点流和处理流。       输出流:     ...
  • du_minchao
  • du_minchao
  • 2015-10-11 12:35:20
  • 20108

Java中IO流学习总结

Java中IO流学习总结一.IO流中的结构 字符流:顾名思义,该流只能处理字符,但处理字符速度很快 字节流:可以处理所有以bit为单位储存的文件,也就是说可以处理所有的文件,但是在处理字符上的速度不如...
  • Yue_Chen
  • Yue_Chen
  • 2017-05-26 18:00:19
  • 1719

Java中的IO流总结

Java中的IO流总结 1. 流的继承关系,以及字节流和字符流。 2. 节点流FileOutputStream和FileInputStream和处理流BufferedInputStream和Bu...
  • u013309870
  • u013309870
  • 2016-08-03 20:03:22
  • 1770

java IO体系的学习总结

java IO体系的整理笔记1.Java Io流的概念,分类,类图。1.1 Java Io流的概念    java的io是实现输入和输出的基础,可以方便的实现数据的输入和输出操作。在java中把不同...
  • nightcurtis
  • nightcurtis
  • 2016-05-05 16:31:28
  • 5592

Java /io总结 很不错

  • 2010年02月28日 00:33
  • 36KB
  • 下载

Java IO完全总结(转载)

Java IO江苏 无锡 缪小东       本篇主要讲述IO相关的内容,主要包括:与IO相关的简单的历史背景知识;Java IO的简单分类;与IO设计相关的两个模式;同时列举几个简单的例子;分析其中...
  • baobeiSimple
  • baobeiSimple
  • 2007-07-28 17:34:00
  • 23847

java中IO流的知识点总结

1.流的概念流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。2.IO流...
  • u011626964
  • u011626964
  • 2015-09-01 10:39:38
  • 8460

最完整的Java IO流学习总结

一、 File类 1、 java.io.File 类是专门对文件进行操作的类。(只能对文件本身进行操作,不能对文件内容操作) 2、 File类是“文件和目录路径名的抽象表示”。 而不是指文件的内容...
  • SilenceOO
  • SilenceOO
  • 2016-03-28 00:37:40
  • 990

JAVA IO基础概念相关总结

java.io包是负责整个IO编程的开发包,在这个开发包中包含有核心的5个类和1个接口 IO学习的关键:同一个方法根据不同的子类可能有不同的实现 一、File类 在整个java.io包...
  • shuxing520
  • shuxing520
  • 2018-02-12 15:42:43
  • 97

JAVA IO完全总结

记录一下,方便以后查看 http://m.blog.csdn.net/blog/baobeiSimple/1713797
  • LWP791212664
  • LWP791212664
  • 2014-03-21 10:49:13
  • 342
收藏助手
不良信息举报
您举报文章:Java IO总结
举报原因:
原因补充:

(最多只允许输入30个字)