IO流

IO流的概述

操作文件时一定要关闭文件,file.close() ,写入文件最好对文件进行刷新 file.flush();采用自动关闭流的方法较好
IO流的主要作用是用来处理设备之间的数据传输,例如可以使用IO流将一台电脑硬盘里面的照片传输到另一台电脑里面,即将照片转换为字节,然后将字节传到另一台电脑上面,另一台电脑接收后,可以将这些字节还原为照片。

IO的分类

按照流向分类:
				        输入流(InPutStream和Reader):从硬盘或者别的地方读到内存里面
				        输出流(OutPutStream和Writer):从内存向硬盘或者别的地方输出
按照操作类型分类:
					    字节流(InPutStream和OutPutStream):字节流可以操作任何数据类型,因为在计算机中的数据都是以字节的形式存储的。
					    字符流(Reader和Writer):字符流只能操作纯字符数据,防止乱码

InputStream,OutPutStream,Reader和Writer都是抽象类!

IO流

使用FileInputStream和FileOutputStream读取和写入文件

操作结束之后注意关闭流释放资源,这里相当于在内存和硬盘之间放一个*,在将硬盘中的文件全部读取到内存之后,需要拿掉*,否则会浪费资源

FileInputStream:
								构造方法: FileInputStream(String name)
								对象方法:
												close()  :关闭资源
												
												read() :返回int,当读完返回-1,Reads a byte of data from this input stream.
												
												read(byte[] b): 返回in当读完返回-1t,Reads up to b.length bytes of data  from this input stream into an array of bytes;  
												b:the buffer into which the data is read
												

											   read(byte[] b,int off,int len):返回int,Reads up to len bytes of data from this input stream into an array of bytes; 
											   b:the buffer into which the data is read,off-
												off:the start offset in the destination array b
												len:the maximum number of bytes read




FileOutputStream:
									构造方法:
														FileOutputStream(String name):Creates a file OutPut stream to write to the file with the specifiedname.
													  FileOutputStream(String name,boolean append) :Creats a file output stream to write to the file,append 为true则不覆盖原文内容。

								对象方法:
								·					write(int b):
													write(byte []b):  write b.lengtj bytes from the specified bytes array to this file output stream.
													write(byte[] b ,int off, int len)
