0
点赞
收藏
分享

微信扫一扫

File类、IO流(部分)

Alex富贵 04-01 11:30 阅读 1
学习java

day34

File类

理解

需求1:

public class Test01 {

	public static void main(String[] args) {
		
		//创建file对象
		File file = new File("D:\\笔记\\奇男子\\qnz.txt");
		
		System.out.println("获取文件路径:" + file.getAbsolutePath());
		System.out.println("获取文件名:" + file.getName());
		System.out.println("获取文件是否可读:" + file.canRead());
		System.out.println("获取文件是否可写:" + file.canWrite());
		System.out.println("获取文件是否隐藏:" + file.isHidden());
		System.out.println("获取文件大小(字节):" + file.length());

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
		String datetime = sdf.format(file.lastModified());
		System.out.println("获取文件最后修改时间:" + datetime);
	}
}

相对路径 和 绝对路径

绝对路径:
相对路径:
public class Test02 {

	public static void main(String[] args) {
		
		File file = new File("file.txt");//使用的是相对路径
		System.out.println(file.length());
		
		//D:\workspace\MyDay33\file.txt
		System.out.println("绝对路径:" + file.getAbsolutePath());
		
		//file.txt
		System.out.println("相对路径:" + file.getPath());
		
	}
}

需求2

1)目录已存在的情况
public class Test01 {
	public static void main(String[] args) throws IOException {
		
		File file = new File("file01\\hhy.txt");
		
		//判断文件是否存在
		if(!file.exists()){
			//创建文件
			file.createNewFile();
		}
	}
}
2)有一个层级的目录不存在的情况
public class Test02 {
	public static void main(String[] args) throws IOException {
		
		//D:\workspace\MyDay34\file01\qnz.txt
		File file = new File("file01\\qnz.txt");
		
		//获取父路径 -- D:\workspace\MyDay34\file01
		File parentFile = file.getParentFile();
		//判断父路径是否存在
		if(!parentFile.exists()){
			//创建一层目录
			parentFile.mkdir();
		}
		
		//判断文件是否存在
		if(!file.exists()){
			//创建文件
			file.createNewFile();
		}
	}
}
3)有多个层级的目录不存在的情况
public class Test03 {
	public static void main(String[] args) throws IOException {
			//D:\workspace\MyDay34\file01\file02\file03\qnz.txt
		File file = new File("file01\\file02\\file03\\qnz.txt");
		
		//获取父路径 -- D:\workspace\MyDay34\file01\file02\file03
		File parentFile = file.getParentFile();
		//判断父路径是否存在
		if(!parentFile.exists()){
			//创建多层目录
			parentFile.mkdirs();
		}
		
		//判断文件是否存在
		if(!file.exists()){
			//创建文件
			file.createNewFile();
		}
	}
}

需求3:

public class Test01 {
	public static void main(String[] args) {
		
		File file = new File("D:\\笔记");
		
		//获取当前目录下所有文件及文件夹的名字
//		String[] list = file.list();
//		for (String fileName : list) {
//			System.out.println(fileName);
//		}
		
		//获取当前目录下所有的file对象
		File[] listFiles = file.listFiles();
		
		//获取当前目录下所有的文件及文件夹的file对象
		for (File f : listFiles) {
			System.out.println(f.getName() + " -- " + f.canRead() + " -- " + f.canWrite());
		}
		
	}
}
1)要求只输出文件后缀名为txt的文件
public class Test02 {
	public static void main(String[] args) {
		
		File file = new File("D:\\笔记");
		
		//获取当前目录下所有文件及文件夹的名字
//		String[] list = file.list();
//		for (String fileName : list) {
//			if(fileName.endsWith(".txt")){
//				System.out.println(fileName);
//			}
//		}
		
		//获取当前目录下所有的file对象
		File[] listFiles = file.listFiles();
		
		//获取当前目录下所有的文件及文件夹的file对象
		for (File f : listFiles) {
			
			String fileName = f.getName();
			
			//判断f是否是文件
			if(f.isFile() && fileName.endsWith(".txt")){
				System.out.println(fileName);
			}
		}
		
	}
}
2)根据API的过滤器来完成该功能
public class Test03 {
	public static void main(String[] args) {
		
		File file = new File("D:\\笔记");
		
		String[] list = file.list(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				
				File childFile = new File(dir, name);
				if(childFile.isFile() && childFile.getName().endsWith(".txt")){
					return true;
				}
				return false;
			}
		});
		
		for (String fileName : list) {
			System.out.println(fileName);
		}
		
	}
}
3)需求继续跟进,列出当前目录及子目录中符合该条件的文件信息(递归)
public class Test04 {
	public static void main(String[] args) {
		
		File file = new File("D:\\笔记");
		
		fileHandler(file, ".txt");
		
	}
	
