您的位置:首页 > 编程语言 > Java开发

java基础之输入流和输出流(FileInputStream,FileOutputStream,FileReader,FileWriter)

2019-06-15 00:00 309 查看

字节流

字节流什么类型的文件都能够操作。

字节输入流 FileInputStream

从系统中的文件中读取文件。若文件路径不对或者文件不存在,系统会报错,找不到文件的异常
构造方法
FileInputStream(File file)
通过打开与实际文件的连接创建一个 FileInputStream ,该文件由文件系统中的 File对象 file命名。
FileInputStream(FileDescriptor fdObj)
创建 FileInputStream通过使用文件描述符 fdObj ,其表示在文件系统中的现有连接到一个实际的文件。
FileInputStream(String name)
通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的路径名 name命名。

代码实现

//这是第一种构造方法的使用,传入File类型的参数
File file = new File("G:\\FileTest\\jiangheng.txt");
try {
//这里可能汇报文件找不到的异常
FileInputStream fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
·
//第二种,直接传入文件的绝对地址路径,String类型的
try {
FileInputStream fis2 = new FileInputStream("G:\\FileTest\\jiangheng.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//以上两种方式是比较常见的构造输入流的方法
主要方法的运用方法
1、读取文件中的内容 read
(1)read() 这个方法每次只能读取一个字节,返回值是int类型的
int code = fis.read();
System.out.println("读取到的字符是:"+(char) code);//读取到的字符是:a

(2)read(byte[] b)这个方法可以将读取的字节存放到数组中。效率比更高

//定义一个字节型的数组,用来存放读取到的内容
byte [] bytes = new byte[5];
int count = fis.read(bytes);
//判断是否读取到文件中的内容
while(count!=-1) {
//将字节型的数组转换成字符串
String str = new String(bytes,0,count);
System.out.print(str);
//继续读取文件
count = fis.read(bytes);
}

输出结果

abcdefg
higklmn
opq

(3)read(byte[] b, int off, int len)
从该输入流读取最多 len字节的数据为字节数组。 off表示从哪开始

try {
FileInputStream fis = new FileInputStream(file);
byte [] bytes = new byte[5];
int count = fis.read(bytes,0,5);
if(count!=-1) {
String str = new String(bytes, 0, count);
System.out.println(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

输出结果是

abcde

2、关闭文件的方法     close();
这个方法要放在finally语句块中,保证这个方法能够执行到。

字节输出流 FileOutputStream

将信息写入到文件中,如果不存在,系统会根据你给的文件名自动创建一个文件

构造方法
FileOutputStream(File file)
创建文件输出流以写入由指定的 File对象表示的文件。
FileOutputStream(File file, boolean append)
创建文件输出流以写入由指定的 File对象表示的文件。
FileOutputStream(FileDescriptor fdObj)
创建文件输出流以写入指定的文件描述符,表示与文件系统中实际文件的现有连接。
FileOutputStream(String name)
创建文件输出流以指定的名称写入文件。
FileOutputStream(String name, boolean append)
创建文件输出流以指定的名称写入文件。

代码演示主要的构造方法的使用

File file = new File("G:\\FileTest\\jiangheng.txt");
try {
//第一种
FileOutputStream fos = new FileOutputStream(file);
//第二种
FileOutputStream fos2 = new FileOutputStream("G:\\FileTest\\jiangheng.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
主要方法的运用方法

1、将信息写到文件中write,没有返回值

(1)write(int b)
这个方法每次只能写入一个字节

fos.write(96);

(2)write(byte[] b)这个方法可以将读取的字节存放到数组中然后在写到另一个文件中效率比更高

//定义一个字节型的数组,用来存放读取到的内容
byte [] bytes = new byte[5];
int count = fis.read(bytes);
//判断是否读取到文件中的内容
while(count!=-1) {
//将字节型的数组转换成字符串
fos.write(bytes);
//继续读取文件
count = fis.read(bytes);
}

(3)write(byte[] b, int off, int len)
从该输入流读取最多 len字节的数据为字节数组。然后在写到另一个文件中, off表示从哪开始,

try {
FileInputStream fis = new FileInputStream(file);
byte [] bytes = new byte[5];
int count = fis.read(bytes,0,5);
if(count!=-1) {
fos.write(bytes,0,5);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

2、关闭文件的方法      close();
这个方法要放在finally语句块中,保证这个方法能够执行到。
3,刷新文件的方法     flash();

字符流

只能操作纯文本文件,如.txt文件

字符输入流 FileReader

读取文件中信息,纯文本

构造方法
FileReader(File file)
创建一个新的 FileReader ,给出 File读取。
FileReader(FileDescriptor fd)
创建一个新的 FileReader ,给定 FileDescriptor读取。
FileReader(String fileName)
创建一个新的 FileReader ,给定要读取的文件的名称。

由于和字节流的用法很相似,这里不一一用代码展示用法了
主要方法的运用方法

1、读取文件中信息read

(1)read()
这个方法每次只能读一个字符,即两个字节

int code = fr.read();

(2read(char[] cbuf, int offset, int length)注意这里是char数组
从该输入流读取最多 len字节的数据为字节数组。然后在写到另一个文件中, off表示从哪开始,

try {
FileReader fr = new FileReader(file);
char [] ch = new char[5];
int count = fis.read(ch,0,5);
if(count!=-1) {
String str = new String(ch, 0, count);
System.out.println(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

字节输出流 FileWriter

将信息写入到文件中,如果不存在,系统会根据你给的文件名自动创建一个文件

构造方法
FileWriter(File file)
给一个File对象构造一个FileWriter对象。
FileWriter(File file, boolean append)
给一个File对象构造一个FileWriter对象。
FileWriter(FileDescriptor fd)
构造与文件描述符关联的FileWriter对象。
FileWriter(String fileName)
构造一个给定文件名的FileWriter对象。
FileWriter(String fileName, boolean append)
构造一个FileWriter对象,给出一个带有布尔值的文件名,表示是否附加写入的数据。
主要方法的运用方法

1、将信息写到文件中write,没有返回值

(1)write(int b)
这个方法每次只能写入一个字符

fw.write(96);

(2)write(char[] cbuf, int off, int len) 这个方法可以将读取的字节存放到数组中然后在写到另一个文件中效率比更高

//定义一个字节型的数组,用来存放读取到的内容
char[] ch= new char[5];r
int count = fr.read(ch,0,5);
//判断是否读取到文件中的内容
if(count!=-1) {
//将字节型的数组转换成字符串
fw.write(ch,0,5);
}

注意区分字节流和字符流的功能。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