`

thinking in java notes

    博客分类:
  • java
阅读更多
public class IOStreamDemo {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {

		try {
			// // 1. Buffered input file
			DataInputStream in = new DataInputStream(new BufferedInputStream(
					new FileInputStream("c:/test.xml")));

			String s, s2 = new String();
			while ((s = in.readLine()) != null)
				s2 += s + "\n";

			in.close();

			System.out.println(s2);

			PrintWriter writer = new PrintWriter(System.out);

			//			
			//
			// // 2. Input from memory
			StringBufferInputStream in2 = new StringBufferInputStream(s2);
			int c;
			while ((c = in2.read()) != -1)
				System.out.print((char) c);

			// // 3. Formatted memory input
			try {
				DataInputStream in3 = new DataInputStream(
						new StringBufferInputStream(s2));
				while (true)
					System.out.print((char) in3.readByte());
			} catch (EOFException e) {
				System.out.println("End of stream encountered");
			}

			LineNumberInputStream lineNumberInputStream = new LineNumberInputStream(
					new StringBufferInputStream(s2));
			DataInputStream objectInputStream = new DataInputStream(
					lineNumberInputStream);
			PrintStream printStream = new PrintStream(new BufferedOutputStream(
					System.out));
			while (objectInputStream.available() > 0) {
				printStream.println("Line "
						+ lineNumberInputStream.getLineNumber() + ":"
						+ objectInputStream.readLine());
			}
			printStream.close();
			objectInputStream.close();
			lineNumberInputStream.close();

			// // 4. Line numbering & file output
			try {
				LineNumberInputStream li = new LineNumberInputStream(
						new StringBufferInputStream(s2));
				DataInputStream in4 = new DataInputStream(li);
				PrintStream out1 = new PrintStream(new BufferedOutputStream(
						new FileOutputStream("IODemo.out")));
				while ((s = in4.readLine()) != null)
					out1.println("Line " + li.getLineNumber() + s);
				out1.close(); // finalize() not reliable!
			} catch (EOFException e) {
				System.out.println("End of stream encountered");
			}

			// // 5. Storing & recovering data
			try {
				DataOutputStream out2 = new DataOutputStream(
						new BufferedOutputStream(new FileOutputStream(
								"Data.txt")));
				out2.writeBytes("Here's the value of pi: \n");
				out2.writeDouble(3.14159);
				out2.close();
				DataInputStream in5 = new DataInputStream(
						new BufferedInputStream(new FileInputStream("Data.txt")));
				System.out.println(in5.readLine());
				System.out.println(in5.readDouble());
			} catch (EOFException e) {
				System.out.println("End of stream encountered");
			}

			// 6. Reading/writing random access files
			RandomAccessFile re = new RandomAccessFile("c:/test.txt", "rw");
			for (int i = 0; i < 10; i++) {
				re.writeDouble(10.1 * i);
			}
			for (int i = 0; i < 10; i++) {
				re.writeChar(i + 'A');
			}
			re.close();

			re = new RandomAccessFile("c:/test.txt", "r");
			for (int i = 0; i < 10; i++) {
				System.out.println(re.readDouble());
			}
			for (int i = 0; i < 10; i++) {
				System.out.println(re.readChar());
			}

			RandomAccessFile rf = new RandomAccessFile("rtest.dat", "rw");
			for (int i = 0; i < 10; i++)
				rf.writeDouble(i * 1.414);
			rf.close();

			rf = new RandomAccessFile("rtest.dat", "rw");
			rf.seek(5 * 8);
			rf.writeDouble(47.0001);
			rf.close();

			rf = new RandomAccessFile("rtest.dat", "r");
			for (int i = 0; i < 10; i++)
				System.out.println("Value " + i + ": " + rf.readDouble());
			rf.close();

			// 7. File input shorthand
			InFile in6 = new InFile(args[0]);
			String s3 = new String();
			System.out.println("First line in file: " + in6.readLine());
			in6.close();

			// 8. Formatted file output shorthand
			PrintFile out3 = new PrintFile("Data2.txt");
			out3.print("Test of PrintFile");
			out3.close();

			// 9. Data file output shorthand
			OutFile out4 = new OutFile("Data3.txt");
			out4.writeBytes("Test of outDataFile\n\r");
			out4.writeChars("Test of outDataFile\n\r");
			out4.close();

		} catch (FileNotFoundException e) {
			System.out.println("File Not Found:" + args[0]);
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("IO Exception");
		}

	}

}

class InFile extends DataInputStream {
	public InFile(String filename) throws FileNotFoundException {
		super(new BufferedInputStream(new FileInputStream(filename)));
	}

	public InFile(File file) throws FileNotFoundException {
		this(file.getPath());
	}
}

class PrintFile extends PrintStream {
	public PrintFile(String fileName) throws FileNotFoundException {
		super(new BufferedOutputStream(new FileOutputStream(fileName)));
	}

	public PrintFile(File file) throws FileNotFoundException {
		this(file.getName());
	}
}

class OutFile extends DataOutputStream {
	public OutFile(String fileName) throws FileNotFoundException {
		super(new BufferedOutputStream(new FileOutputStream(fileName)));
	}

	public OutFile(File file) throws FileNotFoundException {
		this(file.getName());
	}
}

 Java IO设计

针对不同的类型(文件,网络,console)做不同的处理(随机访问,顺序访问|| 二进制访问,字符访问,按行,按字符访问)。

Java IO库包含旧的IO库和新的IO库。

 

Java IO分为输入和输出两部分。

输入的基类是InputStream,方法是read

输出的基类是OutputStream,方法是write

但是我们通常不会使用这两个类,而是用复杂的类来利用他们。

通常我们会将多个类堆叠在一起完成多个功能。

因此为了得到一个流,可能得出创建多个类。

 

 

InputStream的作用是表示从多个不同来源,包括:
字符串,

byte数组,

file,

pipe,

net,

FileterInputStream。

 

 

 

 

 

知识点一: 四大等级结构

java语言的i/o库提供了四大等级结构:InputStream,OutputStream,Reader,Writer四个系列的类。InputStream和OutputStream处理8位字节流数据, Reader和Writer处理16位的字符流数据。InputStream和Reader处理输入, OutputStream和Writer处理输出。大家一定要到J2SE文档中看看这四大等级结构的类继承体系。

除了这四大系列类,i/o库还提供了少数的辅助类,其中比较重要的是InputStreamReader和OutputStreamWriter。InputStreamReader把InputStream适配为Reader, OutputStreamWriter把OutputStream适配为Writer;这样就架起了字节流处理类和字符流处理类间的桥梁。

您使用i/o库时,只要按以上的规则,到相应的类体系中寻找您需要的类即可。

 

System.out进行标准输出,它已预封装成一个PrintStream对象。System.err同样是一个PrintStream,但System.in是一个原始的InputStream,未进行任何封装处理。这意味着尽管能直接使用System.out和System.err,但必须事先封装System.in,否则不能从中读取数据。

读入一行数据:

System.out.println(new DataInputStream(System.in).readLine());

管道数据流主要用于多线程程序。

Java 1.1的IO流

我们需要与新结构中的类联合使用老结构中的类。为达到这个目的,需要使用一些“桥”类:InputStreamReader将一个InputStream转换成Reader,OutputStreamWriter将一个OutputStream转换成Writer。
所以与原来的IO流库相比,经常都要对新IO流进行层次更多的封装。

之所以在Java 1.1里添加了Reader和Writer层次,最重要的原因便是国际化的需求。老式IO流层次结构只支持8位字节流,不能很好地控制16位Unicode字符。除此之外,新库也对速度进行了优化,可比旧库更快地运行。

Sources & Sinks:
Java 1.0 class

Corresponding Java 1.1 class

InputStream

Reader
converter: InputStreamReader

OutputStream

Writer
converter: OutputStreamWriter

FileInputStream

FileReader

FileOutputStream

FileWriter

StringBufferInputStream

StringReader

(no corresponding class)

StringWriter

ByteArrayInputStream

CharArrayReader

ByteArrayOutputStream

CharArrayWriter

PipedInputStream

PipedReader

PipedOutputStream

PipedWriter

 

 

分享到:
评论

相关推荐

    Thinking in Java 4th Edition Annotated Solutions Guide

    根据提供的文件信息,以下是对文件《Thinking in Java 4th Edition Annotated Solutions Guide》中所包含知识点的详细解释: 首先,文件标题《Thinking in Java 4th Edition Annotated Solutions Guide》指出了这是...

    Thinking in java .txt

    《Thinking in Java》第四版由布鲁斯·埃克尔(Bruce Eckel)撰写,他是MindView公司的总裁。这本书被广泛认为是学习Java编程语言的经典教材之一。从读者的反馈来看,《Thinking in Java》不仅覆盖了Java的核心概念...

    Thinking in Java 4 源码 导入IDEA可直接运行

    《Thinking in Java》是Bruce Eckel的经典之作,第四版(TIJ4)更是Java程序员必读的书籍之一。这本书深入浅出地介绍了Java语言的核心概念和技术,包括面向对象编程、集合框架、多线程、网络编程等众多主题。源码是...

    Thinking in java4(中文高清版)-java的'圣经'

    研讨课 Hands-on Java研讨课CD Thinking in Objects研讨课 Thinking in Enterprise Java Thinking in Patterns(with Java) Thinking in Patterns研讨课 设计咨询与复审 附录B 资源 软件 编辑器与IDE 书籍 分析与设计...

    Thinking in Java 练习题答案

    《Thinking in Java》是Bruce Eckel的经典之作,它深入浅出地介绍了Java语言的核心概念和技术。这本书的练习题是学习Java的重要组成部分,因为它们能够帮助读者巩固理论知识并提升实践能力。以下是对"Thinking in ...

    Thinking in Java 4th Edition + Annotated Solution Guide (代码)英文文字版 带书签 有答案

    《Thinking in Java》是Bruce Eckel的经典之作,第四版涵盖了Java编程语言的广泛主题,适合初学者和有经验的程序员。这本书深入浅出地讲解了Java的核心概念和技术,旨在帮助读者建立坚实的编程基础,并理解面向对象...

    Thinking In Java-Java 编程思想(中英文版 第四版)

    Thinking In Java-Java 编程思想(中英文版 第四版) Thinking In Java-Java 编程思想(中英文版 第四版)

    《thinking in java》第三版完整PDF书籍+习题答案(中文版)

    《Thinking in Java》是Bruce Eckel的经典之作,被誉为学习Java编程的权威指南。该书以其深入浅出的方式,详尽地介绍了Java语言的核心概念和技术。第三版是此书的一个重要里程碑,它涵盖了Java语言的诸多关键特性,...

    Thinking in Java Second Edition.doc

    在众多关于Java的学习材料中,《Thinking in Java》第二版以其独特的视角和方法论成为了一本备受推崇的教科书。 本书由Bruce Eckel所著,作为一本权威性的Java编程教程,它不仅仅是向读者介绍Java语言的语法和基础...

    Thinkingin Java电子书

    《Thinking in Java》是一本备受推崇的Java编程教材,由Bruce Eckel撰写,被誉为Java学习者的必读之作。这本书深入浅出地介绍了Java语言的核心概念和技术,覆盖了从基础到高级的主题,对于有一定Java基础的读者来说...

    thinking in java 第四版 源码

    《Thinking in Java》是Bruce Eckel的经典之作,第四版更是被广大Java开发者视为学习和进阶的重要参考书籍。这本书深入浅出地介绍了Java语言的核心概念和技术,包括面向对象编程、集合框架、多线程、网络编程、GUI...

    Thinking in Java 习题答案

    《Thinking in Java》是Bruce Eckel的经典编程教材,它深入浅出地介绍了Java语言的核心概念和技术。这本书以其详尽的解释、丰富的示例和实践性强的习题深受程序员喜爱。"Thinking in Java 习题答案"是配套的解答集,...

    Thinking in java 电子书

    《Thinking in Java》是 Bruce Eckel 编著的一本经典的Java编程教材,它深受程序员喜爱,被誉为学习Java的必备参考书。这本书全面深入地探讨了Java语言的核心概念和技术,不仅适合初学者,也对有经验的程序员提供了...

    Thinking in Java 3rd Edition.rar

    Thinking in Java 3rd Edition.rar Thinking in Java 3rd Edition.rar Thinking in Java 3rd Edition.rar Thinking in Java 3rd Edition.rar Thinking in Java 3rd Edition.rar Thinking in Java 3rd Edition.rar

    thinking in java annotated solution guide 电子书格式

    《Thinking in Java》是Bruce Eckel的经典Java编程教材,它以其深入浅出的讲解和全面系统的内容深受程序员喜爱。这本书的Annotated Solution Guide是作者为读者提供的配套解答指南,帮助读者理解和解决书中练习题,...

    Thinking In Java(英文版 第四版)

    《Thinking In Java(英文版 第四版)》是一本由Bruce Eckel所著的经典Java编程教程。Bruce Eckel是MindView公司的总裁,他凭借自己在Java编程领域的深刻理解和丰富的教学经验,为读者呈现了一本深受世界各地Java...

    Thinking In Java 源码

    《Thinking in Java》是Bruce Eckel的经典Java编程书籍,它深入浅出地讲解了Java语言的核心概念和技术。这本书不仅适合初学者,也对有经验的程序员提供了深入理解Java的宝贵资源。现在,我们有机会免费下载这本书的...

    Thinking in java java核心思想英文原版(带目录)

    Thinking in java java核心思想英文版(带目录),学java必备

    Thinking in Java (中文版)-经典书籍

    他是《Thinking in Java》、《Thinking in C++》、《C++ Inside & Out》《Using C++》和《Thinking in Patterns》的作者,同时还是《Black Belt C++》文集的编辑。他的《Thinking in C++》一本书在1995年被评为...

    Thinking in Java

    《Thinking in Java》是Bruce Eckel的经典之作,它被誉为学习Java编程的最佳教材之一。这本书以其深入浅出的讲解方式和全面覆盖的Java知识点而受到广大程序员的推崇。本压缩包包含的是《Thinking in Java》的第三版...

Global site tag (gtag.js) - Google Analytics