Java IO综合练习

字节流:

-in : 
	  -InputStream
	  -FileInputStream
	  -BufferedInputStream
	  -new BufferedOutputStream(new FileInputStream("文件路径"))
-out:
	  -OutputStream
	  -FileReader
	  -BufferedReader
	  -new BufferedOutputStream(new FileOutputStream("文件路径"))

字符流

-in:
	 -Reader
	 -FileReader
	 -BufferedReader
	 -new BufferedReader(new FileReader("文件路径"))
-out:
	 -Writer
	 -FileWriter
	 -BufferedWriter
	 -new BufferedWriter(new FileWriter("文件路径"))

测试:

  • 文件复制
    //这个类用来测试文件复制
    		public class Test5_Copy {
    			public static void main(String[] args) {
    				// 读取1号文件,写出到2号文件中
    				System.out.println("请输入源文件的路径:");
    				String f = new Scanner(System.in).nextLine();
    				File from = new File(f);// 封装了源文件
    
    				System.out.println("请输入目标文件的路径:");
    				String t = new Scanner(System.in).nextLine();
    				File to = new File(t);// 封装了目标文件
    
    				// copyzf(from, to);// 完成文件复制--字符流
    				copyzj(from, to);// 完成文件复制--字节流
    			}
    
    			private static void copyzj(File from, File to) {
    				try {
    					// 1、读取from文件--字节流
    					InputStream in = new BufferedInputStream(new FileInputStream(from));
    					// 2、写出到to文件中
    					OutputStream out = new BufferedOutputStream(new FileOutputStream(to));
    					
    					// 2.5、边读边写
    					int b = 0;// 定义变量,记录读到的数据
    					while ((b = in.read()) != -1) {// 只要读到的不是-1就一直读
    						out.write(b);
    					}
    					// 3、释放资源
    					out.close();
    					in.close();
    					System.out.println("文件复制完成!!");
    				} catch (IOException e) {
    					System.out.println("文件复制失败!!");
    					e.printStackTrace();// 调试阶段出现,上线时根本不存在这行代码
    				}
    
    			}
    			// 到底用字节流还是用字符流呢??--根据你要复制的文件类型决定
    			private static void copyzf(File from, File to) {
    				try {
    					// 1、读取from文件--字符流--只能操作字符文件
    					Reader in = new BufferedReader(new FileReader(from));
    					// 2、写出到to文件中
    					Writer out = new BufferedWriter(new FileWriter(to));
    					// 2.5、边读边写
    					int b = 0;// 定义变量,记录读到的数据
    					while ((b = in.read()) != -1) {// 只要读到的不是-1就一直读
    						out.write(b);
    					}
    					// 3、释放资源
    					out.close();
    					in.close();
    					System.out.println("文件复制完成!!");
    				} catch (IOException e) {
    					System.out.println("文件复制失败!!");
    					e.printStackTrace();// 调试阶段出现,上线时根本不存在这行代码
    				}
    			}
    		}
    
  • 批量读写
    //这个类用来测试批量读写
    	public class Test6_Batch {
    		public static void main(String[] args) {
    			try {
    				// 1、准备读取文件,写出数据
    				File from = new File("D:\\iotest\\1.txt");
    				File to = new File("D:\\iotest\\2.txt");
    
    				// 2、创建读取流读取from文件--字节流
    				InputStream in = new FileInputStream(from);
    				// 创建写出流写出到to文件
    				OutputStream out = new FileOutputStream(to);
    				
    				//3、边读边写
    				int b = 0;  //定义变量,记录读到的数据
    				
    				//read()是一个字节一个字节的读取
    				//read(byte[] bs)是一个字节数组的读取
    				byte[] bs = new byte[10];//可以参考源码中的数组容量,8*1024字节,相当于8K
    				while(  ( b = in.read(bs) )	!=		-1	) {
    					
    					//write()是一个字节一个字节的写出
    					//write(byte[] bs)是一个字节数组的写出
    					out.write(bs);
    				}
    				//4、释放资源
    				in.close();
    				out.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值