Java-IO流基本操作

IO流

File对象的使用
  1. 创建文件、目录

    file.createNewFile();//创建文件
    file.mkdir();//创建目录,只能创建文件夹
    file.mkdirs();//级联创建文件夹
    
  2. 删除文件、目录

    file.delete();

  3. 查询文件、目录

    file.exists()

  4. 判断是文件、目录

    System.out.println(file.isFile());//判断是不是文件
    System.out.println(file.isDirectory());//判断是不是目录
    
  5. 遍历文件

    //拿到文件的绝对路径
    System.out.println(file.getAbsoluteFile());
    System.out.println(file.getCanonicalPath());
    //剩余空间
    System.out.println(file.getFreeSpace());
    //剩余总空间
    System.out.println(file.getTotalSpace());
    //System.out.println(file.getAbsolutePath());
    //获取文件目录
    System.out.println(file.getParent());
    //获取文件名称
    System.out.println(file.getName());
    //获取父级对象
    File parentFile = file.getParentFile();
    //重命名和移动文件
    file.renameTo(new File())
    //list()会将路径对象的所有文件及文件夹都返回,返回都是字符串
    File f1 = new File("d:\\");
    String[] list = f1.list();
    for(String s: list) {
    System.out.println(s);
    }
    //listFiles()会将路径对象的所有文件及文件夹都返回,返回都是File对象
    File[] listFiles = f1.listFiles();
    for (File f: listFiles) {
    System.out.println(f.getName());
    }
    //指定需要得到文件名称的规则
    String[] list2 = f1.list(new FilenameFilter() {
    					
    @Override
    public boolean accept(File dir, String name) {
    if(name.endsWith("mp4")) {
    	return true;
    } else {
    	return false;
    }
    }
    });
    for(String s: list2) {
    System.out.println(s);
    }
    
  6. 递归遍历磁盘

     import java.io.File;
    import java.io.FilenameFilter;
    
    public class RecurrenceFile {
    
    	public static void main(String[] args) {
    		String s = new String("D:\\work\\");
    		//Dbs(s);
    		Fun(new File(s));
    	}
    	
    	//第一种方法
    	public static void Dbs(String ss) {
    		File file = new File(ss);
    		String[] list = file.list();
    		for(String s: list) {
    			File file1 = new File(ss + s);
    			if(file1.isFile()) {  //判断是不是文件,如果是文件输出文件名,如果是目录递归
    				System.out.println(file1.getAbsolutePath());
    			} else {
    				Dbs(ss + s + "\\");
    			}
    		}
    	}
    	
    	//第二种方法
    	public static void Fun(File file) {
    		File[] list = file.listFiles(new FilenameFilter() {	
    			@Override
    			public boolean accept(File dir, String name) {
                    //判断是不是.java文件
    				if ( name.endsWith("java") || new File(dir,name).isDirectory()) {
    					return true;
    				} else {
    					return false;
    				}
    			}
    		});
    		for(File s: list) { 
    			
    			if(s.isFile()) {//判断是不是文件,如果是文件输出文件名,如果是目录递归
    				System.out.println(s.getAbsolutePath());
    			} else {
    				Fun(s);
    			}
    		}
    	}
    }
    
       
    
