美文网首页技术栈
2019-04-30——Java IO 字符流

2019-04-30——Java IO 字符流

作者: 烟雨乱平生 | 来源:发表于2019-04-30 10:16 被阅读0次
private void useInputStreamReader(String filePath) throws IOException {
    char[] data = new char[1024];
    InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath));
    int size;
    while ((size = isr.read(data))!=-1){
        String content = new String(data,0,size);
        System.out.println(content);
    }
    isr.close();
}

private void useOutputStreamWriter(String filePath) throws IOException {
    OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(filePath));
    osw.write("my name is wang ya wei");
    osw.flush();
    osw.close();
}

private void useFileReader(String filePath) throws IOException {
    char[] data = new char[1024];
    FileReader fr = new FileReader(filePath);
    int size;
    while ((size = fr.read(data))!=-1){
        String content = new String(data,0,size);
        System.out.println(content);
    }
    fr.close();
}

private void useFileWriter(String filePath) throws IOException {
    char[] data = new char[1024];
    FileWriter fw = new FileWriter(filePath);
    fw.write("this is file writer");
    fw.flush();
    fw.close();
}

相关文章

网友评论

    本文标题:2019-04-30——Java IO 字符流

    本文链接:https://www.haomeiwen.com/subject/jampnqtx.html