热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Java编程思想:I/O的典型使用方式

importjava.io.*;publicclassTest{publicstaticvoidmain(String[]args){//

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();
}
}
}

&#160;


推荐阅读
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • Linux环境变量函数getenv、putenv、setenv和unsetenv详解
    本文详细解释了Linux中的环境变量函数getenv、putenv、setenv和unsetenv的用法和功能。通过使用这些函数,可以获取、设置和删除环境变量的值。同时给出了相应的函数原型、参数说明和返回值。通过示例代码演示了如何使用getenv函数获取环境变量的值,并打印出来。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 本文介绍了一个题目的解法,通过二分答案来解决问题,但困难在于如何进行检查。文章提供了一种逃逸方式,通过移动最慢的宿管来锁门时跑到更居中的位置,从而使所有合格的寝室都居中。文章还提到可以分开判断两边的情况,并使用前缀和的方式来求出在任意时刻能够到达宿管即将锁门的寝室的人数。最后,文章提到可以改成O(n)的直接枚举来解决问题。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • 开发笔记:实验7的文件读写操作
    本文介绍了使用C++的ofstream和ifstream类进行文件读写操作的方法,包括创建文件、写入文件和读取文件的过程。同时还介绍了如何判断文件是否成功打开和关闭文件的方法。通过本文的学习,读者可以了解如何在C++中进行文件读写操作。 ... [详细]
  • Ihavethefollowingonhtml我在html上有以下内容<html><head><scriptsrc..3003_Tes ... [详细]
author-avatar
公民不是百姓2
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有