今天我们总结一下java中关于输入流和输出流的知识,博客的代码选自Thinking in java一书。我突然很想忘了你,就像从未遇见你。
java中的输入流
huhx.txt文件的内容如下: I love you, ch. 中文
一、缓冲中输入文件
public class BufferedInputFile { public static String read(String filename) { BufferedReader reader = null; StringBuilder builder = new StringBuilder(); try { reader = new BufferedReader(new FileReader(filename)); String line; while ((line = reader.readLine()) != null) { builder.append(line + "\n"); } } catch (Exception e) { e.printStackTrace(); } finally { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } return builder.toString(); } public static void main(String[] args) { String content = read("file/huhx.txt"); System.out.println(content); }}
二、从内存输入
public class MemoryInput { public static void main(String[] args) { StringReader reader = new StringReader(BufferedInputFile.read("file/huhx.txt")); int c; try { while ((c = reader.read()) != -1) { System.out.print((char) c); } } catch (IOException e) { e.printStackTrace(); } }}
三、格式化的内存输入
public class FormattedMemoryInput { public static void main(String[] args) { DataInputStream inputStream = new DataInputStream( new ByteArrayInputStream(BufferedInputFile.read("file/huhx.txt").getBytes())); try { while (inputStream.available() != 0) { System.out.print((char)inputStream.readByte()); } } catch (IOException e) { e.printStackTrace(); } }}
available()的工作方式会随着所读取的媒介类型的不同而有所不同。也就是说在没有阻塞的情况下所能读取的字节数。对于文件,这意味着整个文件。但是对于不同类型的流,可能就不是这样的。
java中输出流
一、基本的文件输出
public class BasicFileOutput { static String file = "file/linux.txt"; public static void main(String[] args) { BufferedReader reader = new BufferedReader(new StringReader(BufferedInputFile.read("file/huhx.txt"))); PrintWriter out = null; try { out = new PrintWriter(new BufferedWriter(new FileWriter(file))); String line; while ((line = reader.readLine()) != null) { out.println(line); } } catch (IOException e) { e.printStackTrace(); } finally { out.close(); } System.out.println(BufferedInputFile.read(file)); }}
二、文本文件输出的快捷方式
public class FileOutputShortcut { static String file = "file/linux.out"; // 这个也和上述不一样 public static void main(String[] args) { BufferedReader reader = new BufferedReader(new StringReader(BufferedInputFile.read("file/huhx.txt"))); PrintWriter out = null; try { out = new PrintWriter(new FileWriter(file)); // 这里和上述不一样 String line; while ((line = reader.readLine()) != null) { out.println(line); } } catch (IOException e) { e.printStackTrace(); } finally { out.close(); } System.out.println(BufferedInputFile.read(file)); }}
三、存储和恢复数据
public class StoringAndRecoveringData { public static void main(String[] args) { try { DataOutputStream out = new DataOutputStream( new BufferedOutputStream(new FileOutputStream("file/chenhui.txt"))); out.writeDouble(3.1415926); out.writeUTF("中文可以吗?"); out.writeInt(123); out.close(); DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("file/chenhui.txt"))); System.out.println(in.readDouble()); // 如果这里是readInt(),会导致后面的readUTF方法报错 System.out.println(in.readUTF()); System.out.println(in.readInt()); in.close(); } catch (Exception e) { e.printStackTrace(); } }}
三、读写随机访问文件
public class UsingRandomAccessFile { static String file = "file/huhx.txt"; static void display() throws Exception { RandomAccessFile rf = new RandomAccessFile(file, "r"); System.out.println(rf.readDouble()); System.out.println(rf.readDouble()); System.out.println(rf.readUTF()); rf.close(); } public static void main(String[] args) { RandomAccessFile rf; try { rf = new RandomAccessFile(file, "rw"); rf.writeDouble(23.654); rf.writeDouble(3.14156); rf.writeUTF("我爱你!"); rf.close(); display(); rf = new RandomAccessFile(file, "rw"); rf.seek(1 * 8); rf.writeDouble(2.366); rf.close(); display(); } catch (Exception e) { e.printStackTrace(); } }}/*23.6543.14156我爱你!23.6542.366我爱你!*/
友情链接