技术栈

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

2019-04-30  本文已影响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();
}
上一篇 下一篇

猜你喜欢

热点阅读