	public static void fileHandler(File file,String suffix){
		
		File[] listFiles = file.listFiles();
		
		for (File f : listFiles) {
			if(f.isFile()){//文件
				
				String name = f.getName();
				if(name.endsWith(suffix)){
					System.out.println(name);
				}
				
			}else if(f.isDirectory()){//文件夹
				fileHandler(f, suffix);
			}
		}
	}
}

IO流

概念

分类

存储单位

学习注意事项

字节流

利用 文件字节输出流 向文件写入数据

public class Test01 {
	public static void main(String[] args) throws IOException {
		
		//1.创建流对象
		FileOutputStream fos = new FileOutputStream("io.txt");
		
		//2.写入数据
		//fos.write(97);//写入UniCode码
		//fos.write("123abc".getBytes());//写入byte数组
		fos.write("123abc".getBytes(), 2, 3);//写入byte数组、偏移量、写入长度
        //3ab
        
		//3.关闭资源
		fos.close();
		
	}
}
public class Test02 {
	public static void main(String[] args) throws IOException {
		
		//1.创建流对象 + 设置在文件末尾追加
		FileOutputStream fos = new FileOutputStream("io.txt",true);//默认是false
		//2.写入数据
		fos.write("123abc".getBytes());//写入byte数组
		//3.关闭资源
		fos.close();
	}
}
public class Test03 {
	public static void main(String[] args) {
		
		FileOutputStream fos = null;
		try {
			//1.创建流对象 + 设置在文件末尾追加
			fos = new FileOutputStream("io.txt",true);
			
			//2.写入数据
			fos.write("123abc".getBytes());//写入byte数组
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			
			//3.关闭资源
			if(fos != null){
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
			
	}
}

利用 文件字节输入流 读取文件里的数据

public class Test04 {

	public static void main(String[] args) throws IOException {
		
		//1.创建流对象
		FileInputStream fis = new FileInputStream("io.txt");
		
		//2.读取数据
		//read():一个字节一个字节的读取数据,读取到文件末尾返回-1
		int read = fis.read();
		System.out.println(read);
		read = fis.read();
		System.out.println(read);
		read = fis.read();
		System.out.println(read);
		read = fis.read();
		System.out.println(read);
		read = fis.read();
		System.out.println(read);
		read = fis.read();
		System.out.println(read);
		read = fis.read();
		System.out.println(read);
		
		//3.关闭资源
		fis.close();
	}
}
//while循环读取		
		//2.读取数据
		//read():一个字节一个字节的读取数据,读取到文件末尾返回-1
		int read;
		while((read = fis.read()) != -1){
			System.out.println((char)read);
		}	
//指定长度读取
		//2.读取数据
		//read(bs):读取bs长度的数据,并把数据放入数组,返回读取到的有效字节数,如果读取到文件末尾则返回-1
		byte[] bs = new byte[1024];
		int len;
		while((len = fis.read(bs)) != -1){
			System.out.println(new String(bs, 0, len));
		}
public class Test07 {
	public static void main(String[] args){
			
		FileInputStream fis = null;
		try {
			//1.创建流对象
			fis = new FileInputStream("io.txt");
			
			//2.读取数据
			//read(bs):读取bs长度的数据,并把数据放入数组,返回读取到的有效字节数,如果读取到文件末尾则返回-1
			byte[] bs = new byte[1024];
			int len;
			while((len = fis.read(bs)) != -1){
				System.out.println(new String(bs, 0, len));
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			//3.关闭资源
			if(fis != null){
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}	
	}
}

文件拷贝

public class Copy01 {
	public static void main(String[] args) throws IOException {
		
		FileInputStream fis = new FileInputStream("奇男子.mp4");
		FileOutputStream fos = new FileOutputStream("copy.mp4");
		
		int read;
		while((read = fis.read()) != -1){
			fos.write(read);
		}
		
		fis.close();
		fos.close();
	}
}
		byte[] bs = new byte[1024];
		int len;
		while((len = fis.read(bs)) != -1){
			fos.write(bs, 0, len);
		}
public class Copy03 {
	public static void main(String[] args){
		
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream("奇男子.mp4");
			fos = new FileOutputStream("copy.mp4");
			
			byte[] bs = new byte[1024];
			int len;
			while((len = fis.read(bs)) != -1){
				fos.write(bs, 0, len);
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			//分开关闭,不然会出问题if(fis != null && fos != null)导致无法关闭
			if(fis != null){
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			if(fos != null){
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
public class Copy04 {
	public static void main(String[] args){
		
		//注意:小括号里创建的流会在try...catch后自动关闭
		try(FileInputStream fis = new FileInputStream("奇男子.mp4");
				FileOutputStream fos = new FileOutputStream("copy.mp4");) {
			
			byte[] bs = new byte[1024];
			int len;
			while((len = fis.read(bs)) != -1){
				fos.write(bs, 0, len);
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

字符流

各种流

总结

举报

相关推荐

0 条评论