凡是与输入、输出相关的类、接口都定义在 java.io 包下
java.io.File 类
1、File 是一个类,可以有构造器创建其对象。此对象对应着一个文件或者一个目录。
2、File 中的类,仅涉及到如何查找、创建、删除、重命名文件(.txt .jpg .java)或者目录 (io\\io1)。如果涉及到文件里的内容的,File 是无能为力的,只能用 io 流。
3、File 类的对象常作为 io 流的具体类的构造器的形参。
4、mkdir 和 mkdirs:都是创建文件目录,但是 mkdirs 在没有上层目录的时候把上层所有缺失的目录都创建了,而 mkdir 则不行。
- 1 //绝对路径(文件)
- 2File file1 =newFile("d:\\io\\hello.txt");
- 3 //相对路径(文件)
- 4File file2 =newFile("hello.txt");
- 5 //绝对路径(目录)
- 6File file3 =newFile("d:\\io\\io1");
FileInputStream 和 FileOutputStream(字节流)
1、FileInputStream:从硬盘中读取一个文件到程序的内存中。
- 1 //①先声明一个file类,如果"hello.txt"文件不存在,则会抛出FileNotFoundException异常
- 2 File file = new File("hello.txt");
- 3 //②创建一个FileInputStream类的对象
- 4 FileInputStream fis = new FileInputStream(file);
- 5 //③调用FileInputStream的方法,实现file文件的读取
- 6 int b = fis.read(); //read():读取下一个字节,执行到结尾返回-1
- 7
- while (b != -1) {
- 8System.out.print((char) b);
- 9 fis.read();
- 10
- }
- 11 //④关系相应的类,需要显示的关闭资源
- 12 fis.close();
上面再读取文件时候是一个字节一个字节的读取,这是不现实的,现实中我们肯定要一批一批的读取。这时我们就要用到 read 的重载方法 read(byte[]) 刚才的 b 的就变成了每次读入到 byte 中字节的长度,刚才③中的代码改成如下代码:
- 1 byte[] b = new byte[5]; //读取到的数据要写入的数据
- 2 int len; // 每次读入到byte中的字节的长度
- 3
- while ((len = fis.read(b)) = -1) {
- 4
- for (int i = 0; i < len; i++) {
- 5system.out.print( (char) b[i]);
- 6
- }
- 7
- }
输出结果:abcdefg
如果把上面代码 len 修改成 b.length,结果呢 :输出结果:abcdefgcde
为什么呢?当第一次调用 fis.read(b) 的时候,b 数组的 5 个元素就变成了 b[0]= a, b[1] = b,b[2] = c, b[3] = d,b[4] = e,当第二次调用 read 的时候,b[0] = f,b[1] = g,但是后面没值了所以没变化了。
2、FileOutputStream:从内存中读取一个文件存储到磁盘中。
- 1 // ①先声明一个file类,表明要写入的文件位置,输出的文件可以不存在,如果不存在则会自动创建
- 2File file =newFile("hello1.txt");
- 3 //②创建一个FileOutputStream类的对象
- 4FileOutputStream fos =new FileOutputStream(file);
- 5 //③写入的操作
- 6fos.write(newString("i love my family").getBytes());
- 7 //④关闭输出流
- 8fos.close();
FileReader 和 FileWriter(字符流)FileInputStream 和 FileOutputStream 上面两个会了,这两个基本都一样的写法把 byte[] 的 b 换成 char[] 就行了(字节换字符),其他的什么都不变
优点:可以读取到中文缺点:只能处理文本文件,不能处理图片、视频等文件
BufferedInputStream 和 BufferedOutputStream 两个字节缓冲流(默认缓冲区大小是 8M),可以提升读取文件速度 BUfferedOutputStream write 完之后记着要 flush 刷新一下
复制文件代码:
- 1 public static void main(String[] args) {
- 2 3 long time = System.currentTimeMillis();
- 4 5 File source = new File("E:\\test\\a.txt");
- 6 File target = new File("E:\\copy");
- 7 FileInputStream fis = null;
- 8 FileOutputStream fos = null;
- 9 BufferedInputStream bis = null;
- 10 BufferedOutputStream bos = null;
- 11 12 byte[] ch = new byte[1024];
- 13 int len = 0;
- 14 15
- try {
- 16 //创建流对象
- 17 fis = new FileInputStream(source);
- 18 //创建缓冲流
- 19 bis = new BufferedInputStream(fis);
- 20
- if (!target.exists()) {
- 21 target.mkdirs();
- 22
- }
- 23 fos = new FileOutputStream(new File(target, source.getName()));
- 24 bos = new BufferedOutputStream(fos);
- 25
- while ((len = bis.read(ch)) != -1) {
- 26 bos.write(ch, 0, len);
- 27
- }
- 28 //刷新缓冲区
- 29 bos.flush();
- 30 31
- } catch(IOException e) {
- 32 e.printStackTrace();
- 33
- } finally {
- 34 StreamUtils.closeInput(fis);
- 35 StreamUtils.closeInput(bis);
- 36 37 StreamUtils.closeOutput(fos);
- 38 StreamUtils.closeOutput(bos);
- 39
- }
- 40 41 int costTime = (int)(System.currentTimeMillis() - time);
- 42 System.out.println("使用BIS和BOS的时间为:" + costTime + "ms");
- 43 44
- }
BufferedReader 和 BufferedWriter 两个字符缓冲流,可以提升文件读取速度(纯文本文件)
BufferedReader 和 BufferedWriter 类各拥有 8192 字符的缓冲区。当 BufferedReader 在读取文本文件时,会先尽量从文件中读入字符数据并置入缓冲区,而之后若使用 read() 方法,会先从缓冲区中进行读取。如果缓冲区数据不足,才会再从文件中读取,使用 BufferedWriter 时,写入的数据并不会先输出到目的地,而是先存储至缓冲区中。如果缓冲区中的数据满了,才会一次对目的地进行写出。
word 文件只能用字节流打开
例子:使用字符处理流实现文件复制
- importjava.io.*;
- class IODemo
- {
- public static void main(String[] args)
- {
- try
- {
- //使用BufferedReader和BufferedWriter进行文件复制(操作的是字符,以行为单位读入字符) FileReader fr=newFileReader("a.txt");
- BufferedReader br=new BufferedReader(fr);
- FileWriter fw=newFileWriter("d.txt");
- BufferedWriter bw=new BufferedWriter(fw);
- String s=br.readLine();
- while(null!=s)
- {
- bw.write(s);
- //由于BufferedReader的rendLIne()是不读入换行符的,所以写入换行时须用newLine()方法
- bw.newLine();
- //read=fis.read(b); s=br.readLine();
- }
- br.close();
- bw.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- }
InputStreamReader 和 OutputStreamWriter 转换流编码:字符串 ---》字节数组解码:字节数组 ----》字符串
- 1 public static void main(String[] args) throws IOException {
- 2 BufferedReader br = null;
- 3 BufferedWriter bw = null;
- 4
- try {
- 5 //解码
- 6 File file = new File("dbcp.txt");
- 7 FileInputStream fis = new FileInputStream(file);
- 8 InputStreamReader isr = new InputStreamReader(fis, "GBK");
- 9 br = new BufferedReader(isr);
- 10 //编码
- 11 File file1 = new File("dbcp1.txt");
- 12 FileOutputStream fos = new FileOutputStream(file1);
- 13 OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
- 14 bw = new BufferedWriter(osw);
- 15 String str;
- 16
- while ((str = br.readLine()) != null) {
- 17 bw.write(str);
- 18 bw.newLine();
- 19 bw.flush();
- 20
- }
- 21
- }
- 22
- catch(IOException e) {
- 23 e.printStackTrace();
- 24
- }
- 25
- finally {
- 26 bw.close();
- 27 br.close();
- 28
- }
- 29
- }
DataInputStream 和 DataOutputSteam 数据流为了方便的操作 java 语言的基本数据类型的数据,可以使用数据流
ObjectInputStream 和 ObjectOutputstream 对象流用于存储和读取对象的处理流,它可以把对象写入到数据源中,也能把对象从数据源中还原回来。其实就是序列化和反序列化① ObjectInputStream 与 ObjectOutputStream 类所读写的对象必须实现 Serializable 接口,对象中 transient 和 static 类型成员变量不会被读取和写入②
- 1 import java.io. * ;
- 2 public class serializtion {
- 3 4 public static void main(String[] args) throws IOException {
- 5Student s1 = new Student("张三", 1, 15, "化学");
- 6Student s2 = new Student("李四", 2, 19, "生物");
- 7 8FileOutputStream fout = new FileOutputStream("student.txt");
- 9ObjectOutputStream out = new ObjectOutputStream(fout);
- 10out.writeObject(s1);
- 11out.writeObject(s2);
- 12out.close();
- 13FileInputStream fin = new FileInputStream("student.txt");
- 14ObjectInputStream in =new ObjectInputStream(fin);
- 15
- try {
- 16s1 = (Student) in .readObject();
- 17s2 = (Student) in .readObject();
- 18
- } catch(ClassNotFoundException e) {
- 19 // TODO Auto-generated catch block
- 20e.printStackTrace();
- 21
- }
- 22 in .close();
- 23System.out.print("name:" + s1.name);
- 24System.out.print(" id:" + s1.id);
- 25System.out.print(" age:" + s1.age);
- 26System.out.println(" department:" + s1.department);
- 27System.out.print("name:" + s2.name);
- 28System.out.print(" id:" + s2.id);
- 29System.out.print(" age:" + s2.age);
- 30System.out.println(" department:" + s2.department);
- 31
- }
- 32
- }
- 33 import java.io. * ;
- 34 public class Student implements Serializable {
- 35String name;
- 36int id;
- 37int age;
- 38String department;
- 39public Student(String name, int id, int age, String department) {
- 40this.age = age;
- 41this.department = department;
- 42this.id = id;
- 43this.name = name;
- 44
- }
- 45
- }
RandomAccessFile 随机存取文件流 √ 既可以充当一个输入流,又充当一个输出流√ 支持从文件的开头读取、写入。若文件不存在则创建,否则覆盖√ 支持任意的位置的 "插入"
来源: http://www.cnblogs.com/hzg110/p/6687331.html