IO流(File类,IO流的分类,字节流和字符流,转换流,缓冲流,对象序列化)

1.File类

File类可以在程序中 操作文件和目录。File类是通过建立File类对象,在调用File类的对象来进行相关操作的。

示例:

[java] view plain copy
?
  1. public class Demo01 {  
  2.   
  3.     public static void main(String[] args) {  
  4.         File f = new File("f:/我的歌声里.txt");  
  5.         //访问文件名相关  
  6.         String name = f.getName();  
  7.         System.out.println("文件名:" + name);  
  8.         String absolutePath = f.getAbsolutePath();  
  9.         System.out.println("绝对路径:" + absolutePath);  
  10.         String parent = f.getParent();  
  11.         System.out.println("父目录:" + parent);  
  12.           
  13.         //检测相关  
  14.         System.out.println("是否存在:" + f.exists());  
  15.         System.out.println("是否可读" + f.canRead());  
  16.         System.out.println("是否可写:" + f.canWrite());  
  17.           
  18.         //获取文件信息  
  19.         System.out.println("文件的大小: " + f.length());  
  20.           
  21.         //以当前路径创建File对象  
  22.         File file = new File(".");  
  23.         String[] list = file.list();  
  24.         //遍历目录下的文件  
  25.         System.out.println();  
  26.         System.out.println("当前目录下有文件:");  
  27.         for(String name1:list){  
  28.             System.out.println(name1);  
  29.         }  
  30.       
  31.     }  
  32.   
  33. }  
public class Demo01 {
public static void main(String[] args) {
	File f = new File("f:/我的歌声里.txt");
	//访问文件名相关
	String name = f.getName();
	System.out.println("文件名:" + name);
	String absolutePath = f.getAbsolutePath();
	System.out.println("绝对路径:" + absolutePath);
	String parent = f.getParent();
	System.out.println("父目录:" + parent);
	
    //检测相关
	System.out.println("是否存在:" + f.exists());
	System.out.println("是否可读" + f.canRead());
	System.out.println("是否可写:" + f.canWrite());
	
	//获取文件信息
	System.out.println("文件的大小: " + f.length());
	
	//以当前路径创建File对象
	File file = new File(".");
	String[] list = file.list();
	//遍历目录下的文件
	System.out.println();
	System.out.println("当前目录下有文件:");
	for(String name1:list){
		System.out.println(name1);
	}

}

}

运行结果:

IO流(File类,IO流的分类,字节流和字符流,转换流,缓冲流,对象序列化)

2.IO流的分类

按照方向:输入流和输出流

按照流的大小:字节流和字符流

按照流的角色:节点流和处理流

流的类关系图如下:

IO流(File类,IO流的分类,字节流和字符流,转换流,缓冲流,对象序列化)


3.字节流和字符流

字节流:FileInputStream 和 FileOutputStream

示例:把文件复制成另外的文件

[java] view plain copy
?
  1. public class Demo02 {  
  2.   
  3.     public static void main(String[] args) throws IOException {  
  4.         /* 
  5.          * 需求把文件“我的歌声里.txt” 复制并改文件名为 “我的歌声里.java” 
  6.          *  
  7.          */  
  8.     //创建输入流  
  9.      InputStream is = new FileInputStream(new File("f:/我的歌声里.txt"));  
  10.      //创建输出流  
  11.      OutputStream os = new FileOutputStream(new File("f:/我的歌声里.java"));  
  12.      //创建接收字节数组  
  13.      byte[] bytes = new byte[1024];  
  14.      int len = 0;  
  15.      //循环输入输出  
  16.      while((len = is.read(bytes)) != -1 ){  
  17.          os.write(bytes, 0, len);  
  18.      }  
  19.      //关闭资源  
  20.      os.close();  
  21.      is.close();  
  22.   
  23.     }  
  24.   
  25. }  
public class Demo02 {
public static void main(String[] args) throws IOException {
	/*
	 * 需求把文件“我的歌声里.txt” 复制并改文件名为 “我的歌声里.java”
	 * 
	 */
//创建输入流
 InputStream is = new FileInputStream(new File("f:/我的歌声里.txt"));
 //创建输出流
 OutputStream os = new FileOutputStream(new File("f:/我的歌声里.java"));
 //创建接收字节数组
 byte[] bytes = new byte[1024];
 int len = 0;
 //循环输入输出
 while((len = is.read(bytes)) != -1 ){
	 os.write(bytes, 0, len);
 }
 //关闭资源
 os.close();
 is.close();

}

}

运行结果:

