JAVA的高级特性——File类以及IO流

File类

概述:

1.File:翻译是文件,用于表达java中的路径名。

2.路径:用于表示文件或者文件夹在当前系统中的位置

3.路径的分类:

①.绝对路径,没有任何的歧义,也没有任何的前提条件,Windows,从盘符开始的路径,例如"D:/a/b/c/d/a.txt"

②.相对路径,相对于某个文件的路径。

File file = new File( String pathname );
file.mkdir()创建文件夹,如果父级路径不存在,则文件夹创建失败
file.mkdirs()创建文件夹,如果父级路径不存在,则自动创建父级路径,再创建子级路径

File类常用方法

说 明方法名称
判断文件或目录是否存在boolean exists( )
判断是否是文件boolean isFile( )
判断是否是目录boolean isDirectory( )
返回此对象表示的文件的相对路径名String getPath( )
返回此对象表示的文件的绝对路径名String getAbsolutePath( )
返回此对象表示的文件或目录的名称String getName( )
删除此对象指定的文件或目录boolean delete( )
创建名称的空文件,不创建文件夹boolean createNewFile( )
返回文件的长度,单位为字节, 如果文件不存在,则返回 0Llong length()
	File file = new File("test.txt");
		if (file.exists()) {
			file.delete();
			System.out.println("删除成功");
		} else {
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("创建成功");
		}
	}
        File file1 = new File("G:\\test.txt");
		File file2 = new File("test.txt");

		System.out.println(file1.isFile());
		System.out.println(file1.isDirectory());
		System.out.println(file1.getPath());
		System.out.println(file1.getAbsolutePath());
		System.out.println(file1.getName());
		System.out.println(file1.length());

		System.out.println("=============");

		System.out.println(file2.isFile());
		System.out.println(file2.isDirectory());
		System.out.println(file2.getPath());
		System.out.println(file2.getAbsolutePath());
		System.out.println(file2.getName());
		System.out.println(file2.length());

在这里插入图片描述
构造方法:

1.File(String path)

将一个字符串描述的路径,封装成一个 File对象。

2.File(String parent,String Child)

将两个字符串(父级路径, 子级路径),拼接之后形成的路径封装成一个File对象。

3.File(File parent, String child)

将File类型的父级路径和String类型的字节路径拼接成一个新路径,封装成File对象

注意:

创建好File对象后,只是封装了一个路径, 和磁盘上是否有这个路径无关。

通过IO流来读写文件

流是指一连串流动的字符,是以先进先出方式发送信息的通道

流向目的地的数据流

按照流向分: 输入流 VS 输出流

  1. 输出流 OutputStream和Write作为基类
  2. 输入流 InputStream和Reader作为基类

按照单位分: 字节流VS 字符流
1.字节流
① 字节输入流 InputStream基类

② 字节输出流 OutputStream基类

2.字符流
① 字符输入流Reader基类

② 字符输出流Write基类

字节流 读取 MP4、MP3、MP5、JPG、PNG
字符流 读取txt等文本文档

字节流是 8 位通用字节流,字符流是 16 位 Unicode 字符流

按照角色分: 缓冲流 VS 处理流

处理流缓冲流
FileInputStreamBufferedInputStream
FileOutputStreamBufferedOutputStream
FileReaderBufferedReader
FileWriterBufferedWriter
流的基类
InputStream
OutputStream
Reader
Writer