IO流的分类,一个简单的FileInputStream的案例,说明流使用的步骤
  1. 流的分类

    • 通过流的方法
      • 输入流 InputStream Reader
      • 输出流 OutputStream Writer
    • 按照功能划分
      • 节点流 直接使用new能够得到对象的流
      • 过滤流 过滤流重要用于装饰节点流
        • 如果过滤流没有关闭,数据都不全
        • 装饰则的设计模式
    • 根据流的数据类型
      • 字节流 InputStream OutputStream
      • 字符流 Reader Writer
    • 转换流
  2. 输入流代码

    package iotest;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    
    public class TestInputStream {
    
    	public static void main(String[] args) {
    		File file = new File("D:\\大学\\2020\\机组\\机组.md");
    		//1. 获取输入流对象
    			InputStream in = null;
    		try {
    			in = new FileInputStream(file);
    			//2. 定义一个字节数组
    			byte[] buf = new byte[1024];
    			//3. 定义一个int类型长度
    			int len = 0;
    			//4. 循环读取
    			while((len = in.read(buf)) > -1) {
    				//使用标准输出流,输出到控制台
    				//System.out.write(buf);
    				System.out.write(buf, 0, len);
    			}
    			
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		} finally {
    			//5. 关闭输入流
    			if (in != null) {
    				try {
    					in.close();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			}
    			
    		}
    	}
    
    }
    
    
文件输入输出流的使用(文件的复制和拷贝)
@Test//测试代码块
		public void test() {
			//定义在外面 好为下面关闭流操作
			FileInputStream fis = null;
			FileOutputStream fos = null;
			
			try {
				//指定对象
				fis = new FileInputStream("D:\\\\下载\\\\图片\\\\截图\\\\两个人在雪山上徒步旅行4k风景壁纸_彼岸图网.jpg");
				fos = new FileOutputStream(new File("d:\\a.jpg"));
				//创建字节数组
				byte[] buffer = new byte[1024];
				int len =0;
				while((len = fis.read(buffer)) > -1) {
					//写入到文件
					fos.write(buffer, 0, len);
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {//最后关闭流
				if (fis != null) {
					try {
						fis.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if (fos != null) {
					try {
						fos.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			
		}
节点流和过滤流,案例拷贝数据
@Test//测试代码块
		public void test() {
		//定义在外面 好为下面关闭流操作
			FileInputStream fis = null;
			FileOutputStream fos = null;
			BufferedInputStream bis = null;
			BufferedOutputStream bos = null;
			
			try {
				fis = new FileInputStream("D:\\\\大学\\\\2020\\\\机组\\\\机组.md");
				bis = new BufferedInputStream(fis);
				fos = new FileOutputStream(new File("d:\\a.md"));
				bos = new BufferedOutputStream(fos);
				
				byte[] buffer = new byte[1024];
				int len =0;
				while((len = bis.read(buffer)) > -1) {
					bos.write(buffer, 0, len);
				}
			//	bos.flush();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
			//如果不关闭流,BufferedOutputStream会有一个缓冲,最后进不去
			//要手动刷新bos.flush();
			//关闭流之前他会自动刷新一遍
				if (bos != null) {
					try {
						bos.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if (bis != null) {
					try {
						bis.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if( fis != null) {
					try {
						fis.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if(fos != null) {
					try {
						fos.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				
			}
			
		}
DateInputStream DateOutputStream演示保存字节对象
@Test//测试代码块
		public void teatData2() {
		//定义在外面 好为下面关闭流操作
			DataOutputStream data = null;
			DataInputStream datain = null;
			try {
				data = new DataOutputStream(new FileOutputStream("d:\\a.txt"));
				datain = new DataInputStream(new FileInputStream("d:\\a.txt"));
				data.writeInt(45654);
				//有可能出现异常
				try {
					System.out.println(datain.readInt());
				} catch (Exception e) {
					System.out.println("数据读取完毕");
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {//关闭流
				if(data != null)
					try {
						data.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
			}
		}
字符流(Reader和Writer)
@Test//测试代码块
		public void test3() {
		//定义在外面 好为下面关闭流操作
			BufferedReader br = null;
			//推荐使用PrintWriter
			PrintWriter out = null;
//			BufferedWriter bw = null;
			try {
				br = new BufferedReader(new FileReader("D:\\work\\JavaWork\\IO\\src\\RecurrenceFile.java"));
				out = new PrintWriter("d:\\a.java");
//				bw = new BufferedWriter(new FileWriter("d:\\a.java"));
				String str = null;
				while((str = br.readLine()) != null) {
//					bw.write(str);
//					bw.newLine();
					out.println(str);
					
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (br != null) {
					try {
						br.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
//				if(bw != null) {
//					try {
//						bw.close();
//					} catch (IOException e) {
//						e.printStackTrace();
//					}
				}
				out.close();//PrintWriter关闭不会出现异常
			}
转换流(InputStringReader和OutoutStreamReader)

转换流的本质是字符流,主要功能就是将字节流转换为字符流

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值