Day31-IO流原理及流的分类、FileInputStream、FileOutputStream、文件拷贝
IO流原理及流的分类
- java IO流的原理
- 流的分类
- Java的IO流共涉及40多个类,实际上非常规则,都是从如上四个抽象基类派生的。
- 由这四个类派生出来的子类名称都是以其父类名作为子类名后缀的。
FileInputStream
- 单个字节的读取
package com.ghy.inputstream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
//演示FileInputStream的使用(字节输入流,文件--》程序)
public class FileInputStream01 {
public static void main(String[] args) {
String filePath = "D:\\code\\HelloXiaoZhao.txt";
int readData = 0;
FileInputStream fileInputStream =null;
try {
//创建了 FileInputStream 对象,用于读取 文件
//需要用try...catch捕获异常,不然会出现编译错误
fileInputStream = new FileInputStream(filePath);
//不能读取汉字,一个汉字代表三个字节,只可以读取一个字节,会乱码
//从该输入流读取一个字节的数据,如果没有输入可用,此方法将阻止。返回为-1表示到达文件末
//判断是否等于-1
while ((readData = fileInputStream.read())!= -1){
System.out.print((char)readData);//转成char类型
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭文件流,避免造成资源浪费
//也需要捕获异常
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
输出
hello,xiaozhao!
- 优化,使用read(byte[] b)
package com.ghy.inputstream;
//使用read(byte[] b)读取文件,提高效率
// .)
import java.io.FileInputStream;
import java.io.IOException;
//演示FileInputStream的使用(字节输入流,文件--》程序)
public class FileInputStream02 {
public static void main(String[] args) {
String filePath = "D:\\code\\HelloXiaoZhao.txt";
int readData = 0;
//定义字节数组
byte[] buf =new byte[8];//一次读取八个字节,
FileInputStream fileInputStream =null;
try {
//创建了 FileInputStream 对象,用于读取 文件
//需要用try...catch捕获异常,不然会出现编译错误
fileInputStream = new FileInputStream(filePath);
//从该输入流读取最多b.length字节的数据到字节数组,此方法将阻塞,直到某些输入可用。返回为-1表示到达文件末
//判断是否等于-1
//如果读取正常,返回实际读取的字节数
while ((readData = fileInputStream.read(buf))!= -1){
System.out.print(new String(buf,0,readData));//转成字符串显示
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭文件流,避免造成资源浪费
//也需要捕获异常
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
输出
hello,xiaozhao!
读的次数变少了,所以效率提高了
FileOutputStream
- 写入一个字节
package com.ghy.outputstream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
//将数据写入到该文件中,如果数据不存在则创建该文件
public class FileOutputStream01 {
public static void main(String[] args) {
//创建 FileOutputStream对象
FileOutputStream fileOutputStream=null;
//文件路径
String filePath = "D:\\code\\a\\ZhaoHeart.txt";
try {
//得到FileOutputStream对象
fileOutputStream = new FileOutputStream(filePath);
//写入一个字节
fileOutputStream.write('G');
System.out.println("文件写入成功!");
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
输出
文件写入成功!
- 写入字符串
package com.ghy.outputstream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
//将数据写入到该文件中,如果数据不存在则创建该文件
public class FileOutputStream01 {
public static void main(String[] args) {
//创建 FileOutputStream对象
FileOutputStream fileOutputStream=null;
//文件路径
String filePath = "D:\\code\\a\\ZhaoHeart.txt";
try {
//得到FileOutputStream对象
fileOutputStream = new FileOutputStream(filePath);
//写入一个字节
//fileOutputStream.write('G');
String str="hello,xiaozhao!";
//str.getBytes()将可用将 字符串转换成字节数组
fileOutputStream.write(str.getBytes());
System.out.println("文件写入成功!");
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
输出
文件写入成功!
查看
- 写入字符串(方法二)
package com.ghy.outputstream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
//将数据写入到该文件中,如果数据不存在则创建该文件
public class FileOutputStream01 {
public static void main(String[] args) {
//创建 FileOutputStream对象
FileOutputStream fileOutputStream=null;
//文件路径
String filePath = "D:\\code\\a\\ZhaoHeart.txt";
try {
//得到FileOutputStream对象
fileOutputStream = new FileOutputStream(filePath);
//写入一个字节
//fileOutputStream.write('G');
String str="hello,xiaogong!";
//str.getBytes()将可用将 字符串转换成字节数组
//fileOutputStream.write(str.getBytes());
//fileOutputStream.write(str.getBytes(),起始位置,输入的长度);
fileOutputStream.write(str.getBytes(),0,str.length());
System.out.println("文件写入成功!");
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
输出
文件写入成功!
查看
1.new FileOutputStream(filePath)创建方式会覆盖原来的内容
2.new FileOutputStream(filePath,true)创建方式是追加到文件后面
文件拷贝
package com.ghy.outputstream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopy01 {
public static void main(String[] args) {
//完成文件拷贝"D:\\code\\xiaozhu.jpg"拷贝到"D:\\code\\a"
//思路分析
//1.创建文件的输入流,将文件读入到程序。
//2.创建文件的输出流,将读取到的文件数据,写入到指定的文件。
String srcFilePath = "D:\\code\\xiaozhu.jpg";//源文件路径
String newFilePath = "D:\\code\\a\\xiaozhu.jpg";//新文件路径
FileInputStream fileInputStream =null;//输入
FileOutputStream fileOutputStream=null;//输出
try {
fileInputStream = new FileInputStream(srcFilePath);//输入流
fileOutputStream = new FileOutputStream(newFilePath);//输出流
//读取
//先定义字节数组提高效率
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 {
try {
if(fileInputStream != null){
//输入流不为空则关闭
fileInputStream.close();
}
if(fileOutputStream != null){
//输出流不为空则关闭
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
输出
拷贝成功!
查看