File IO

File I/O

1.使用File类操作文件或目录属性

1.1File的对象

File对象既可以表示文件,也可以表示目录。

创建一个File对象的语法格式如下:

File file = new File(String path);

path指的是文件(目录)路径。我们继续了解其他构造

File file = new File(String parent,String child)

根据一个目录和一个子文件/目录得到File对象

File file = new File(File parent,String child)

根据一个父File对象和一个子文件得到File对象

1.2File的常用方法

方法名称说明
boolean exists()判断文件或者目录是否存在
boolean isFile()判断是否是文件
boolean isDirectory()判断是否是文件目录
String getPath()获取路径
String getAbsolutePath()获取绝对路径
String getName()获取文件或者目录的名称
boolean createNewFile()创建文件
long length()获取文件的长度,单位为字节,文件不存在,返回OL
boolean delete()删除指定文件、目录
方法名称说明
String getParent()获取上层文件目录路径
long lastModified()获取最后一次修改时间
boolean renameTo(File dest)文件重命名
boolean canRead()判断是否可读
boolean canWrite()判断是否可写
boolean isHidden()判断是否隐藏
boolean mkdir()创建文件目录
boolean mkdirs()创建多个文件目录

File类有许多方法,对于这些方法,大家要稍微了解,也可查看API。

2.Java的流

流:一连串流动的字符,是以先进先出的方式发送和接收数据的通道

按照流的方向:

输入流:只能从中读取数据

输出流:只能向其中写入数据

按照所操作的数据单元不同:

字节流:最先小单元为8个字节

字节输入;字节输出

字符流:最先小单元为16个字符

字符输入;字符输出

2.1四大基类(抽象类abstract)

文件读写步骤:

  1. 导包
  2. 创建流对象
  3. 执行读写操作
  4. 关闭流对象
2.1.1字节输入流InputStream

常用方法:

方法名称说明
int read()读取一个字节数据
int read(byte [] b)将数据读取到字节数组中
int read(byte[] b,int off,int len)从输入流中读取最多len长度的字节,保存到字节数组b中,保存的位置从off开始
void close()关闭输入流
int available()返回输入流读取的估计字节数

InputStream的实现类

(1)FileInputStream

//放File对象
File file =new File("hello.txt");
FileInputStream fis =new FileInputStream(file);
//放路径
FileInputStream fis =new FileInputStream("hello.txt");

(2)DataInputStream

FileInputStream fis =new FileInputStream("hello.txt");
//必须放InputStream对象
DataInputStream dis =new DataInputStream(fis);

优点:操作基本数据类型,保证字节的原样性

2.1.2字节输出流OutputStream

常用方法:

方法名称说明
void write(int c)写入一个字节
void write(byte [] buf)写入数组buf的所有字节
void write(byte [] b,int off,int len)将字节数组中从off位置开始,长度为len的字节数据输出到输出流中
void close()关闭输出流

OutputStream的实现类

(1)FileOutputStream

(2)DataOutputStream

操作与FileInputStream,DataInputStream类似,不再叙述

2.1.3.字符输入流Reader

常用方法:

方法名称说明
int read()从输入流中读取单个字符
int read(byte [] c)从输入流中读取c.length长度的字符,保存到字符数组c中,返回实际读取的字符数
int read(byte [] c,int off ,in len)从输入流中读取最多len长度的字符,保存到字符数组c中,保存的位置从off位置开始,返回实际读取的字符长度
void close()关闭流

Reader的实现类

(1)FileReader

Reader fr =new FileReader("hello.txt");

操作:

Reader fr = null;
StringBuffer sbf = null;
try {
	fr =new FileReader("路径");
	char ch [] =new char[1024];
	sbf =new StringBuffer();
	int length =fr.read(ch);
	while (length!=-1) {
		sbf.append(ch);
		length =fr.read();
	}
}catch(IOException e) {
	e.printStackTrace();
}finally {
	try {
		if(fr!=null) {
			fr.close();  
		}
	 catch (IOException e) {
		e.printStackTrace();
	 }
}
System.out.println(sbf.toString());

(2)BufferedReader

它与FileReader类的区别在于,BufferedReader类带有缓冲区,它可以先把一批数据读到缓冲区,然后从缓冲区内获取数据,避免每次都从数据源读取数据进行字符编码转换,从而提高读取操作效率

着重强调的方法:

readLine() 读取一行数据,返回字符串;

2.1.4.字符输出流Writer

常用方法:

write(String str)*str字符串里包含的字符输出到指定的输出流中
Write(String str,int off ,int len)将str字符串里从off位置开始长度为len的字符输出到输出流中
voud flush()刷新输出流
void close()关闭输出流
方法名称说明

Write的实现类

(1)FileWriter

Writer fw =null;
try {
	fw =new FileWriter("路径");
	fw.write("我热爱我的团队!");
	fw.flush();  ///刷新缓冲区
} catch (IOException e) {
	e.printStackTrace();
}finally{
	try {
		if(fw!=null) {
			fw.close();
		}
    }catch (IOException e) {
		e.printStackTrace();
	}
}

(2)BufferedWriter

BufferedWriter是把一批数据写到缓冲区,当缓冲区满的时候再把缓冲区的数据写到字符输出流中,可以避免每次都执行物理写操作,从而提高效率。

说明:操作类似,不做多余介绍

3.异常

常见异常

IOException 写读异常

FileNotFoundException 文件找不到异常

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值