IO流----操作文件的9种方法代码实现

时间:2021-08-25 03:08:03

IO流----操作文件的9种方法代码实现:

1:使用字节流读写数据:

四种方式:

method1:          每次读写一个字节,边读边写:

/*
* 复制文本文件。
*
* 数据源:从哪里来
* a.txt -- 读取数据 -- FileInputStream
*
* 目的地:到哪里去
* b.txt -- 写数据 -- FileOutputStream
*
* java.io.FileNotFoundException: a.txt (系统找不到指定的文件。)
*
*边读边写:
*/
public class CopyFileDemo {
public static void main(String[] args) throws IOException {
// 封装数据源
FileInputStream fis = new FileInputStream("a.txt");
// 封装目的地
FileOutputStream fos = new FileOutputStream("b.txt"); int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
} // 释放资源(先关谁都行)
fos.close();
fis.close();
}
}

method2:每次读写一个字节数组:

/*
* 需求:把c:\\a.txt内容复制到d:\\b.txt中
*
* 数据源:
* c:\\a.txt -- 读取数据 -- FileInputStream
* 目的地:
* d:\\b.txt -- 写出数据 -- FileOutputStream
*/
public class CopyFileDemo {
public static void main(String[] args) throws IOException {
// 封装数据源
FileInputStream fis = new FileInputStream("c:\\a.txt");
FileOutputStream fos = new FileOutputStream("d:\\b.txt"); // 复制数据
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
//0:从0角标开始读取,len:每次读取的长度:
fos.write(bys, 0, len);
} // 释放资源
fos.close();
fis.close();
}
}

method3,method4:使用缓冲区高效率读写:

读取:

/*
* 注意:虽然我们有两种方式可以读取,但是,请注意,这两种方式针对同一个对象在一个代码中只能使用一个。
*/
public class BufferedInputStreamDemo {
public static void main(String[] args) throws IOException {
// BufferedInputStream(InputStream in)
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
"bos.txt"));
//一次读取一个字节:
// 读取数据
// int by = 0;
// while ((by = bis.read()) != -1) {
// System.out.print((char) by);
// }
// System.out.println("---------");
//一次读取一个字节数组:
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
System.out.print(new String(bys, 0, len));
} // 释放资源
bis.close();
}
}

写数据:

/
* 为什么不传递一个具体的文件或者文件路径,而是传递一个OutputStream对象呢?
* 原因很简单,字节缓冲区流仅仅提供缓冲区,为高效而设计的。但是呢,真正的读写操作还得靠基本的流对象实现。
*/
public class BufferedOutputStreamDemo {
public static void main(String[] args) throws IOException {
// BufferedOutputStream(OutputStream out)
// FileOutputStream fos = new FileOutputStream("bos.txt");
// BufferedOutputStream bos = new BufferedOutputStream(fos);
// 简单写法
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("bos.txt")); // 写数据
bos.write("hello".getBytes()); // 释放资源
bos.close();
}
}

2:使用字符流读写数据:

5种方式:

method1、method2 :一次读写一个字符,一次读写一个字符数组:

public class CopyFileDemo {
public static void main(String[] args) throws IOException {
// 封装数据源
FileReader fr = new FileReader("OnlyFileTest/Demo2.txt");
// 封装目的地
FileWriter fw = new FileWriter("a.txt");
// 读写数据
// 方式1 :一次读取一个字符:
// int ch = 0;
// while ((ch = fr.read()) != -1) {
// fw.write(ch);
// } // 方式2 一次读取一个字符数组:
char[] chs = new char[1024];
int len = 0;
while ((len = fr.read(chs)) != -1) {
fw.write(chs, 0, len);
//刷新缓冲区:
// fw.flush();
}
// 释放资源
fr.close();
fw.close();
}
}

method3、method4   :使用缓冲区高效读写:

   public static void main(String[] args) throws IOException {
// 封装数据源
BufferedReader fr = new BufferedReader(new FileReader("OnlyFileTest/Demo2.txt"));
// 封装目的地
BufferedWriter fw = new BufferedWriter(new FileWriter("a.txt"));
// 读写数据
// 方式3
// int ch = 0;
// while ((ch = fr.read()) != -1) {
// fw.write(ch);
// } // 方式4
char[] chs = new char[1024];
int len = 0;
while ((len = fr.read(chs)) != -1) {
fw.write(chs, 0, len);
//刷新缓冲区:
fw.flush();
}
// 释放资源
fr.close();
fw.close();
}

method5:一次读写一行:

/*
* 需求:把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt中
*
* 数据源:
* a.txt -- 读取数据 -- 字符转换流 -- InputStreamReader -- FileReader -- BufferedReader
* 目的地:
* b.txt -- 写出数据 -- 字符转换流 -- OutputStreamWriter -- FileWriter -- BufferedWriter
*/
public class CopyFileDemo2 {
public static void main(String[] args) throws IOException {
// 封装数据源
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
// 封装目的地
BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt")); // 读写数据
String line = null;
while ((line = br.readLine()) != null) {
//写入一行数据到“b.txt”中:
bw.write(line);
//换行写入另一行:
bw.newLine();
//刷新缓冲区:
bw.flush();
} // 释放资源
bw.close();
br.close();
}
}

注意:如果使用的相对路径(a.txt),创建的文件或文件夹默认在当前项目下面,使用绝对路径(“E:\\program\\file”)才可以指定到目录下;

3:打印流:

   @Test
public void test() throws IOException {
//字符输入流读取文件:
BufferedReader br = new BufferedReader(new FileReader("OnlyFileTest/Demo2.txt"));
//打印流对象:
PrintWriter pw = new PrintWriter(new FileWriter("OnlyFileTest/Demo3.txt"),true);
String line = null;
while ((line=br.readLine())!=null){
pw.println(line);
}
}

文件覆盖问题解决:

注意:一般我们在使用输出流(FileOutputStream)的时候会覆盖掉文件里面原先以存在的数据:

解决:

IO流----操作文件的9种方法代码实现