java 常用io流操作

Posted 背时的哥哥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 常用io流操作相关的知识,希望对你有一定的参考价值。

前言

所谓温故而知新,最近学习一些新技能时需要用到我许久都未触碰的io流操作,借此机会,回顾一些基本的io流操作。

实例

1.fileInputStream&fileOutputStream

public static void main(String[] args) throws Exception{
        File file = new File("D:/test.txt");
        write(file);
        String res = read(file);
        System.out.println(res);
    }

    public static void write(File file) throws FileNotFoundException {
        OutputStream os = new FileOutputStream(file);
        String text = "1234";
        try {
            os.write(text.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public static String read(File file) throws IOException {
        byte bytes[] = new byte[1024];
        InputStream in = new FileInputStream(file);
        StringBuilder sb = new StringBuilder();
        // 读取到的字节数组长度,为-1时表示没有数据
        int length = 0;
        // 循环取数据
        while ((length = in.read(bytes)) != -1) {
            // 将读取的内容转换成字符串
            sb.append(new String(bytes, 0, length));
        }
        // 关闭流
        in.close();

        return sb.toString();
    }

2.BufferedInputStream&BufferedOutputStream

public static void main(String[] args) throws Exception{
        File file = new File("D:/test.txt");
        write(file);
        String read = read(file);
        System.out.println(read);
    }


//    缓冲字节流是为高效率而设计的,真正的读写操作还是靠FileOutputStream和FileInputStream
    public static void write(File file) throws Exception{
        OutputStream outputStream = new FileOutputStream(file);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
        String text = "l for bb";
        bufferedOutputStream.write(text.getBytes());
        bufferedOutputStream.flush();
        outputStream.close();
        bufferedOutputStream.close();
    }

    public static String read(File file) throws Exception{
        InputStream inputStream = new FileInputStream(file);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        byte[] buf = new byte[1024];
        int length = 0;
        StringBuffer stringBuffer = new StringBuffer();
        while ((length=bufferedInputStream.read(buf))!=-1){
            stringBuffer.append(new String(buf,0,length));
        }
        inputStream.close();
        bufferedInputStream.close();
        return stringBuffer.toString();
    }

3.FileWriter&FileReader

public static void main(String[] args) throws Exception{
        File file = new File("D:/test,txt");
        write(file);
        String read = read(file);
        System.out.println(read);

    }


    public static void write(File file) throws Exception{
        FileWriter fileWriter = new FileWriter(file);
        String text = "l for bsj";
        fileWriter.write(text);
        fileWriter.close();
    }

    public static String read(File file) throws Exception{
        FileReader fileReader = new FileReader(file);
        char[] buf = new char[1024];
        int length = 0;
        StringBuilder stringBuilder = new StringBuilder();
        while ((length=fileReader.read(buf))!=-1){
            stringBuilder.append(new String(buf,0,length));
        }
        fileReader.close();
        return stringBuilder.toString();
    }

4.BufferedWriter&BufferedReader

public static void main(String[] args) throws Exception{
        File file = new File("D:/test.txt");
        write(file);
        String read = read(file);
        System.out.println(read);
    }


    public static void write(File file) throws Exception{
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
        String text = "cause you";
        bufferedWriter.write(text);
        bufferedWriter.close();
    }

    public static String read(File file) throws Exception{
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        char[] buf = new char[1024];
        int length = 0;
        StringBuilder stringBuilder = new StringBuilder();
        while (((length=bufferedReader.read(buf)))!=-1){
            stringBuilder.append(new String(buf,0,length));
        }
        bufferedReader.close();
        return stringBuilder.toString();
    }

5.OutputStreamWriter&InputStreamReader

public static void main(String[] args) throws Exception{
        File file = new File("D:/test.txt");
        write(file);
        String read = read(file);
        System.out.println(read);
    }

    public static void write(File file) throws Exception{
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
        String text = "l for bs";
        outputStreamWriter.write(text);
        outputStreamWriter.close();
    }

    public static String read(File file) throws Exception{
        InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file),"UTF-8");
        char[] buf = new char[1024];
        int length = 0;
        StringBuilder stringBuilder = new StringBuilder();
        while ((length=inputStreamReader.read(buf))!=-1){
            stringBuilder.append(new String(buf,0,length));
        }

        inputStreamReader.close();
        return stringBuilder.toString();
    }

以上是关于java 常用io流操作的主要内容,如果未能解决你的问题,请参考以下文章

Java 常用IO流操作详解

Java一文认识IO操作流

Java一文认识IO操作流

java 常用io流操作

Java中常用IO流之文件流的基本使用姿势

Java中Io流操作-File类的常用操作-创建文件,创建文件夹