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: |
Corresponding Java 1.1 class |
InputStream |
Reader |
OutputStream |
Writer |
FileInputStream |
FileReader |
FileOutputStream |
FileWriter |
StringBufferInputStream |
StringReader |
(no corresponding class) |
StringWriter |
ByteArrayInputStream |
CharArrayReader |
ByteArrayOutputStream |
CharArrayWriter |
PipedInputStream |
PipedReader |
PipedOutputStream |
PipedWriter |
。