Java专题十一:IO

Posted 黄先生的日志

tags:

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

Java专题十一(1):IO概述

IO是基于字节流和字符流操作的

IO基类:

Reader Writer InputStream OutputStream
字符流
字节流
输入流
输出流

各种资源IO类

Reader Writer InputStream OutputStream
File FileReader FileWriter FileInputStream FileOutputStream
Buffer BufferReader BufferWriter BufferedInputStream BufferedOutputStream
Pipe PipedReader PipedWriter PipedInputStream PipedOutputStream
String StringReader StringWriter
ByteArray ByteArrayReader ByteArrayWriter ByteArrayInputStream ByteArrayOutputStream
CharArray CharArrayReader CharArrayWriter
Object ObjectInputStream ObjectOutputStream
Filter FilterReader FilterWriter FilterInputStream FilterOutputStream
Data DataInputStream DataOutputStream

1.字节流转换成字符(InputStreamReader)

public static String byte2Char(InputStream is, String charsetName)
        throws IOException {
    if (charsetName == null || !Charset.isSupported(charsetName)){
        charsetName = DEFAULT_CHARSET;
    }
    InputStreamReader isr = new InputStreamReader(is, charsetName);
    char[] cbuf = new char[DEFAULT_BYTE_SIZE];
    StringBuilder sbf = new StringBuilder();
    int readCount;
    while((readCount = isr.read(cbuf)) > 0){
        sbf.append(cbuf, 0, readCount);
    }
    return sbf.toString();
}

2.从文件中读取字符(FileReader)

public static String readCharFromFile(String path) throws IOException {
    File fi = new File(path);
    if (fi.exists() && fi.isFile()){
        FileReader fr = new FileReader(fi);
        char[] cbuf = new char[DEFAULT_CHAR_SIZE];
        int readCount;
        StringBuilder sbf = new StringBuilder();
        while((readCount = fr.read(cbuf)) > 0){
            sbf.append(cbuf, 0, readCount);
        }
        return sbf.toString();
    }
    throw new FileNotFoundException();
}

3.字节流输出到文件(FileOutputStream)

public static File writeByteToFile(InputStream is, String path, boolean append) throws IOException {
    File fi = new File(path);
    // create file first if not exists
    if (!fi.exists()) {
        fi.createNewFile();
        append = false;
    }
    FileOutputStream fos = new FileOutputStream(fi, append);
    byte[] buf = new byte[DEFAULT_BYTE_SIZE];
    int readCount;
    while((readCount = is.read(buf)) > 0){
        fos.write(buf, 0, readCount);
    }
    fos.close();
    is.close();
    return fi;
}

4.字符输出到文件(FileWriter)

public static File writeCharToFile(String content, String path, boolean append) throws IOException {
    File fi = new File(path);
    // create file first if not exists
    if (!fi.exists()) {
        fi.createNewFile();
        append = false;
    }
    FileWriter fw = new FileWriter(fi, append);
    fw.write(content, 0, content.length());
    fw.close();
    return fi;
}

字节流转换成字符流,文件与字节,文件与字符转换操作完整代码见:
IOTools

以上是关于Java专题十一:IO的主要内容,如果未能解决你的问题,请参考以下文章

Java专栏

java内存流:java.io.ByteArrayInputStreamjava.io.ByteArrayOutputStreamjava.io.CharArrayReaderjava.io(代码片段

java缓冲字符字节输入输出流:java.io.BufferedReaderjava.io.BufferedWriterjava.io.BufferedInputStreamjava.io.(代码片段

面试 IO第十一篇 java IO

初学Java IO之使用FileInputStream和FileReader读取文件 四十一

kuangbin专题专题十一 网络流 Dining POJ - 3281