字节流
字节流(总体参照是自己的程序)
输出:程序-->文件
输入:文件-->程序
字节输出流
字节输出流(OutPut) -- 写文件
OutPutStream是抽象类(不能实例化对象)并且是所有输出流的父类
一次写入一个字节(一字节是8个二进制位)
/*
* 构造方法:
* 参数 1.文件(被写入的文件)
* 2.文件路径(被写入的路径)
*
* 写入文件的流程:
* 1.绑定要写入的文件 或 文件路径
* 2.使用write方法写入
* 3.关闭资源
*/
public static void main(String[] args) throws IOException {
// 该路径下没有该文件会帮你创建出这个文件
// 如果已经有了该文件文件会被覆盖
// 绑定数据的目的地(绑定要写入的文件)
File file = new File("/Users/lanou/Desktop/level/yin.txt");
FileOutputStream oStream = new FileOutputStream(file);
// 写入数据 按ASCII表的值输入的
oStream.write(48);
// 用字节数组写入
byte[] b = {65,66,67,68};
oStream.write(b);
// 按字节数组的索引和长度写入
oStream.write(b, 0, 3);
// 简单写法
oStream.write("hello".getBytes());
oStream.write("world".getBytes());
// 关闭资源
oStream.close();
}
文件的续写和换行:
public static void main(String[] args) throws IOException {
File file = new File("/Users/lanou/Desktop/level/gs.txt");
FileOutputStream oStream = new FileOutputStream(file, true);
oStream.write("hello\n".getBytes());
oStream.close();
}
/*
* 异常处理
* IO发生异常 都需要停止程序 修改代码
*/
public static void main(String[] args) throws IOException {
File file = new File("/Users/lanou/Desktop/level/gs.txt");
// 增加作用域
FileOutputStream oStream = null;
try {
// 创建了一个流
oStream = new FileOutputStream(file);
oStream.write("hello".getBytes());
} catch (FileNotFoundException e) {
// 抛出一个运行时异常(直接停止掉程序)
throw new RuntimeException("文件找不到");
} catch (IOException e) {
// 文件编写失败 直接停止掉程序
throw new RuntimeException("文件编写失败");
} finally {
// 关闭资源
try {
// 非空判断
// 如果是空的 说明流创建失败 失败就不需要关闭
if (oStream != null) {
// 只要是你手动创建的流你就需要手动关闭
oStream.close();
}
} catch (IOException e) {
// 关闭资源失败 直接停止掉程序
throw new RuntimeException("关闭失败");
}
}
}
字节输入流
字节输入流(Input) -- 读文件
InputStream是抽象类不能实例化对象并且是所有输入流的父类
注意:读取是一个字节一个字节的读
读取文件流程
1.绑定数据源文件
2.使用read方法读
3.关闭资源
public static void main(String[] args) throws IOException{
// 绑定数据的目的地(绑定要写入的文件)
File file = new File("/Users/lanou/Desktop/level/znb.txt");
FileInputStream iStream = new FileInputStream(file);
// 读取
int i1 = iStream.read();
System.out.println((char)i1);
i1 = iStream.read();
System.out.println((char)i1);
i1 = iStream.read();
System.out.println((char)i1);
i1 = iStream.read();
System.out.println((char)i1);
i1 = iStream.read();
System.out.println((char)i1);
i1 = iStream.read();
// 当读取到文件末尾的时候 会返回-1
// 相当于 你读到-1 文件都读完了
System.out.println(i1);
// 关闭资源
iStream.close();
}
循环读取:
public static void main(String[] args) throws IOException{
File file = new File("/Users/lanou/Desktop/level/znb.txt");
FileInputStream iStream = new FileInputStream(file);
// 声明一个变量 保存读出来的结果
int num = 0;
while ((num=iStream.read()) != -1) {
System.out.println((char)num);
}
iStream.close();
}
注意:read方法每调用一次就读取一个字节,循环的时候read方法只能出现一次
使用字节数组读取文件:
public static void main(String[] args) throws IOException{
// 使用字节数组读取文件
File file = new File("/Users/lanou/Desktop/level/znb.txt");
FileInputStream iStream = new FileInputStream(file);
// 声明了一个长度为2的 空的 字节数组
// 读取时 会把读出来的内容放进字节数组中 存储
byte[] b = new byte[2];
int i1 = iStream.read(b);
System.out.println(i1); // 2 有效读取个数
// 利用字符串的构造方法直接打印字符串
System.out.println(new String(b)); // ab
i1 = iStream.read(b);
System.out.println(i1); // 2
// 利用字符串的构造方法直接打印字符串
System.out.println(new String(b)); // cd
i1 = iStream.read(b);
System.out.println(i1); // 1
// 利用字符串的构造方法直接打印字符串
System.out.println(new String(b)); // ed
i1 = iStream.read(b);
System.out.println(i1); // -1
// 利用字符串的构造方法直接打印字符串
System.out.println(new String(b)); // ed
iStream.close();
}
字节的输入输出流练习
使用字节的输入输出流进行文件的复制:
public class Demo01 {
public static void main(String[] args) {
long start = System.currentTimeMillis();
// 异常处理 的复制文件
// 读
FileInputStream iStream = null;
// 写
FileOutputStream oStream = null;
try {
iStream = new FileInputStream("/Users/lanou/Desktop/level/9.jpg");
oStream = new FileOutputStream("/Users/lanou/Desktop/level/10.jpg");
// 读一个字节 写一个字节
/*
int len = 0;
while ((len = iStream.read()) != -1) {
// 直接把读出来的写进文件
oStream.write(len);
}
*/
// 用字节数组的方式读写 利用缓冲数组读写 效率较高
byte[] b = new byte[1024];
int num = 0;
while ((num = iStream.read(b)) != -1) {
oStream.write(b, 0, num);
}
} catch (FileNotFoundException e) {
throw new RuntimeException("文件找不到");
} catch (IOException e) {
throw new RuntimeException("文件复制失败");
} finally {
// 这时读取和写入都完事了 先关哪个都可以
try {
if (iStream != null) {
iStream.close();
}
} catch (IOException e) {
throw new RuntimeException("关闭资源失败");
} finally {
// 写一起的话 如果第一个报了异常 第二个流会无法关闭
try {
if (oStream != null) {
oStream.close();
}
} catch (IOException e) {
throw new RuntimeException("关闭资源失败");
}
}
}
// 获取结束时间
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}
将一个文件夹复制到另一个文件夹:
public class Demo02 {
public static void main(String[] args) throws IOException {
File file1 = new File("/Users/lanou/Desktop/level");
File file2 = new File("/Users/lanou/Desktop/XTest");
copyFile(file1, file2);
}
/*
* 复制文件夹方法
* 参数
* file1 源文件(要被复制的)
* file2 目标文件夹(要把原文件夹复制进去)
*/
public static void copyFile(File file1,File file2) throws IOException {
// 在目标文件夹下创建 新的文件夹 名字跟源文件夹一样
File newDir = new File(file2, file1.getName());
newDir.mkdir();
/*
* 复制文件 ---> 找到文件 ---> 一边读一边写
* ---> 找到文件夹 ---> 继续调用本方法
*/
// 遍历源文件夹
File[] listFiles = file1.listFiles();
for (File file3 : listFiles) {
if (file3.isDirectory()) {
// 需要创建文件夹 继续遍历读写
copyFile(file3, newDir);
} else {
FileInputStream iStream = new FileInputStream(file3);
// 要在新建的文件夹下写入 newDir + file1.getName
File tempFile = new File(newDir, file3.getName());
FileOutputStream oStream = new FileOutputStream(tempFile);
byte[] b = new byte[1024];
int num = 0;
while ((num = iStream.read(b)) != -1) {
oStream.write(b, 0, num);
}
iStream.close();
oStream.close();
}
}
}
}
将一个文件夹下的txt文件复制到另一个文件夹:
public class Demo03 {
public static void main(String[] args) throws IOException {
File file1 = new File("/Users/lanou/Desktop/level");
File file2 = new File("/Users/lanou/Desktop/XTest");
getTXTFile(file1, file2);
}
public static void getTXTFile(File file1,File file2) throws IOException {
File[] listFiles = file1.listFiles(new FilterTXT());
for (File file : listFiles) {
if (file.isFile()) {
FileInputStream iStream = new FileInputStream(file);
File tempFile = new File(file2, file.getName());
FileOutputStream oStream = new FileOutputStream(tempFile);
byte[] b = new byte[1024];
int num = 0;
while ((num = iStream.read(b)) != -1) {
oStream.write(b, 0, num);
}
iStream.close();
oStream.close();
} else {
getTXTFile(file, file2);
}
}
}
}
// 过滤txt文件
class FilterTXT implements FileFilter {
@Override
public boolean accept(File pathname) {
// 不过滤文件夹
if (pathname.isDirectory()) {
return true;
}
return pathname.getName().endsWith("txt");
}
}