2020-08-23

File类:
代表与平台无关的文件和目录
File能新建创建文件和目录,但file文件不能访问文件内容本身
如果需要访问文件内容本身,则需要使用输入输出流
//使用绝对路径
File f =new File(“F:\filetest\test1.txt”);
//判断文件是否存在
System.out.println(f.exists());
//访问文件名称
String name =f.getName();
System.out.println(name);
//重命名
boolean flag =f.renameTo(new File(“F:\filetest\test1.txt”));
System.out.println(flag);
//是否有权限读写
boolean a =f.canRead();
boolean b =f.canWrite();
System.out.println(a);
System.out.println(b);

	//判断是否为目录
	File file3 =new File("F:\\filetest");
	try {
		file3.createNewFile();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}   //创建文件
	System.out.println("是否目录"+file3.isDirectory());
	System.out.println(f.isDirectory());

//获取修改时间
long time =f.lastModified();

	//获取目录所有文件列表
	String[] dirlist = file3.list();
	for(String i:dirlist){
		
		System.out.println(new File("F:\\filetest\\"+i).isDirectory());
	}

Io流 stream

Java中对于数据处理,以输入输出流的方式进行,按照类型可分为字符,字节流
源文件–————-》程序(内存)————》目标文件
输入流 输出流
//从硬盘设备,读取文件到程序
//f:\filetest ->打印并输出
//input输入流
//节点流,字节输入流
//节点流还是处理流,节点流时从一个特定的设备都写的数据流,也叫文件流(访问文件相关)
// 处理流:对已经存在的流进行封装链接

public static void testFileIo() throws Exception{
	//一。读取文件并打印的过程
	//1.先将文件映射到类中,先创建文件对象
	File file =new File("F:\\filetest\\test1.txt");
	
	//2.搭建一个通道(fileInputStream)
	FileInputStream input = null;
	try {
		input = new FileInputStream(file);
		
		
		//3.通过通道传输字节信息   字节转换方式

// int b =input.read();

		int b =0;
		while((b =input.read())!=-1){
			
			System.out.println((char)b);
		}
		
		
		
		
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		throw new myException();
	}finally{
		//4.关闭通道end
		try {
			input.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

文件复制
public void filecopy(){
FileInputStream input =null;
FileOutputStream output =null;
try {
//1.定义读写文件
File file1 =new File("");
File file2 =new File("");

		//2.定义输入输出流
		input =new FileInputStream(file1);
		output = new FileOutputStream(file2);
		
		//实现文件复制
		byte[] buf =new byte[20];
		int len;
		try {
			while((len =input.read(buf))!=-1){
				output.write(buf,0,len);
				
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally {
		try {
			input.close();
			output.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}

文件写入
FileOutputStream output =null;
File file= new File(“F:\filetest\outtest.txt”);
try {
output =new FileOutputStream(file);

			//内存
			String string ="hello world";
			
			//写入文件
			try {
				output.write(string.getBytes());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				output.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

处理流
处理流(缓冲流)可以提高文件的操作效率
Bufferedinputstream 处理字节的输入流
Bufferedoutputstream 处理字节的输出流

Bufferedreader 处理字符的输入流
Bufferedwriter 处理文件的输出流
流不可逆
用缓冲流和输入流对接
缓冲流读取数据需要刷新 b.flush()

程序中打开的io文件不属于内存中的资源,垃圾回收机制无法回收该资源,所以应该显示的手动关闭流

Reader 字符流
fileReader
FileWriter

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值