1、使用IO字节流读取文件数据
/**
* 使用IO字节流读取文件数据
*
* @param path 文件的路径,可以是相对路径或者绝对路径
*/
public static void readFileByFileInputStream(String path) {
System.out.println("=== 读取文件开始 Start ===");
// 根据路径拿到文件对象,文件对象可以获取更多的文件信息
File file = new File(path);
// 初始化输入流
InputStream inputStream = null;
try {
// 创建字节输入流
inputStream = new FileInputStream(file);
// 创建1kb字节数组用来
byte[] buffer = new byte[2048];
// 每次读取的字节数
int len;
// 读取数据并放到buffer数组中
while ((len = inputStream.read(buffer)) != -1) {
// UTF-8为变长编码,一个汉字占3个字节
System.out.println("本次读取" + len + "个字节数据内容为:" + new String(buffer, 0, len));
}
} catch (FileNotFoundException e) {
// 文件未找到时异常处理
e.printStackTrace();
} catch (IOException e) {
// 读取过程中,删除文件会出此异常
e.printStackTrace();
} finally { // 关闭流过程,也有可能出现异常
try {
if (inputStream != null) inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("=== 读取文件 End ===");
}
2、写入文件通过IO字节流方法
/**
* 写入文件通过IO字节流方法
*
* @param path 输出文件的路径
*/
public static void writeFileByOS(String path) {
System.out.println("=== 写入文件 Start ===");
// 1、创建文件对象
File file = new File(path);
// 2、初始化字节输出流
OutputStream outputStream = null;
// 3、写出内容
String outInfo = "唐建-写出测试outputstream";
// 4、内容转成字节数组
byte[] byteArray = outInfo.getBytes();
try {
// 输出字节流(file,true)接通管道,这里有true的话,代表可以在文件后面追加内容
outputStream = new FileOutputStream(file);
// 输出流写入字节数据
outputStream.write(byteArray);
System.out.println("按照字节流成功写出内容:" + outInfo);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally { // 关闭写出流时,注意抓异常
try {
if (outputStream != null) outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("=== 写入文件 End ===");
}
3、文件复制 try catch finally
/**
* 通过字节流的方式将文件内容拷贝到另一个文件中
* <p>
* Step 1.根据文件路径,构建源文件对象
* Step 2.根据文件路径,构造目的文件对象
* Step 3.创建字节输入流从源文件中读取信息
* Step 4.将读入到内存的信息再写出到目的文件中
* Step 5.拷贝完成后关闭输入输出流
*/
public static void copyFile(String from, String to) {
System.out.println("=== 复制文件 Start ===");
// 输入文件对象
File inFile = new File(from);
// 输出文件对象
File outFile = new File(to);
// 初始化输入流
InputStream inputStream = null;
// 初始化输出流
OutputStream outputStream = null;
try {
// 将输入流和输入文件对象接通
inputStream = new FileInputStream(inFile);
// 将输出流和输出文件对象接通
outputStream = new FileOutputStream(outFile);
// 定义一个字节数组转移数据
byte[] buffer = new byte[1024];
int len;//记录每次读取的字节数
while ((len=inputStream.read(buffer))!=-1){
outputStream.write(buffer,0,len); //读多少,输出多少
}
System.out.println("复制文件成功完成");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally { // 关闭输入流异常后,也要保证输出流关闭
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
System.out.println("=== 复制文件 End ===");
}
4、文件复制try catch resouce写法
public static void copyFile(String from, String to) {
System.out.println("=== 复制文件 Start ===");
// 输入文件对象
File inFile = new File(from);
// 输出文件对象
File outFile = new File(to);
try (// 将输入流和输入文件对象接通
InputStream inputStream = new FileInputStream(inFile);
// 将输出流和输出文件对象接通
OutputStream outputStream = new FileOutputStream(outFile);
) { // 定义一个字节数组转移数据
byte[] buffer = new byte[1024];
int len;//记录每次读取的字节数
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len); //读多少,输出多少
}
System.out.println("复制文件成功完成");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("=== 复制文件 End ===");
}