Java IO 总结

1字节流方式读写

  文件读写  FileInputStream FileOutputStream
  字节读写  ByteArrayInputStream  ByteArrayOutputStream
  缓存读写  BufferedInputStream   BufferedOutputStream
  
  样例:
  从文件到字节 非缓存: FileInputStream ---> ByteArrayOutputStream 
  从文件到 字节 带缓存:BufferedInputStream(FileInputStream) ---> BufferedOutputStream(ByteArrayOutputStream)
 
  从字节到文件 非缓存:ByteArrayInputStream ----> FileOutputStream
  从字节到文件 带缓存 BufferedInputStream(ByteArrayInputStream) -----> BufferedOutputStream(FileOutputStream)
 
  
2 字符流读写
  文件读写 FileReader InputStreamReader(FileInputStream) FileWriter OutputStreamWriter(FileOutputStream)
  字符读写 StringReader   CharArrayReader 以及对应的writer(暂时用不到)
  缓存读写 BufferedReader BufferedWriter 
    
  样例
  从文件到字符 非缓存 FileReader ----> StringBuild
  
  从文件到字符 带缓存 BufferedReader(InputStreamReader(FileInputStream)) readline()  ----> StringBuild
  
  从字符到文件 不带缓存 StringReader(CharArrayReader) ----> FileWriter(OutputStreamWriter(FileOutputStream))
  
  从字符到文件 带缓存  StringReader ---->  BufferedWriter(OutputStreamWriter(FileOutputStream))
  
注意点:
  1 从数组读取数据时,一定要指定长度,不然数组后面的值会被一同写入,造成数据误差
 

    char[] datas = new char[100];
            int length = -1;
            while ((length = fr.read(datas)) != -1) {
                sb.append(datas, 0, length);              
            }
  
     int length = -1;
            while ((length = sr.read(data)) != -1) {
                bw.write(data, 0, length);
            }

代码样例:

公共工具类:

public class IOUtils {