IO流(File类,IO流的分类,字节流和字符流,转换流,缓冲流,对象序列化)


字符流:FileReader和FileWriter

示例:切割文件

[java] view plain copy
?
  1. public class Demo03 {  
  2.     public static void main(String[] args) {  
  3.         FileReader r = null;  
  4.         FileWriter w = null;  
  5.         try {  
  6.             int count = 0;//定义一个标记  
  7.             int flag = 0;//文件名标记  
  8.             r = new FileReader("f:/我的歌声里.txt");  
  9.             w = new FileWriter("f:/我的歌声里" + flag +".txt");  
  10.             char[] chars = new char[10];  
  11.             int len = 0;  
  12.             while((len = r.read(chars)) != -1){  
  13.                 System.out.println(new String(chars, 0, len));  
  14.                 w.write(chars, 0, len);  
  15.                 w.flush();  
  16.                 count++;  
  17.                 //定义切割的条件  
  18.                 if(count >10 ){  
  19.                     flag++;  
  20.                     w = new FileWriter("f:/我的歌声里" + flag +".txt");  
  21.                     count = 0;  
  22.                 }  
  23.                   
  24.             }  
  25.               
  26.         } catch (FileNotFoundException e) {  
  27.             e.printStackTrace();  
  28.         } catch (IOException e) {  
  29.             // TODO Auto-generated catch block  
  30.             e.printStackTrace();  
  31.         }finally{  
  32.             try {  
  33.                 w.close();  
  34.                 r.close();  
  35.             } catch (IOException e) {  
  36.                 // TODO Auto-generated catch block  
  37.                 e.printStackTrace();  
  38.             }  
  39.               
  40.         }  
  41.           
  42.   
  43.     }  
  44.   
  45. }  
public class Demo03 {
	public static void main(String[] args) {
		FileReader r = null;
	    FileWriter w = null;
		try {
			int count = 0;//定义一个标记
			int flag = 0;//文件名标记
			r = new FileReader("f:/我的歌声里.txt");
			w = new FileWriter("f:/我的歌声里" + flag +".txt");
			char[] chars = new char[10];
			int len = 0;
			while((len = r.read(chars)) != -1){
				System.out.println(new String(chars, 0, len));
				w.write(chars, 0, len);
				w.flush();
				count++;
				//定义切割的条件
				if(count >10 ){
					flag++;
					w = new FileWriter("f:/我的歌声里" + flag +".txt");
					count = 0;
				}
		}
		
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		try {
			w.close();
			r.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	

}

}





运行结果

IO流(File类,IO流的分类,字节流和字符流,转换流,缓冲流,对象序列化)

4.转换流

转换流:把字节流转换为字符流,一次来实现性能优化

       InputStreamReader 和 OutputStreamWriter

示例:

[java] view plain copy
?
  1. public class Demo04 {  
  2.   
  3.     /** 
  4.      * @param args 
  5.      * @throws IOException  
  6.      */  
  7.     public static void main(String[] args) throws IOException {  
  8.         //键盘输入到文件  
  9.         InputStreamReader isr = new InputStreamReader(System.in);  
  10.         char[] chars = new char[1024];  
  11.         int len = 0;  
  12.         OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("f:/我的文件.txt"));  
  13.         int count = 0;  
  14.         while((len = isr.read(chars)) != -1){  
  15.             osw.write(chars, 0, len);     
  16.             osw.flush();  
  17.             count++;  
  18.             if(count == 10){  
  19.                 break;  
  20.   
  21.             }  
  22.         }  
  23.         isr.close();  
  24.         osw.close();  
  25.           
  26.   
  27.     }  
  28.   
  29. }  
public class Demo04 {
/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
	//键盘输入到文件
	InputStreamReader isr = new InputStreamReader(System.in);
	char[] chars = new char[1024];
	int len = 0;
	OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("f:/我的文件.txt"));
	int count = 0;
	while((len = isr.read(chars)) != -1){
	    osw.write(chars, 0, len);	
	    osw.flush();
	    count++;
	    if(count == 10){
	    	break;

	    }
	}
	isr.close();
	osw.close();
	

}

}

运行结果:

IO流(File类,IO流的分类,字节流和字符流,转换流,缓冲流,对象序列化)


IO流(File类,IO流的分类,字节流和字符流,转换流,缓冲流,对象序列化)

5.缓冲流

        把流读到缓冲区,然后再一次读到内存中来,以此来提高性能

        BuffererInputStream 和BufferedOutputStream

        BufferedReader 和 BufferedWriter

示例:

