java基础---->java输入输出流
2017-01-18 14:29
106 查看
今天我们总结一下java中关于输入流和输出流的知识,博客的代码选自Thinking in java一书。我突然很想忘了你,就像从未遇见你。
available()的工作方式会随着所读取的媒介类型的不同而有所不同。也就是说在没有阻塞的情况下所能读取的字节数。对于文件,这意味着整个文件。但是对于不同类型的流,可能就不是这样的。
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.654 3.14156 我爱你! 23.654 2.366 我爱你! */
友情链接
相关文章推荐
- Java语言基础-IO流(输入输出流) 其他
- Java 7之传统I/O第1篇- 输入输出流基础框架
- [置顶] Java基础入门-IO输入输出流
- JAVA基础(二)输入输出流总结
- Java基础之IO输入输出流
- java基础学习中理解的问题-输入输出流,a++,++a
- java基础—8.IO输入输出流
- 【Java基础知识】IO流--标准输入输出流、打印流PrintStream
- Java基础知识强化之IO流笔记61:标准输入输出流的本质
- JavaIO 总结笔记<三> 基本字节字符输入输出流和文件复制
- java基础<IO输出与输入>--->IO流<字节流>
- Java基础-IO-字节输入输出流(笔记)
- 黑马程序员——JAVA基础——IO(一)---流概述,节点流、处理流、转换流与标准输入输出流、打印流、File文件对象、合并流
- Java基础-22总结登录注册IO版,数据操作流,内存操作流,打印流,标准输入输出流,转换流,随机访问流,合并流,序列化流,Properties
- Java语言基础-IO流(输入输出流) 字符流
- java基础<IO输出与输入>--->字符编码
- 黑马程序员_java基础之IO(输入,输出流)
- java基础-输入输出流-大文本文件读取及写入另一文本-----(此程序print部分有bug,输出不正常,原因琢磨不清了。。。)
- Java学习笔记-《Java程序员面试宝典》-第四章基础知识-4.7输入输出流(4.7.1-4.7.3)
- Java基础 - 序列流,内存输出流,随机访问流,对象操作流,数据输入输出流,打印流,标准输入输出流,Properties