IO流的学习

来源:互联网 发布:优酷登陆数据连接失败 编辑:程序博客网 时间:2024/06/05 17:38

所谓流,是指一组有顺序的字节集合,是对数据传输的抽象,而又根据各种传输的需求,将流定义为各种类。各种流的一般都含有File文件的形参。要理解,流是从文件对象抽象而来的。

根据流向的不同,可以分为输入流和输出流。根据处理数据的不同,可以分为字符流和字节流。

一般含有Stream的是字节流,含有Reader、writer的是字符流。注意结合类的定义字符去理解它的含义。

首先来总结一下各种流:

文件操作相关流:FileReader、FileWriter、FileInputStream、FileOutputStream

数组操作相关流:ByteArrayInputStream、ByteArrayOutputStream

基本数据类型相关流:DataInputStream、DataOutputStream

对象序列化和反序列化:ObjectInputStream、ObjectOutputStream、sierializable、transient

带缓存的字节流:BufferedInputStream、BufferedOutputStream

带缓存的字符流:BufferedReader、BufferedWriter

多线程通讯流:PipeInputStream、PipeOutputStream


下面结合代码来讲述各种流的使用。


文件的创建、删除等操作

首先,关于File类,通常用File类对象来指向某个文件。有两个File常量,分别是:File.separator代表“/”,以及File.pathSeparator代表“;”。这两个常量在指定文件的路径的时候十分有用,比如说:

String fileName ="D:"+File.separator+"abd.txt";File f = new File(fileName);
如果文件不存在,这里可以调用函数来创建文件:

f.createNewFile();
同样也可以判断文件是否存在、删除文件等操作,比如说:

if(f.exists()){f.delete();System.out.println("The file have being delet!");}

获取指定目录下的所有文件

获取指定文件夹的所有文件名的操作可以使用list(),它返回的是String数组。不过这个方法返回的只是文件名,如果想要获取完成的路径,可以使用listFile()方法,此时返回的是File文件。此时的代码如下:

String drc ="D:"+File.separator;File f3 =  new File(drc);String[] str = f3.list();File[] str2 = f3.listFiles();for(int i=0;i<str.length;i++){System.out.println(str[i]);}for(int i=0;i<str.length;i++){System.out.println(str2[i]);}if(f3.isDirectory()){System.out.println("YES, this is a directory.");}else System.out.println("NO.");


搜索指定目录下的所有文件,并把文件名写入另一个文件

public static void print(File f,File f2) throws IOException{OutputStream www = new FileOutputStream(f2,true);byte[] newLine = System.getProperty("line.separator").getBytes();if(f != null){if(f.isDirectory()){<span style="white-space:pre"></span>File[] fileArray = f.listFiles();<span style="white-space:pre"></span>if(fileArray != null){<span style="white-space:pre"></span>for(int i=0;i<fileArray.length;i++){<span style="white-space:pre"></span><span style="white-space:pre"></span>print(fileArray[i],f2);<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}}else{www.write(f.toString().getBytes());www.write(newLine);}}www.close();}
在这个方法中,传入两个数据,f为指定目录,f2为某一个文件类对象。从目录f下读取的所有文件名写入到f2文件中。同时,这里使用了递归调用的技巧。同时,这里用到了InputStream中的换行的技巧,使用了一个newLine字节数组,这个newLine的内容如下:

byte[] newLine = System.getProperty("line.separator").getBytes();

RandomAccessFile的使用

RandomAccessFile可以从任意的位置写入或者是读取流的内容。不多讲,看一个例子:

RandomAccessFile demo = new RandomAccessFile(f,"rw");String random = "randomAccessFile";demo.write(random.getBytes());demo.write(newLine);demo.writeChars(random);demo.write(newLine);

FileInputStream和FileOutputStream的使用

这两个流是最常用的字节流,几乎所有的文件都可以使用这两个流,用一个例子来说明这两个流的使用:

FileOutputStream fileOutput = new FileOutputStream(f,true);//true表示在文件尾追加,不加的话表示从文件首位开始写入String mark1 = "路径分隔符:";fileOutput.write(mark1.getBytes());fileOutput.write(File.separator.getBytes());// File类中的常量"/"fileOutput.write(newLine);fileOutput.write(<span style="font-family: Arial, Helvetica, sans-serif;">"this is a test file".getBytes()</span>);fileOutput.close();byte[] temp = new byte[1024];//定义一个缓存数组InputStream in =new FileInputStream(f);in.read(temp);System.out.println(temp.toString());//使用FileInputStream读文件的一种方式int flag=0;//这是一个从文件头读到文件尾,并打印字符的经典代码段。read()方法读到文件尾的返回值为-1while((-1) != (flag=in.read())){System.out.println((char)flag);}in.close();//文件复制if(!f.exists()){System.out.println("被复制的文件不存在");System.exit(1);;}InputStream input = new FileInputStream(f);OutputStream output = new FileOutputStream(f4);if((input!=null)&&(output!=null)){int tempuse =0;while((-1)!=(tempuse=input.read())){<span style="white-space:pre"></span>output.write(tempuse);}}input.close();output.close();
注意所有的流,当使用完毕时一定要关闭,关闭的规则是:后打开的先关闭。

在文件复制的代码中,其规则就是从一个文件中读取,然后写入到另外一个文件中。






0 0