package com.chapter8;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Chapter8 {

	public static void main(String[] args) {
		
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			 fis = new FileInputStream("helloword");
			 //如果文件不存在的话,就会自动创建
			 //如果想要在文件中继续写的话,第二个参数传入true
			 fos = new FileOutputStream("123.txt");
			 
			 
			 //read()方法: 效率低
			 int temp =0;
//			 while((temp=fis.read())!=-1){
//				 System.out.println((char)temp);
//			 }
			 
			 //read(byte[] b)方法
			 //定义byte数组告诉系统一次读取几个字节,减少硬盘盒内存之间通信提高效率
			 byte [] b = new byte[5];
			 //有参的read方法返回的int值是读取了几个字节
			 while((temp=fis.read(b))!=-1) {
				 System.out.println(new String(b,0,temp));
			 }
			 
			 
			 //write方法
			 // write(int)
			 fos.write(98);
			  
			 //write(byte[] b) 方法
			 String msg = "monkey1024";
			 //将byte数组中所有的数据全部写入
			 byte[] bytes =msg.getBytes();
			 //将byte数组中的所有数据全部写入
			 fos.write(bytes);
			 //刷新一下确保数全部写入硬盘
			 fos.flush();
			 
			 
		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				fis.close();
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}

文件copy

package com.chapter8;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/*
  将helloword中内容考到 hellowordcopy中,先以流的方式将helloword文件读取到内存中,然后再以流的方式将内存中的内容写出到硬盘里面
 */
public class copyTest {

	public static void main(String[] args) {
		
		try(FileInputStream fis = new FileInputStream("helloword");
			FileOutputStream fos = new FileOutputStream("hellowordcopy.txt");	
			) {
			
			int temp = 0;
			byte[] arr = new byte[1024]; //注意读取的字节数,1024
			while((temp=fis.read(arr))!=-1) {
				fos.write(arr); //写入文件
			}
			fos.flush();
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}

}

使用缓冲流进行文件拷贝

Java中提供了BufferedInputStream和BufferedInputStream缓冲流来读取和写出
BufferedInputStream读取时会创建一个长度为 8192的 byte类型数组,程序一次读取 8192 个字节数据到数组中 使用缓冲流之后 不用再自定义byte类型数组了。

package com.chapter8;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class BufferedCopy {

	public static void main(String[] args) {
		
		try(
				//使用缓冲流装饰一下
				BufferedInputStream bis = new BufferedInputStream(new FileInputStream("helloword"));
				BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("hellowordcopy2.txt"));) {
			
			int temp=0;
			while((temp=bis.read())!=-1) {
				bos.write(temp);
			}
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
	}

}

文件的加密

通过输入流将文件读取到内存里面,可以对这些数据做一些处理,之后再将数据写出到硬盘里面从而达到加密的效果。

package com.chapter8;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class CodeWord {

	public static void main(String[] args) {
		
		try(FileInputStream fis = new FileInputStream("helloword");
			FileOutputStream fos = new FileOutputStream("hellowordcode");	
			) {
			
			int temp = 0;
			while((temp=fis.read())!=-1) {
				//数据异或一个数字进行加密
				fos.write((char)temp^88);
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}		
	}

}

文件解密
上面的加密操作就是在写出数据之前对数据进行异或操作,利用对一个数进行两次异或的结果就是他本身的整个特点,将待解密的word读取到内存里面,然后在进行异或操作即可。u

字符流FileReader

字符流FileReader主要用来读取字符的IO流,使用字符流读取文本 文件可以解决 乱码问题。

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/*
 读取字符: FileReader
 		     Convenience class for reading character files.
 		用法和 FileInputStream 一样
 */

public class FileReaderTest01 {

	public static void main(String[] args) {
		
		try(FileReader fr = new FileReader("小重山");) {
			
			int temp = 0;
			
			//一次读取一个字符
			System.out.println((char)fr.read()); //昨
			while((temp=fr.read())!=-1) {
			   System.out.println((char)temp);}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}

}

使用BufferedReader可以一次读取一行的文字:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class BufferedReaderTEST {
	/*BufferedReader
	 		Reads text from a character-input stream,buffering characters so as to provide for the efficient reading of the
	 		
	 		characters,arrays,and lines.
	 		
	 	对象方法:
	 			readLine()  //按行读取,达到文件末尾返回null
	 * 
	*/
	public static void main(String[] args) {
		
		try(BufferedReader br = new BufferedReader(new FileReader("小重山"));) {
			//读取一个字节
			System.out.println((char)br.read());//昨
			
			//读取一行
			System.out.println(br.readLine()); //夜寒蛩不住鸣。
			
			//利用按行读取,读取全文内容
			String s;
			while ((s=br.readLine())!=null) {
				System.out.println(s); 
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
	}

}

字符流FileWriter

使用字符流FileWriter可以解决写出文本文件中乱码的问题

import java.io.FileWriter;
import java.io.IOException;

public class FileWriterTest {

	public static void main(String[] args) {
		try(FileWriter fw = new FileWriter("中文");) {
			fw.write(97);
			
			fw.write("中文");
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

使用字符流拷贝文件

使用字符流拷贝文本文件可以避免文件中的内容乱码,需要注意的是 ***字符流不能拷贝非文本文件,***比如照片。

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CopyTest01 {

	public static void main(String[] args) {
		
		
		try(BufferedReader br = new BufferedReader(new FileReader("小重山"));
			BufferedWriter bw = new BufferedWriter(new FileWriter("小重山copy"));	
				) {
			
			String s ;
			while((s=br.readLine())!=null) {
			    bw.write(s);
			    bw.newLine(); //重写 一行
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

}

Java装饰者设计模式

设计模式简介
设计模式是可以重复利用的解决方案,软件开发的先驱者或前辈们将之前开发中遇到的问题进行总结并给出了解决方案,后辈在遇到这些问题之后直接使用这些方案即可解决问题。

装饰者设计模式

使用场景:
给某个对象的功能进行扩展时,可以考虑使用装饰者模式。

在IO流这部分内容中,JDK源码使用了大量的装饰者设计模式。比如BufferedReader可以对FileReader进行装饰。

装饰者设计模式的优点:
不用修改被装饰对象的源码,装饰者与被装饰者耦合度不高。

以去拉面管吃拉面为例,一碗面做好后都会向里面加料装饰,所以先定义一个装饰拉面的接口,提供一个加料的方法:

package decorator;

/*
 * 
 * 装饰拉面
 */
public interface NoodleDecorate {
	
	//向拉面中加料
	void addThings();
}

创建一个拉面(Noodle)类实现NoodleDecorate接口:

package decorator;

public class Noodle implements NoodleDecorate {

	@Override
	//制作一碗普通拉面
	public void addThings() {
		System.out.println("加两块肉末");
		System.out.println("加两块白菜");
		System.out.println("加三根香菜");	
	}
}

普通的拉面做好之后,有人爱吃辣椒,想在拉面中添加辣椒,创建一个辣椒拉面(ChiliNoodle)类:

package decorator;

public class ChiliNoodle implements NoodleDecorate {
	
	//普通拉面
	private Noodle noodle;
	
	//传入要加辣椒的普通拉面对象,先做一碗拉面然后加入带辣椒的材料
	public ChiliNoodle(Noodle noodle) {
		super();
		this.noodle = noodle;
	}


	@Override
	//对普通拉面进行升级,加入辣椒
	public void addThings() {
		noodle.addThings();
		System.out.println("加三少辣椒");

	}

}

测试一下:

package decorator;

public class NoodleTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Noodle noodle = new Noodle();
		noodle.addThings();
		
		
		//装饰者模式
		ChiliNoodle cn = new ChiliNoodle(new Noodle());
		cn.addThings();
	}

}

在实际生活中你去拉面馆点了一碗拉面,做好之后如果 你爱吃辣椒的话可以向里面加点辣椒,而不是 让厨师重新做一碗辣椒面。

File类简介

File类在Java.io包下,看名字应该可以猜到,这个类跟文件夹的操作有关

构造方法:      File(String pathname)  //创造文件名,或得对象
 
 对象方法: 
 						createNewFile()      //创建空的文件夹或文件,假如已经存在则不创建

						delete()     //删除文件或文件夹
						exists()     //Tests whether the file or directory exits
						getAbsoluteFile()   //获取绝对路径
						isDirectory()  //判断是否为文件夹
						isFile()    //判断是否为文件
						lastModified()  //返回最后修改时间;毫秒值
						length()  //Returns the length of the file denoted by this abstract pathname;获取文件大小。字节数
						list()     //获取文件路径下的所有文件名(同一层级目录)
						list(FilenameFilter filter)  //Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.
						listFiles()  :获取文件路径下的所有File对象数组
						mkdir()
						mkdirs()
					

文件过滤器

package com.serilizable;
/*
 * 问题:统计根目录下以.txt结尾的文件数量,并将文件名打印出来
 */
import java.io.File;

public class ObjectInputTEST {
	public static void main(String[] args) {
		
		//methond1();
		methond2();

		
	}

	//去除根目录干扰,较好(关键如何去除目录文件夹的影响)
	private static void methond2() {
		File file1 = new File("D:\\java\\code\\Chapter08");
		File[] fi = file1.listFiles();
		for(File f:fi) {
			String s = f.toString();     //将File类型转换为String类型
			if(s.endsWith(".txt")&&f.isFile()) { //判断其是否以.txt结尾,并判断其是否为文件(去除文件夹的影响)
				System.out.println(s);
			}
		}
	}

	//会将根目录下以.txt结尾的文件夹也统计出来,严格说来不正确
	@Deprecated
	private static void methond1() {
		File file1 = new File("D:\\java\\code\\Chapter08");
		String[] st = file1.list();
		for(String s:st) {
			if(s.endsWith(".txt")) {
				System.out.println(s);
			}
		}
	}
	
	
	
	
	
}