java IO 流的学习(我们到底能走多远系列1)
我们到底能走多远系列(1)
“我们到底能走多远系列”:开始我的java之路。我要挖出这个地道,离开这里。
IO入门代码阅读:
字节流:
private void writeTxt(String path, String value) throws IOException{ OutputStream fos = new FileOutputStream(path);//构造方法1 fos.write(value.getBytes()); fos.close(); } private void readTxt(String path) throws IOException{ File file = new File(path); InputStream fis = new FileInputStream(path);//构造方法2 byte b[] = new byte[(int)file.length()] ; fis.read(b); System.out.print(new String(b)); fis.close(); }
字符流:
private void writeTxt(String path, String value) throws IOException{ Writer writer = new FileWriter(path); writer.write(value); writer.close(); } private void readTxt(String path) throws IOException{ Reader reader = new FileReader(path); char c[] = new char[1024] ; int len = reader.read(c); System.out.print(new String(c, 0, len)); reader.close(); }