[java] view plain copy
?
  1. public static void main(String[] args) throws IOException {  
  2.           
  3.         //读取并复制保存图片  
  4.         BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("f:/qq.jpg")));  
  5.         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("f:/qqq.png")));  
  6.           
  7.         String line = null;  
  8.         while((line = br.readLine()) != null){  
  9.             bw.write(line);  
  10.             bw.flush();  
  11.         }  
  12.           
  13.         bw.close();  
  14.         br.close();  
  15.   
  16.     }  
public static void main(String[] args) throws IOException {
	//读取并复制保存图片
	BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("f:/qq.jpg")));
	BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("f:/qqq.png")));
	
	String line = null;
	while((line = br.readLine()) != null){
	    bw.write(line);
	    bw.flush();
	}
	
	bw.close();
	br.close();

}</pre>

运行结果:

IO流(File类,IO流的分类,字节流和字符流,转换流,缓冲流,对象序列化)

6.对象序列化

 对象流:ObjectInStream和 ObjectOutputStream

 Serialiazable关键字:标记接口可序列化

 transient关键字:标记瞬态实例变量

示例:

[java] view plain copy
?
  1. public class Demo06 {  
  2.   
  3.     /** 
  4.      * @param args 
  5.      * @throws IOException  
  6.      * @throws FileNotFoundException  
  7.      * @throws ClassNotFoundException  
  8.      */  
  9.     public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {  
  10.         Student s1 = new Student("小红"19);  
  11.         Student s2 = new Student("小白"18);  
  12.           
  13.         ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("f:/序列化.txt"));  
  14.         oos.writeObject(s1);  
  15.         oos.writeObject(s2);  
  16.           
  17.         s2.setName("小白白");  
  18.         oos.writeObject(s2);//更改变量的属性,即使重新序列化也不会改变原属性值  
  19.         oos.close();  
  20.           
  21.         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("f:/序列化.txt"));  
  22.         Student rs1 = (Student) ois.readObject();  
  23.         Student rs2 = (Student) ois.readObject();  
  24.         System.out.println(rs1);  
  25.         System.out.println(rs2);  
  26.       
  27.    
  28.     }  
  29.   
  30. }  
  31. class Student implements Serializable{  
  32.   
  33.   
  34.       
  35.     private String name;//学生姓名  
  36.     private transient int age;//年龄设置为瞬时变量,将不被序列化  
  37.       
  38.       
  39.     public Student(String name, int age) {  
  40.         super();  
  41.         this.name = name;  
  42.         this.age = age;  
  43.     }  
  44.       
  45.   
  46.   
  47.     public String getName() {  
  48.         return name;  
  49.     }  
  50.   
  51.   
  52.   
  53.     public void setName(String name) {  
  54.         this.name = name;  
  55.     }  
  56.   
  57.   
  58.   
  59.     public int getAge() {  
  60.         return age;  
  61.     }  
  62.   
  63.   
  64.   
  65.     public void setAge(int age) {  
  66.         this.age = age;  
  67.     }  
  68.   
  69.   
  70. @Override  
  71. public String toString() {  
  72.       
  73.     return "该学生的名字为:" + name + ",年龄为:" + age;  
  74. }  
  75. }  
public class Demo06 {
/**
 * @param args
 * @throws IOException 
 * @throws FileNotFoundException 
 * @throws ClassNotFoundException 
 */
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
	Student s1 = new Student("小红", 19);
	Student s2 = new Student("小白", 18);
	
	ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("f:/序列化.txt"));
	oos.writeObject(s1);
	oos.writeObject(s2);
	
	s2.setName("小白白");
	oos.writeObject(s2);//更改变量的属性,即使重新序列化也不会改变原属性值
	oos.close();
	
	ObjectInputStream ois = new ObjectInputStream(new FileInputStream("f:/序列化.txt"));
	Student rs1 = (Student) ois.readObject();
	Student rs2 = (Student) ois.readObject();
	System.out.println(rs1);
	System.out.println(rs2);


}

}
class Student implements Serializable{

private String name;//学生姓名
private transient int age;//年龄设置为瞬时变量,将不被序列化


public Student(String name, int age) {
	super();
	this.name = name;
	this.age = age;
}



public String getName() {
	return name;
}



public void setName(String name) {
	this.name = name;
}



public int getAge() {
	return age;
}



public void setAge(int age) {
	this.age = age;
}

@Override
public String toString() {

return "该学生的名字为:" + name + ",年龄为:" + age;

}
}

运行结果:

IO流(File类,IO流的分类,字节流和字符流,转换流,缓冲流,对象序列化)