IO>6. I/O的典型使用方法:
6.1缓冲输入文件:
---------------------------------------------------------------------------------------------------------------------
public static String read(String filename)throws IOException{
StringBuffer sb = new StringBuffer(1024);//默认16byte;
BufferedReader br = new BufferedReader(new FileReader(filename));
String br_temp = null;
while((br_temp=br.readLine())!=null){
sb.append(br_temp+"/n");
}
br.close();//重要,需要关闭流资源;
return sb.toString();
}
tip:
//补充: javac 命令的使用方法: javac ***.java -d . 如果不使用-d 则编译生成文件默认为当前目录;使用-d . 编译生成的
// class文件在以-d 参数 为开始生成class指定的包层次和文件;
// java 命令 如果生成的文件有包层次,需要在包层次的最上层调用java命令,并使用类名包含package路径的全称呼;
// 譬如: 对D:/BufferedInputFile.java 在D目录下执行javac命令
// javac BufferedInputFile.java -d . 则系统生成的class文件: D:/io/BufferedInputFile.class
// 执行时需在D:目录下 使用命令 java io.BufferedInputFile
tip2: // Eclipse中的相对路径.默认为根目录;
//System.out.println(read("./src/io/BufferedInputFile.java"));
//String filename = BufferedInputFile.class.getResource("BufferedInputFile.class").toString();
//System.out.println("BufferedInputFile");
//System.out.println(read(filename));
// 使用默认的javac编译和java执行可以得到正确的处理结构,也即java命令默认.执行命令的路径;
---------------------------------------------------------------------------------------------------------------------
6.2 内存输入文件:
StringReader
---------------------------------------------------------------------------------------------------------------------
/**
* 从内存中的String串输入,从而创建一个StringReader;
* @author Administrator
*
*/
public class StringIOTest {
public static void main(String[] args)throws Exception{
//将String类型转为StringReader中的流;
StringReader in = new StringReader(BufferedInputFile.read("./src/io/practices/StringIOTest.java"));
int c ;
while((c=in.read())!=-1){
System.out.print((char)c);
}
}
}
---------------------------------------------------------------------------------------------------------------------
6.3 格式化的内存输入:
DataInputStream/DataOutputStream
---------------------------------------------------------------------------------------------------------------------
/**
* 格式化的内存输入,使用DataInputStream,因为面向字节,配合原始的输入流InputStream
* @author Administrator
*
*/
public class FormattedMemoryInput {
/**
* @param args
*/
public static void main(String[] args)throws IOException {
//DataInputStream in = new DataInputStream(new ByteArrayInputStream(BufferedInputFile.read("./src/io/practices/FormattedMemoryInput.java").getBytes()));
DataInputStream in = new DataInputStream(new FileInputStream("./src/io/practices/FormattedMemoryInput.java"));
//此处使用异常进行流程控制,通常认为是错误地使用异常的特性;
try{
while(true){
System.out.print((char)in.readByte());
}
}catch(EOFException e){
System.err.println("End of Stream");
}
//使用available,工作方式会随所读取的媒介类型的不同而有所不同,
// 字面意思是"在没有阻塞的情况下所能读取的字节数";
//对于文件,意味着整个文件;对于不同类型的流,可能就不是这样的,因此需要谨慎地使用;
in = new DataInputStream(new FileInputStream("./src/io/practices/FormattedMemoryInput.java"));
while(in.available()!=0){
System.out.print((char)in.readByte());
}
}
}
---------------------------------------------------------------------------------------------------------------------
6.4 基本的文件输出:
PrintWriter(filename) 或者 PrintWriter(BufferedWriter(FileWriter(file,true)))); 其中前一个是自动缓存的输出;后一个是手动缓冲,FileWriter传入的第二个参数是用来判定是否要在文件原来的基础上进行追加;
---------------------------------------------------------------------------------------------------------------------
/**
* 基本的文件输出;
* from Thinking in java 4th;
* @author Administrator
*
*/
public class BasicFileOutput {
static String file = "D:/BasicFileOutput.out";
/**
* @param args
*/
public static void main(String[] args)throws IOException {
long start = System.currentTimeMillis();
BufferedReader in = new BufferedReader(new FileReader("./src/io/practices/BasicFileOutput.java"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file,true)));
// PrintWriter out = new PrintWriter(file); 较为简洁的//PrintWriter调用法
int lineCount = 1;
String myline ;
while((myline=in.readLine())!=null){
pw.println((lineCount++)+":"+myline);
}
pw.close();
in.close();
System.out.println(BufferedInputFile.read(file));
long end = System.currentTimeMillis();
System.out.println(end-start);
}
}
---------------------------------------------------------------------------------------------------------------------
6.5 存储和恢复数据:
DataOutputStream/DataInputStream
---------------------------------------------------------------------------------------------------------------------
/**
* @param args
*/
public static void main(String[] args)throws IOException {
String filename = "D:/data.txt";
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filename)));
dos.writeDouble(3.14145926);
dos.writeUTF("This is PI");
dos.writeDouble(5.2554);
dos.writeUTF("这是我瞎定义的");
dos.close();
DataInputStream din = new DataInputStream(new BufferedInputStream(new FileInputStream(filename)));
System.out.println(din.readDouble());
System.out.println(din.readUTF());
System.out.println(din.readDouble());
System.out.println(din.readUTF());
}
6.6 随机读写访问文件:
RandomAccessFile
---------------------------------------------------------------------------------------------------------------------
public class UsingRandomAccessFile {
static String file = "D:/rtest.dat";
static void display()throws IOException{
RandomAccessFile rf = new RandomAccessFile(file,"r");
for(int i=0;i<7;i++){
System.out.println("value"+i+":"+rf.readDouble());
}
System.out.println(rf.readUTF());
}
/**
* @param args
*/
public static void main(String[] args)throws IOException {
RandomAccessFile rf = new RandomAccessFile(file,"rw");
for(int i=0;i<7;i++){
rf.writeDouble(i*1.414);
}
rf.writeUTF("The end Of File");
rf.close();
display();
System.out.println();
rf = new RandomAccessFile(file,"rw");
rf.seek(5*8);
rf.writeDouble(47.0001);
rf.close();
display();
}
}
---------------------------------------------------------------------------------------------------------------------
6.7 管道流
PipedInputStream,PipedOutputStream,PipedReader和PipedWriter用于任务之间的通信,其意义在于多线程;