Java编程思想:I/O的典型使用方式
2024-09-01 01:53:10
import java.io.*; public class Test { public static void main(String[] args) { // BufferedInputFile.test(); // MemoryInput.test(); // FormattedMemoryInput.test(); // TestEOF.test1(); // TestEOF.test2(); // BasicFileOutput.test(); // FileOutputShotcut.test(); // StoringAndRecoveringData.test(); UsingRandomAccessFile.test(); } } /* 打开一个文件用于字符输入,为提高速度,需要用到缓冲 */ class BufferedInputFile { public static String read(String file) { String result = null; try { BufferedReader reader = new BufferedReader(new FileReader(file)); StringBuilder sb = new StringBuilder(); String str; while ((str=reader.readLine()) != null) { sb.append(str+"\n"); } reader.close(); result=sb.toString(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return result; } public static void test() { System.out.println(read("./src/Test.java")); } } /* BufferedInputFile.read()读入的String结果被用来创造一个StringReader 然后调用read()每次读取一个字符,并将它送到控制台 */ class MemoryInput { public static void test() { StringReader sr = new StringReader(BufferedInputFile.read("./src/Test.java")); int c; try { while ((c = sr.read()) != -1) { System.out.println((char) c); } } catch (IOException e) { e.printStackTrace(); } } } /* 要读取格式化数据,可以使用DataInputStream,它是一个面向字节的I/O类(不是面向 字符的)。因此我们必须使用InputStream类而不是Reader类 */ class FormattedMemoryInput { public static void test() { try{ DataInputStream in = new DataInputStream( new ByteArrayInputStream( BufferedInputFile.read("./src/Test.java").getBytes() ) ); while (true) { System.out.println((char)in.readByte()); } } catch (IOException e) { System.out.println("End of stream"); e.printStackTrace(); } } } /* 利用avaliable()来查看还有多少可供读取的字符,用于检测输入是否结束 */ class TestEOF { //需要为ByteArrayInputStream提供字节数组作为构造参数 public static void test1() { try{ DataInputStream in = new DataInputStream( new ByteArrayInputStream( BufferedInputFile.read("./src/Test.java").getBytes() ) ); while (in.available()!=0) { System.out.println((char)in.readByte()); } } catch (IOException e) { e.printStackTrace(); } } //这儿DataInputStream和BufferedInputStream都是装饰器 public static void test2() { try { DataInputStream in = new DataInputStream( new BufferedInputStream( new FileInputStream("./src/Test.java") ) ); } catch (FileNotFoundException e) { e.printStackTrace(); } } } /* 为了提供格式化机制,FileWriter被装饰成了PrintWriter */ class BasicFileOutput { private static String file = "./src/file3"; public static void test() { try { //创建文件输入流 BufferedReader in = new BufferedReader( new StringReader( BufferedInputFile.read("./src/Test.java"))); //创建文件输出流 PrintWriter out = new PrintWriter( new BufferedWriter( new FileWriter(file))); //从输入流写到输出流 int lineCount=1; String str; while ((str = in.readLine()) != null) { out.println(lineCount++ + " : "+str); } out.close(); System.out.println(BufferedInputFile.read(file)); } catch (IOException e) { e.printStackTrace(); } } } /* PrintWriter的辅助构造器,可以减少装饰工作 */ class FileOutputShotcut { private static String file = "./src/file4"; public static void test() { try { //创建文件输入流 BufferedReader in = new BufferedReader( new StringReader( BufferedInputFile.read("./src/Test.java"))); //创建文件输出流 PrintWriter out = new PrintWriter(file); //从输入流写到输出流 int lineCount=1; String str; while ((str = in.readLine()) != null) { out.println(lineCount++ + " : "+str); } out.close(); System.out.println(BufferedInputFile.read(file)); } catch (IOException e) { e.printStackTrace(); } } } /* DataOutputStream输出可供另一个流恢复的数据 */ class StoringAndRecoveringData { public static void test() { try{ //建立输出流,输出数据 DataOutputStream out = new DataOutputStream( new BufferedOutputStream( new FileOutputStream("./src/file4"))); //建立输入流,恢复数据 DataInputStream in = new DataInputStream( new BufferedInputStream( new FileInputStream("./src/file4"))); out.writeDouble(3.1415926); out.writeUTF("That was pi"); out.writeDouble(1.41413); out.writeUTF("Square root of 2"); out.close(); System.out.println(in.readDouble()); System.out.println(in.readUTF()); System.out.println(in.readDouble()); System.out.println(in.readUTF()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } /* RandomAcccessFile拥有读取基本烈性和UTF-8字符串的各种具体的方法,同时seek() 方法可以在文件中到处移动 */ class UsingRandomAccessFile { private static String file = "./src/file5"; private static void display(){ try{ RandomAccessFile rf = new RandomAccessFile(file,"r"); for (int i = 0; i < 7; i++) { System.out.println("VALUE "+i+": "+rf.readDouble()); } System.out.println(rf.readUTF()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private static void write() { try{ RandomAccessFile rf = new RandomAccessFile(file,"rw"); for (int i = 0; i < 7; i++) { rf.writeDouble(i*1.414); } rf.writeUTF("The end of the file"); rf.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void test() { write(); display(); try{ RandomAccessFile rf = new RandomAccessFile(file,"rw"); rf.seek(5*8); rf.writeDouble(47.01); rf.close(); display(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
最新文章
- 破解SQLServer for Linux预览版的3.5GB内存限制 (RHEL篇)
- 我们为什么不能只用O记号来谈论算法?
- css之定位
- Beta版本冲刺第六天
- [转]Linux df 命令不更新磁盘数据空间使用情况的解决办法
- easyui combobox onSelect事件
- Kafka 分区备份实战
- Common Subsequence(dp)
- Java封装自己的Api
- oracle语句随笔
- unique-substrings-in-wraparound-string(好)
- 第四周 更新Scrum站立会议
- jQuery easyui 提示框
- 【技术贴】解决MySql连接不上 ip远程连接Host is not allowed to conn
- UVa784 Maze Exploration
- Java中的String类
- rancher api key
- [0] CollectionBase与索引符DictionaryBase与迭代器
- 3.Flask-SQLAlchemy
- [Swift]LeetCode552. 学生出勤记录 II | Student Attendance Record II
热门文章
- Oracle报错:不是单组分组函数
- 核心思想:互联网创业十问?(大部分创业者是从学习借鉴成功者起步的,不需要把商业模式考虑完备,失败者没资格说趁着年轻...)4种失败的信号 good
- Android零基础入门第71节:CardView简单实现卡片式布局
- 学会了使用qmake -query
- 2015新款 MacBook 用心的测评与试用. 最轻薄的Mac上市
- Understand the Qt containers(有对应表)
- Qt动画效果的幕后英雄:QTimeLine
- MongoDB自学日记1——基本操作
- php和JS 判断http还是https,以及获得当前url的方法
- 初次比较正式的IT职场面试后几点对自己web开发的思考