使用FileInputStream 读文本文件

	// 1.创建File
		File file = new File("test.txt");

		// 2.创建合适的流
		FileInputStream fileInputStream = null;
		try {
			fileInputStream = new FileInputStream(file);
			//3.读操作
			//调用FileInputStream类中的read()方法,一个一个字节的去读内容
			//直到读取到最后一个内容的时候 返回 -1
			
			//优化前
			/*
			while(read!=-1){
				for (int i = 0; i < read; i++) {
					System.out.print((char)by[i]);
				}
				read = fileInputStream.read(by);
			}*/
			
			//优化后
			//创建字节数组
			byte [] by = new byte[50];
			int read;
			while((read = fileInputStream.read(by))!=-1){
				String str = new String(by, 0, read);
				System.out.print(str);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				//4.关闭流
				fileInputStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

1.创建File

File file = new File("test.txt");

2.创建合适的流

FileInputStream fileInputStream= new FileInputStream(file);

3.读操作
调用FileInputStream类中的read()方法,一个一个字节的去读内容
直到读取到最后一个内容的时候 返回 -1

优化前

int read = fileInputStream.read();
		while (read != -1) {
			//返回值不为1则循环读取文件内容并打印
			System.out.print((char) read);
			read = fileInputStream.read();
		}

优化过

//创建字节数组
			byte [] by = new byte[50];

            int read;
			while((read = fileInputStream.read(by))!=-1){
				String str = new String(by, 0, read);
				System.out.print(str);
			}

4.关闭流

fileInputStream.close();

使用FileOutputStream 写文本文件


		//1.创建File类
		File file = new File("Hello.txt");
		
		//2.创建相关的流
		FileOutputStream fos=null;
		try {
			fos = new FileOutputStream(file);
			//3.写的相关操作   将字符串转为字节 .getBytes()
			fos.write("Hello Java".getBytes());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				//4.关闭流
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

1.创建File类

File file = new File("Hello.txt");

2.创建合适的流

FileOutputStream fos=new FileOutputStream(file);

3.写的相关操作 将字符串转为字节 .getBytes()

fos.write("Hello Java".getBytes());

4.关闭流

fos.close();

使用FileInputStream、FileOutputStream 复制文本文件


		//1.创建File类
		File file =new File("Hlleo.txt");
		File file2 =new File("HlleoWord.txt");
		//2.创建相关的流
		FileInputStream fis=null;
		FileOutputStream fos = null;
		try {
			//3.读写的相关操作
			 fis = new FileInputStream(file);
			 fos = new FileOutputStream(file2);
			 byte [] byt = new byte[50];
			 int len;
			 while((len=fis.read(byt))!=-1){
				 //把读取的内容写入HelloWord
				 fos.write(byt, 0, len);
			 }
			 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//4.关闭流(先开流的 后关流)
			try {
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			try {
				fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	

1.创建File类

File file =new File("Hlleo.txt");
File file2 =new File("HlleoWord.txt");

2.创建相关的流

FileInputStream  fis = new FileInputStream(file);
FileOutputStream  fos = new FileOutputStream(file2);

3.读写的相关操作

             byte [] byt = new byte[50];
             int len;
			 while((len=fis.read(byt))!=-1){
				 //把读取的内容写入HelloWord文件中
				 fos.write(byt, 0, len);
			 }

4.关闭流 先开流的后关流

fos.close();
fis.close();

使用FileReader、FileWriter复制文本文件


		File file =new File("Hlleo.txt");
		File file2 =new File("Hlleo2.txt");
		FileReader fr=null;
		FileWriter fw=null;
		try {
			 fr = new FileReader(file);
			 fw = new FileWriter(file2);
			 char [] rc = new char[50];
			 int len;
			 while((len=fr.read(rc))!=-1 ){
				 fw.write(rc, 0, len);
			 }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				fw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				fr.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		

1.创建File类

File file = new File("Hlleo.txt");
File file2 = new File("Hlleo2.txt");

2.创建相关的流

FileReader fr = new FileReader(file);
FileWriter fw = new FileWriter(file2);

3.读写的相关操作

             char [] rc = new char[50];
			 int len;
			 while((len=fr.read(rc))!=-1 ){
				 fw.write(rc, 0, len);
			 }

4.关闭流 先开流的后关流

fw.close();
fr.close();

使用BufferedInputStream 、BufferedOutputStream 复制文本文件

	File file =new File(str1);
	File file2 =new File(str2);
	
	FileInputStream  fis = new FileInputStream(file);
	FileOutputStream  fos = new FileOutputStream(file2);
	
	BufferedInputStream bis = new BufferedInputStream(fis);
	BufferedOutputStream bos = new BufferedOutputStream(fos);
	
	byte [] by = new byte[50];
	int len;
	while((len = bis.read(by))!=-1){
		bos.write(by, 0, len);
		bos.flush();
	}
	bos.close();
	bis.close();
	fos.close();
	fis.close();

1.创建File类

File file =new File(str1);
File file2 =new File(str2);

2.创建相关的流

FileInputStream  fis = new FileInputStream(file);
FileOutputStream  fos = new FileOutputStream(file2);
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);

3.读写的相关操作

           byte [] by = new byte[50];
	       int len;
	       while((len = bis.read(by))!=-1){
		     bos.write(by, 0, len);
		     bos.flush();
	       }

4.关闭流 先开流的后关流

    bos.close();
	bis.close();
	fos.close();
	fis.close();

总结
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值