输入和输出(IO)处理(一)

学习阶段存储方式的演变:
**临时存储:**变量 -> 数组 -> 对象 -> 对象数组 -> 集合[最常用的程序内临时存储的顶级存储方式]
**持久化存储:**文件

1. 掌握File类的常用API

**文件的概念:**相关记录或者放在一起的数据的集合。它就可以实现持久化存储。

**java.io.File类:**可以实现对文件的操作。

构造方法

  1. File(String pathname) 直接根据字符串类型地址获取对应的文件对象
  2. File(String parent,String child) 根据字符串类型的父路径和子路径获取对应的文件对象
  3. File(File parent,String child) 根据文件类型的父路径和字符串类型的子路径获取对应的文件对象
// 1.File(String pathname)
// File file = new File("d:/a.txt");
// File file = new File("d:\\a.txt");

// 2.File(String parent父路径,String child子路径)
// 通常应用于:你已经获取到了某个文件的父路径  然后只需要通过本构造即可实现路径自动拼接
// File file = new File("d:", "a.txt");

// 3.File(File parent,String child)
// 通常应用于:你已经获取到了某个文件的父路径(File对象型的)....
File parent = new File("d:");
File file = new File(parent , "a.txt");
// 获取文件的字节数
System.out.println(file.length());

普通方法

  1. 获取文件的字节数:long length()
  2. 判断文件/文件夹是否存在:boolean exists();
  3. 判断是否是文件:boolean isFile();
  4. 判断是否是文件夹:boolean isDirectory();
  5. 获取相对路径:String getPath();
  6. 获取绝对路径:String getAbsolutepath();
  7. 创建文件:boolean createNewFile();
  8. 创建文件夹:boolean mkdir();
  9. 删除文件/空文件夹:boolean delete();
  10. 获取文件名称:String getName();
  11. 获取文件夹的子目录:File[] listFiles();

2. 掌握流的概念和分类

I/O流概念

输入/输出流

**流:**水流 电流 车流 数据流… 流动的概念

方向性:相对于程序为参照物,流是有读取和写入操作,是有方向的。

流的分类

按照方向性分类:

**输入流:**读 InputStream Reader

**输出流:**写 OutputStream Writer

按照处理单元:

**字节流:**可以用来传输一切内容。包括:音频、视频、图片、文本…

字节输入流InputStream 字节输出流OutputStream

**字符流:**可以用来传输文本内容 根据不同国家进行了相应文字转换

字符输入流Reader 字符输出流Writer

3. 掌握使用字节流实现文件复制

字节输入流FileInputStream

构造:

FileInputStream(File file);

FileInputStream(String pathname);

常用方法:

  1. 读取一个字节数据:int read();
  2. 读取一个字节数组长度的字节数据 返回实际读取到的字节数量:int read(byte[] b);
  3. 读取输入流中从指定索引开始,指定长度的字节数据到字节数组中:int read(byte b,int offs,int len);
  4. 关流:void colse();
字节输出流FileOutputStream

构造:

FileOutputStream(File file);

FileOutputStream(String path);

FileOutputStream(File/String file,boolean append); 如果为true 表示追加数据 而不是覆盖数据

常用方法:

  1. void write(int b); 输出一个字节
  2. void write(byte[] b); 输出一个字节数组的内容
  3. void write(byte[] b,int offs,int len); 输出一个字节数组中的指定范围的内容
  4. void close(); 关流

** 文件的复制**

public class Test {

	public static void main(String[] args) throws IOException {
		FileInputStream fis = null;		
		FileOutputStream fos = null;
		try {
			// 1.指定数据源和输出目的地
			fis = new FileInputStream("d:/a.txt");
			fos = new FileOutputStream("c:/a.txt");
			
			// 2.读取数据
			// 临时存储读取到的数据
			byte[] buffer = new byte[1024];
			int len = 0;
			while((len = fis.read(buffer)) != -1) {
				// 3.写入数据  读取到多少写出去多少
				fos.write(buffer,0,len);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			// 4.关流
			// 先开的后关
			if(fos != null) {
				fos.close();
			}
			if(fis != null) {
				fis.close();
			}
		}

	}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值