在从File类开始IO系统介绍一文中,我们详细的介绍了File类。这个类非常有用,我们可以用它作桥梁把文件和流轻松的联系起来。在Java IO专题中,我准备先介绍一些实用的关于Java IO编程方法,而不是先从整体来把握IO,因为我觉得那样效果并不好。当我们解决了这些平时开发中涉及到的问题后,再总结一下Java的IO系统。
当我们要对文件进行操作的时候,我们首先要确定我们对什么样的文件进行操作,是二进制的文件例如图片还是字符类型的文本文件,这非常的重要。当我们对二进制的文件处理的时候,我们应该使用FileInputStream和FileOutputStream,对文本文件的处理将在后面的文章讲述。
当我们操作文件的时候,可以首先使用File类得到对这个文件的引用,例如 File file = new File("Idea.jpg");然后把file作为参数传给FileInputStream或者FileOutputStream得到相应的输入流或者输出流。通常我们对文件无非是进行读写,修改,删除等操作。最重要的就是读写操作。当我们读文件的时候应该使用InputStream,写文件的时候使用OutputStream。read()方法是在InputStream中定义的,它是个抽象方法。InputStream当然也是个抽象类,我们得到的实例都是它的子类,例如FileInputStream,子类如果不是抽象类的话就要实现父类的抽象方法。在FileInputStream中不但实现了read()并且重载了这个方法提供了read(byte[] buffer)和read(byte[] buffer,int off,int length)两个方法。下面详细介绍一下:
read()方法将读取输入流中的下一个字节,并把它作为返回值。返回值在0-255之间,如果返回为-1那么表示到了文件结尾。用read()我们可以一个一个字节的读取并根据返回值进行判断处理。 while((ch = image.read())!=-1) { System.out.print(ch); newFile.write(ch); }
read(byte[] buffer)会把流中一定长度的字节读入buffer中,返回值为实际读入buffer的字节长度,如果返回-1表示已经到了流的末尾。 while((ch = image.read(buffer))!=-1) { System.out.println(ch); newFile.write(buffer); }
read(byte[] buffer,int off,int length)的意思是把流内length长度的字节写入以off为偏移量的buffer内,例如off=7,length=100的情况下,这个方法会从流中读100个字节放到buffer[7]到buffer[106]内。返回值为实际写入buffer的字节长度。 while((ch = image.read(buffer,10,500))!=-1) { System.out.println(ch); newFile.write(buffer,10,500);
}
对上面的方法进行介绍的时候我们没有考虑异常的情况,读者应该参考API doc进行必要的了解。当我们对流操作的时候,有的时候我们可以对流进行标记和重置的操作,当然要流支持这样的操作。参考一下mark(),reset()和markSupported()方法的说明。最后在使用结束后,确保关闭流,调用close()方法。由于FileOutputStream的write相关的方法和FileInptutStream的read()非常类似,因此不再多说。下面提供一个例子说明如何对二进制文件进行操作,我们打开一个JPEG格式的文件,通过三种不同的方式读取内容,并生成一个新的文件。运行结束后你会发现这两个文件完全一样! import java.io.*;
public class LinkFile { public static void main(String[] args) throws IOException { linkBinaryFile("Idea.jpg"); } private static void linkBinaryFile(String fileName) throws IOException { File imageFile = new File(fileName); if(!imageFile.exists()&&!imageFile.canRead()) { System.out.println("can not read the image or the image file doesn't exists"); System.exit(1); } long length = imageFile.length(); int ch = 0; System.out.println(length); byte[] buffer = new byte[(int)length/7]; InputStream image = new FileInputStream(imageFile); File file = new File("hello.jpg"); if(!file.exists()) { file.createNewFile(); } FileOutputStream newFile = new FileOutputStream(file,true); boolean go = true; while(go) { System.out.println("please select how to read the file:\n"+ "1: read()\n2:read(byte[] buffer)\n3:read(byte[] buffer,int off,int len)\n"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = br.readLine(); if(line.equals("1")) { while((ch = image.read())!=-1) { System.out.print(ch); newFile.write(ch); } } else if(line.equals("2")) { while((ch = image.read(buffer))!=-1) { System.out.println(ch); newFile.write(buffer); } } else if(line.equals("3")) { while((ch = image.read(buffer,10,500))!=-1) { System.out.println(ch); newFile.write(buffer,10,500);
} for(int i = 0;i<10;i++) { System.out.print(buffer[i]); } } go = false; } image.close(); newFile.close(); } }

|