`

JAVA I/O使用方法

 
阅读更多

下面四张图表明了类之间的继承关系,其中 红色、加粗的类名是常用的类

 

常用转换

FileReader——>BufferedReader

BufferedReader in= new BufferedReader(new FileReader("Text.java"));

InputStream——>InputStreamReader——>BufferedReader

BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

String——>byte[]——>ByteArrayInputStream——>DataInputStream

DataInputStream in= new DataInputStream(new ByteArrayInputStream(str.getBytes()));

FileInputStream——>BufferedInputStream——>DataInputStream

DataInputStream in=new DataInputStream(new BufferedInputStream(new FileInputStream("Data.txt")));

FileWriter——>BufferedWriter——>PrintWriter

PrintWriter pw=new PrintWriter(new BufferedWriter("text.out"));

System.out(PrintStream)——>PrintWriter

PrintWriter pw=new PrintWriter(System.out,true);

FileOutputStream——>BufferedOutputStream——>PrintStream

PrintStream ps= new PrintStream(new BufferedOutputStream(new FileOutputStream("text.out")));

FileOutputStream——>BufferedOutputStream——>DataOutputStream

DataOutputStream dos= new DataOutputStream(new BufferedOutputStream(new FileOutputStream("Data.txt")));

程序举例

import java.io.*;

public class IoStreamDemo {

public static void main(String[] args) throws IOException {

1.要想打开文件读取字符,你得先用String或File对象创建一个FileReader。

为了提高速度,你应该对这个文件作缓冲,因此你得把FileReader的reference 交给BufferedReader。

BufferedReader提供了readLine()方法,当你读到文件的末尾时readLine()会返回一个null,于是就退出while()循环了。

String sb是用来累加文件内容的,(再加一个换行符“\n”因为readLine()会把它们都剥掉).

最后用close()来清空缓冲区。

BufferedReader in = new BufferedReader(new FileReader("IOStreamDemo.java"));

String s,s2= new String();

StringBuffer sb = new StringBuffer();

while((s = in.readLine())!= null)

{ sb.append(s);

sb.append("\n");

}

in.close();

s2=sb.toString();

2. 用System.in生成一个能读取控制台输入的流。System.in是一个InputStream,

而BufferedReader需要一个Reader作参数,所以要先通过InputStreamReader来转转手。

Java遵循标准I/O的模型,提供了Syetem.in,System.out,以及System.err。

System.out是一个已经预先处理过的,被包装成PrintStream的对象。

System.err也是一个PrintStream;

System.in是一个未经处理的InputStream。

也就是说,虽然你可以直接往System.out和System.err上写,但是要想读System.in的话,就必须先做处理了。

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter a line:");

System.out.println(stdin.readLine());

3. 用String s2作参数创建一个StringReader。然后用StringReader的read()方法把字符读出来,再送到控制台上去。read()会把读出来的byte当作int,所以要想正常打印的话,你得先把它们转成char。

StringReader in2 = new StringReader(s2);

int c;

while((c = in2.read()) != -1)

System.out.print((char)c);

4. 要想读取"格式化"的数据,你就得用DataInputStream了,DataInputStream是一个面向byte的I/O类,不是面向char 的。因此你只能从头到底一直用InputStream了。当然你可以把所有的东西都当成byte。然后用InputStream读出来。但这里是 String。要想把String当成byte数组。可以用String的getBytes()方法。而ByteArrayInputStream是可以 处理byte数组的。

try {

DataInputStream in3 = new DataInputStream(

new ByteArrayInputStream(s2.getBytes()));

while(true)

System.out.print((char)in3.readByte());

} catch(EOFException e) {

System.err.println("End of stream");

}

5.向文件中写数据。先创建一个FileWriter,BufferedWriter是免不掉的。

然后再让PrintWriter去排版。这样就能得出能够读得懂的,普通的文本文件了。

输入流用完之后,readLine()会返回null。

最后调用close()方法清空缓冲区。

try {

BufferedReader in4 = new BufferedReader( new StringReader(s2));

PrintWriter out1 = new PrintWriter( new BufferedWriter(new FileWriter("IODemo.out")));

int lineCount = 1;

while((s = in4.readLine()) != null )

out1.println(lineCount++ + ": " + s);

out1.close();

} catch(EOFException e) {

System.err.println("End of stream");

}

}

6. 存储和恢复数据

PrintWriter会对数据进行格式化,这样人就能读懂了。但是如果数据输出之后,还要恢复出来供其它流用,那你就必须用 DataOutputStream来写数据,再用DataInputStream来读数据了。当然,它们可以是任何流,不过我们这里用的是一个经缓冲的文 件。DataOutputStream和DataInputStream是面向byte的,因此这些流必须都是InputStream和 OutputStream。

如果数据是用DataOutputStream写的,那么不管在哪个平台上,DataInputStream都能准确地把它还原出来。这一点真是太有用了,因为没人知道谁在为平台专属的数据操心。如果你在两个平台上都用Java,那这个问题就根本不存在了。

用DataOutputStream写String的时候,要想确保将来能用DataInputStream恢复出来,唯一的办法就是使用UTF-8编码。

UTF-8是Unicode的一种变形。Unicode用两个字节来表示一个字符。但是,如果你处理的全部,或主要是ASCII字符(只有7位),那么无 论从存储空间还是从带宽上看,就都显得太浪费了,所以UTF-8 用一个字节表示ASCII字符,用两或三个字节表示非ASCII的字符。此外,字符串的长度信息存在(字符串)的头两个字节里。writeUTF( )和readUTF( )用的是Java自己的UTF-8版本(JDK文档里有关于这个字符集的完整讲解,就在这两个方法的文档里),所以如果你要用一个Java程序读取 writeUTF( )写的字符串的话,就必须进行一些特殊处理了。

有了writeUTF( )和readUTF( ),你就能放心地把String和其它数据混在一起交给DataOutputStream了,因为你知道String是以Unicode的形式存储的,而且可以很方便地用DataOutputStream恢复出来。

writeDouble( )会往流里写double,而它"影子"readDouble( )则负责把它恢复出来(其它数据也有类似的读写方法)。但是要想让读取方法能正常工作,你就必须知道流的各个位置上都放了些什么数据。因为你完全可以把 double读成byte,char,或其它什么东西。所以要么以固定的格式写文件,要么在文件里提供额外的解释信息,然后一边读数据一边找数据。先提一 下,对于复杂数据的存储和恢复,对象的序列化可能会比较简单。

try {

DataOutputStream out2 = new DataOutputStream(

new BufferedOutputStream(new FileOutputStream("Data.txt")));

out2.writeDouble(3.14159);

out2.writeUTF("That was pi");

out2.writeDouble(1.41413);

out2.writeUTF("Square root of 2");

out2.close();

DataInputStream in5 = new DataInputStream(

new BufferedInputStream( new FileInputStream("Data.txt")));

System.out.println(in5.readDouble());

System.out.println(in5.readUTF());

System.out.println(in5.readDouble());

System.out.println(in5.readUTF());

} catch(EOFException e) {

throw new RuntimeException(e);

}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics