本次博客带领大家学习IO流中的常用类:FileInputStream和OutputStream。
FileInputStream
- 文件字节输入流,万能的,任何类型的文件都可以采用这个方法。
- 字节的方式,完成输入的操作,完成读的操作(硬盘 ——> 内存)。
- 方法一:使用循环readData接收文件.read()中的数据。
@Test
public void readFile01(){
String filePath = "d:\\111\\hello.txt";
int readData = 0;
FileInputStream fileInputStream = null;
try {
//创建 FileInputStream对象 ,用于读取 文件
fileInputStream = new FileInputStream(filePath);
//从该输入流读取一个字节的数据。如果没有输入可用,此方法将阻止。。
//如果返回-1,表示读取完毕。
while ((readData = fileInputStream.read())!= -1){
System.out.print((char)readData);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 方法二:使用循环buf数组接收read中的数据,注意buf数组的大小。
@Test
public void readFile02(){
String filePath = "d:\\111\\hello.txt";
//字节数组
byte[] buf = new byte[30]; //一次读取30个字节。
int readLen = 0;
FileInputStream fileInputStream = null;
try {
//创建 FileInputStream对象 ,用于读取 文件
fileInputStream = new FileInputStream(filePath);
//从该输入流读取最多b.length字节的数据到字节数组。如果没有输入可用,此方法将阻止。。
//如果返回-1,表示读取完毕。
//如果读取正常,返回实际读取的字节数
while ((readLen = fileInputStream.read(buf))!= -1){
System.out.print(new String(buf, 0,readLen));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
OutputStream
- 字节的方式,完成输出的操作,完成写的操作(内存 ——> 硬盘)。
- 当需要写入一个字节到文件里面的话,使用fileOutputStream.write(‘L’);
- 当需要写入一个字符串到文件里面的话,使用fileOutputStream.write(str.getBytes());
- 当需要写入一个字符串并按规定的长度到文件里面的话,使用fileOutputStream.write(str.getBytes(),0,3);
@Test
public void writeFile(){
//创建 FileOutputStream 对象
String filePath = "d:\\111\\a.txt";
FileOutputStream fileOutputStream = null;
try {
//得到 FileOutputStream 对象
fileOutputStream = new FileOutputStream(filePath);
//写入一个字节
//fileOutputStream.write('L');
//写入字符串
String str = "hello,world";
//str.getBytes() 可以把 字符串->字节数组
//fileOutputStream.write(str.getBytes());
/*
write(byte[] b,int off,int len)将 len字节从位于偏移量 off的指定字节数组写入此文件输入流。
*/
fileOutputStream.write(str.getBytes(),0,3);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 注意事项:
- new FileOutputStream(filePath) 创建方式,当写入内容时,会覆盖原来的内容。
- new FileOutputStream(filePath,true) 创建方式,当写入内容时,是追加到文件后面。
综合案例:拷贝文件
- 完成 文件拷贝,将 d:\111\boluo.jpg 拷贝到 d:\boluo.jpg。
public static void main(String[] args) {
// 完成 文件拷贝,将 d:\\111\\boluo.jpg 拷贝到 d:\\
//思路分析
//1.创建文件的输入流,将文件读入到程序
//2.创建文件的输出流,将读取到的文件数据,写入到指定的文件
String srcFilePath = "d:\\111\\boluo.jpg";
String destFilePath = "d:\\boluo.jpg";
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream(srcFilePath);
fileOutputStream = new FileOutputStream(destFilePath);
//定义一个字节数组,提高读取效果。
byte[] buf = new byte[1024];
int readLen = 0;
while ((readLen= fileInputStream.read(buf))!= -1){
//读取到后,就写入到文件,通过 fileOutputStream
fileOutputStream.write(buf,0,readLen);
}
System.out.println("拷贝完成!");
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fileInputStream != null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream != null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}