10、Reader和Writer
Reader和Writer是所有字符流的抽象基类,使用上和字节流类似,把byte改成char就可以。掌握了字节流,字符流很容易就可以掌握。
public static void test1() { BufferedReader reader = null; BufferedWriter writer = null; try { reader = new BufferedReader(new FileReader(path + "result.txt")); writer = new BufferedWriter(new FileWriter(path + "result1.txt")); String line; while ((line = reader.readLine()) != null) { writer.write(line, 0, line.length()); writer.newLine(); } } catch (Exception e) { } finally { org.apache.commons.io.IOUtils.closeQuietly(reader); IOUtils.closeQuietly(writer); } } public static void test2() { BufferedWriter writer = null; BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(path + "result.txt")); writer = new BufferedWriter(new FileWriter(path + "result1.txt")); char buff[] = new char[1024]; int len; while ((len = reader.read(buff)) != -1) { writer.write(buff, 0, len); } } catch (Exception e) { } finally { IOUtils.closeQuietly(writer); IOUtils.closeQuietly(reader); } }
java IO先整理到这里,后面有碰到相关的问题会继续做整理
时间: 2024-10-25 12:11:04