Java IO的一些典型应用,内容来源于《Java编程思想》
1.缓冲输入文件
public class BufferedInputFile {
public static String read(String filename) throws IOException {
/**
* 为了提高速度,使用BufferedReader,添加缓存功能
*/
BufferedReader in = new BufferedReader(new FileReader(filename));
String s;
StringBuilder sb = new StringBuilder();
while ((s = in.readLine()) != null) {
sb.append(s + "\n");
}
in.close();
return sb.toString();
}
public static void main(String[] args) throws IOException {
System.out.println(read("E:\\t.txt"));
}
}
2.从内存输入
import java.io.IOException;
import java.io.StringReader;
public class MemoryInput {
public static void main(String[] args) throws IOException {
StringReader in = new StringReader(BufferedInputFile.read("E:\\t.txt"));
int c;
while ((c = in.read()) != -1) {
//返回的int必须通过转化才能为char才能打印
System.out.print((char) c);
}
}
}
3.格式化读取数据
(1)使用DataInputStream字节流读取,流数据结束用异常判断
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
/**
* 格式化数据的读取
* @author chenxh
*
*/
public class FormattedMemeoryInput {
public static void main(String[] args) throws IOException {
try {
DataInputStream in = new DataInputStream(new ByteArrayInputStream(
BufferedInputFile.read("src\\com\\blueinfo\\io\\classify\\FormattedMemeoryInput.java")
.getBytes()));
while (true) {
System.out.print((char)in.readByte());
}
} catch (EOFException e) {
System.err.println("end of stream");
}
}
}
(2)流数据结束用,DataInputStream.available判断,获得流中还有多少数据可以读取,不为0时,可以读。
available函数的意思是:在没有阻塞的情况下所能读取的字节数。对于文件来说就是整个文件的数据;对于其他数据源,意义可能不同。
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class TestEOF {
public static void main(String[] args) throws IOException {
DataInputStream in = new DataInputStream(
new BufferedInputStream(
new FileInputStream(
"src\\com\\blueinfo\\io\\classify\\FormattedMemeoryInput.java")));
while(in.available() !=0 ){
System.out.print((char)in.readByte());
}
}
}
4.文本文件输出
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
public class FileOutput {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(
new StringReader(BufferedInputFile.read("E:\\t.txt")));
/**
* 使用快捷构造函数,相当于new PrintWriter( new BufferedWriter( new FileWriter(file)));
*/
PrintWriter out = new PrintWriter("E:\\out.txt");
int lineCount = 1;
String s;
while((s = in.readLine()) != null){
out.println(lineCount++ + ":" + s);
}
out.close();
System.out.print(BufferedInputFile.read("E:\\out.txt"));
}
}
5.存储和恢复数据
public class StoringAndRecoveringData {
public static void main(String[] args) throws IOException {
/**
* 按格式存储数据
*/
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(
new FileOutputStream("E:\\data.txt")));
out.writeDouble(3.14159);
out.writeUTF("this was pi");
out.writeDouble(1.41413);
out.writeUTF("2的平方根");
out.close();
/**
* 按格式得到数据
*/
DataInputStream in = new DataInputStream(new BufferedInputStream(
new FileInputStream("E:\\data.txt")));
System.out.println(in.readDouble());
System.out.println(in.readUTF());
System.out.println(in.readDouble());
System.out.println(in.readUTF());
in.close();
}
}