缓冲输入文件
如果想要打开一个文件,读取里面的内容,将内容作为字符。我们可以用FileInputReader,它接受String类型的文件路径或File类型的文件。为了提高速度,我们希望对文件内容进行缓冲,所以我们将FileInputReader传给一个BufferReader,然后通过它的readLine()来读取。一旦读取到文件末尾,readLine()将返回null,所以我们用它来终止读取。代码如下:
BufferedReader in = new BufferedReader(new FileReader("E:/test.txt"));
String s, s2 = new String();
while ((s = in.readLine()) != null) {
s2 += s + "\n";
}
in.close();
System.out.println(s2);
如上所述,new FileReader("E:/test.txt")也可以传入一个File类。
在读取时,readLine()会将换行符删除。所以我们一定要自己加上。另外,在最后我们一定要记得调用close()来关闭流。
下面这段代码展示了如何从标准输入读取:
BufferedReader stdin = new BufferedReader(new InputStreamReader(
System.in));
System.out.println("请输入一行字,以回车结束:");
System.out.println("从标准输入读取内容:" + stdin.readLine());
当程序运行时,程序会提示用户输入,输入完成后按回车。这时控制台会将用户输入的内容打印出来。
从内存输入
StringReader in2 = new StringReader(s2);
int c;
while ((c = in2.read()) != -1) {
System.out.println((char) c);
}
in2.close();
s2是已经被读取到内存中的字符串。这里将它读入流,然后打印。当然,直接就可以打印它,不用这么麻烦。
格式化的内存输入
格式化数据意思就是读取特定类型的数据。这里要用到DataInputStream,它是一个面向字节的I/O类,不是面向字符的。因此我们必须用InputStream类,而不是Reader类.
try {
DataInputStream in3 = new DataInputStream(new ByteArrayInputStream(
s2.getBytes()));
while (true) {
System.out.println((char) in3.readByte());
}
} catch (EOFException e) {
System.out.println("流结束");
}
这里我们是从内存中读取的,也可以从文件中读取,如:
DataInputStream in = new DataInputStream(new
BufferedInputStream(new
FileInputStream(“E:/test.txt”)));
while(in.avaliable() != 0){
System.out.print((char)in.readByte());
}
文件输出
向一个文件输入内容,先要创建一个指定文件的FileWriter,然后再用BufferedWriter将其包装一下以缓冲输入提高性能(不是必须,但有好处)。最后转换成PrintWriter。
try {
BufferedReader in4 = new BufferedReader(new StringReader(s2));
PrintWriter out1 = new PrintWriter(new BufferedWriter(
new FileWriter("E:/out.txt")));
int lineCount = 1;
while ((s = in4.readLine()) != null) {
out1.print(lineCount++ + ":" + s);
}
out1.close();
} catch (EOFException e) {
System.out.println("流结束");
}
一定要记住关闭流。
从标准输入读取
按照I/O模型,JAVA提供了System.in,System.out和System.err。其中System.out已经被封装成了PrintStream对象,System.err也被封装成了PrintStream,但System.in却是一个未被封装过的InputStream。所以在读取它之前要进行封装。就象前面的:
BufferedReader stdin = new BufferedReader(new InputStreamReader(
System.in));
System.out转换成PrintWriter
System.out是一个PrintStream,而PrintStream是一个OutputStream.PrintWriter可以接收OutputStream作为构造函数的参数。所以在需要时,我们可以把System.out转换成PrintWriter。
PrintWriter out = new
PrintWriter(System.out,true);
Gzip简单压缩
BufferedReader in = new BufferedReader(new FileReader("E:/test.txt"));
BufferedOutputStream out = new BufferedOutputStream(
new GZIPOutputStream(new FileOutputStream("E:/test.gz")));
System.out.println("压缩成GZIP文件");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
in.close();
out.close();
System.out.println("读取GZIP文件内容");
BufferedReader in2 = new BufferedReader(new InputStreamReader(
new GZIPInputStream(new FileInputStream("E:/test.gz"))));
String s;
while ((s = in2.readLine()) != null) {
System.out.println(s);
}
Zip的多文件保存
public static void main(String[] args) throws IOException {
String[] arg = {"E:/test.txt","E:/out.txt","E:/out2.txt"};
zip(arg);
}
public static void zip(String[] args) throws IOException {
FileOutputStream f = new FileOutputStream("E:/test.zip");
CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
ZipOutputStream zos = new ZipOutputStream(csum);
BufferedOutputStream out = new BufferedOutputStream(zos);
zos.setComment("测试JAVA的zip压缩");
for (int i = 0; i < args.length; i++) {
System.out.println("压缩文件:" + args[i]);
BufferedReader in = new BufferedReader(new FileReader(args[i]));
zos.putNextEntry(new ZipEntry(args[i]));
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
in.close();
}
out.close();
System.out.println("CheckSum:" + csum.getChecksum().getValue());
System.out.println("解压文件:");
FileInputStream fi = new FileInputStream("E:/test.zip");
CheckedInputStream csumi = new CheckedInputStream(fi, new Adler32());
ZipInputStream in2 = new ZipInputStream(csumi);
BufferedInputStream bis = new BufferedInputStream(in2);
ZipEntry ze;
while ((ze = in2.getNextEntry()) !=
null) {
System.out.println("正在读取:" + ze);
int x;
while ((x = bis.read()) != -1) {
System.out.write(x);
}
}
System.out.println("CheckSum:" + csumi.getChecksum().getValue());
bis.close();
ZipFile zf = new ZipFile("E:/test.zip");
Enumeration e = zf.entries();
while (e.hasMoreElements()) {
ZipEntry ze2 = (ZipEntry) e.nextElement();
System.out.println("文件:" + ze2);
}
}