    public static void close(Closeable io) {
        if (null != io) {
            try {
                io.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static String getPath() {
        String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
        return path;
    }
}

1 读取文件到byte数组,从byte数组写入文件

public class File2ByteNoBuffer {

    public static void main(String[] args) {

        String textValue = readFile2String();
        if (!textValue.equals("")) {
            writString2File(textValue);
        }

    }

    public static String readFile2String() {
        String path = IOUtils.getPath();
        FileInputStream fis = null;
        ByteArrayOutputStream bArrayos = null;
        try {
            fis = new FileInputStream(path + File.separator + "guide.txt");
            bArrayos = new ByteArrayOutputStream();
            byte[] b = new byte[10240];
            int dataLength = -1;
            while ((dataLength = fis.read(b)) != -1) {
                bArrayos.write(b, 0, dataLength);
            }
            bArrayos.flush();
            String textValue = bArrayos.toString();
            System.out.println(textValue);
            return textValue;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        } finally {
            IOUtils.close(fis);
            IOUtils.close(bArrayos);
        }
    }

    public static void writString2File(String textValue) {
        FileOutputStream fos = null;
        String path = IOUtils.getPath();
        try {
            fos = new FileOutputStream(path + File.separator + "guidew_no_buffer.txt");
            byte[] datas = textValue.getBytes();

            fos.write(datas);
            fos.flush();

            System.out.println("write finished .....");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.close(fos);
        }

    }
}

2 读取文件到byte数组,从byte数组写入文件 使用缓存

public class File2ByteBuffer {

    public static void main(String[] args) {
        String textValue = readFile2String();
        if (!textValue.equals("")) {
            writeString2File(textValue.getBytes());
        }

    }

    public static String readFile2String() {
        String path = IOUtils.getPath();
        FileInputStream fis = null;
        ByteArrayOutputStream bArrayos = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            fis = new FileInputStream(path + File.separator + "guide.txt");
            bis = new BufferedInputStream(fis);
            bArrayos = new ByteArrayOutputStream();
            bos = new BufferedOutputStream(bArrayos);
            byte[] b = new byte[10240];
            int dataLength = -1;
            while ((dataLength = bis.read(b)) != -1) {
                bos.write(b, 0, dataLength);
            }

            String textValue = bArrayos.toString();
            System.out.println(textValue);
            return textValue;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        } finally {
            IOUtils.close(fis);
            IOUtils.close(bArrayos);
            IOUtils.close(bis);
            IOUtils.close(bos);
        }
    }

    public static void writeString2File(byte[] datas) {
        String path = IOUtils.getPath();
        BufferedOutputStream bos = null;
        ByteArrayInputStream bArrayis = null;
        BufferedInputStream bis = null;

        try {
            bos = new BufferedOutputStream(new FileOutputStream(path + File.separator + "guidew_buffer.txt"));
            bArrayis = new ByteArrayInputStream(datas);
            bis = new BufferedInputStream(bArrayis);
            byte[] data = new byte[10240];
            int length = -1;
            while ((length = bis.read(data)) != -1) {
                bos.write(data, 0, length);
            }
            bos.flush();
            System.out.println("write file finish......");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3  文件与字符读写

public class File2StringNoBuffer {

    public static void main(String[] args) {
        String textValue = readFile2String();
        if (!textValue.equals("")) {
            write2File(textValue);

        }

        String textValue2 = readFile2String2();

        if (!textValue2.equals("")) {
            write2File2(textValue2);

        }
    }

    public static String readFile2String() {
        String path = IOUtils.getPath();
        File file = null;
        FileReader fr = null;
        StringBuilder sb = new StringBuilder();
        try {
            file = new File(path + File.separator + "guide.txt");
            fr = new FileReader(file);
            char[] datas = new char[100];
            int length = -1;
            while ((length = fr.read(datas)) != -1) {
                sb.append(datas, 0, length);
                datas = new char[100];  // 不清空,最后一次数组填充不满,就会保留上次数据
            }
            System.out.println(sb.toString());
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        } finally {
            IOUtils.close(fr);

        }

    }

    public static String readFile2String2() {
        String path = IOUtils.getPath();
        File file = null;
        InputStreamReader isr = null;
        StringBuilder sb = new StringBuilder();
        try {
            file = new File(path + File.separator + "guide.txt");
            isr = new InputStreamReader(new FileInputStream(file));
            char[] datas = new char[100];
            int length = -1;
            while ((length = isr.read(datas)) != -1) {
                sb.append(datas, 0, length);
                datas = new char[100];  // 不清空,最后一次数组填充不满,就会保留上次数据
            }
            System.out.println(sb.toString());
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        } finally {
            IOUtils.close(isr);

        }

    }

    public static void write2File(String textValue) {
        FileWriter fw = null;
        CharArrayReader car = null;

        String path = IOUtils.getPath();
        try {
            fw = new FileWriter(path + File.separator + "guidesw_no_buffer.txt");

            char[] datas = textValue.toCharArray();
            car = new CharArrayReader(datas);
            char[] data = new char[1024];
            int length = -1;
            while ((length = car.read(data)) != -1) {
                fw.write(data, 0, length);
            }
            fw.flush();
            System.out.println("write finish");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void write2File2(String textValue) {
        OutputStreamWriter osw = null;
        CharArrayReader car = null;

        String path = IOUtils.getPath();
        try {
            osw = new OutputStreamWriter(new FileOutputStream(path + File.separator + "guidesw_no_buffer2.txt"));

            char[] datas = textValue.toCharArray();
            car = new CharArrayReader(datas);
            char[] data = new char[1024];
            int length = -1;
            while ((length = car.read(data)) != -1) {
                osw.write(data, 0, length);
            }
            osw.flush();
            System.out.println("write finish");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.close(osw);
            IOUtils.close(car);
        }

    }
}

4 文件字符读写 使用缓存(一)

public class File2StringUseBuffer {
    public static void main(String[] args) {
        String textValue = readFil2String();
        if (!"".equals(textValue)) {
            writeString2File(textValue);
        }

    }

    public static String readFil2String() {

        String path = IOUtils.getPath();
        InputStreamReader isr = null;
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();
        try {
            isr = new InputStreamReader(new FileInputStream(path + File.separator + "guide.txt"));
            br = new BufferedReader(isr);
            String lineData = null;
            while (null != (lineData = br.readLine())) {
                sb.append(lineData).append(System.lineSeparator());
            }
            System.out.println(sb.toString());
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        } finally {
            IOUtils.close(isr);
            IOUtils.close(br);
        }
    }

    public static void writeString2File(String textValue) {
        String path = IOUtils.getPath();
        BufferedWriter bw = null;
        OutputStream os = null;
        StringReader sr = null;
        try {
            bw = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(path + File.separator + "guidesw_buffer.txt")));

            sr = new StringReader(textValue);
            char[] data = new char[100];
            int length = -1;
            while ((length = sr.read(data)) != -1) {
                bw.write(data, 0, length);
            }
            bw.flush();
            System.out.println("write file finish");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2 文件与字符读写 使用缓存(二)

public class File2StringUseBuffer2 {
    public static void main(String[] args) {
        String textValue = readFile2String();
        if (!"".equals(textValue)) {
            writeString2File(textValue);
        }
    }

    public static String readFile2String() {
        String path = IOUtils.getPath();
        FileReader fr = null;
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();
        try {
            fr = new FileReader(new File(path + File.separator + "guide.txt"));
            br = new BufferedReader(fr);
            String lineData = null;
            while (null != (lineData = br.readLine())) {
                sb.append(lineData).append(System.lineSeparator());
            }
            System.out.println(sb.toString());

            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        } finally {
            IOUtils.close(fr);
            IOUtils.close(br);
        }
    }

    public static void writeString2File(String textValue) {
        String path = IOUtils.getPath();
        BufferedWriter bw = null;
        StringReader sr = null;

        try {
            bw = new BufferedWriter(new FileWriter(path + File.separator + "guidesw_buffer2.txt"));
            sr = new StringReader(textValue);
            char[] data = new char[128];
            int length = -1;
            while ((length = sr.read(data)) != -1) {
                bw.write(data, 0, length);
            }
            bw.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值