1.FileWriter
package daigua8;
import java.io.FileWriter;
import java.io.IOException;
/*
输出流写数据的步骤:
* A:创建输出流对象
* B:调用输出流对象的写数据的方法
* C:释放资源
*/
public class FileWriterDemo {
public static void main(String[] args) throws IOException {
// 创建一个输出流对象
FileWriter fw = new FileWriter("/Users/dengrunting/Desktop/test.txt");
// 写入信息到文件
fw.write("你好,我是你爸爸!");
// 从内存缓冲区,压入到磁盘
fw.flush();
// 关闭输出流对象,系统释放相关资源
fw.close();
}
}
2.FileReader
package daigua8;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderDemo {
public static void main(String[] args) throws IOException{
// 创建一个输入流对象
FileReader fr = new FileReader("/Users/dengrunting/Documents/eclipse-workspace/my_demo/src/daigua8/FileReaderDemo.java");
int ch;
while ((ch = fr.read()) != -1) {
System.out.print((char)ch);
}
fr.close();
}
}
3.文件复制(单字节)
package com.daigua;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;
public class FileCopy {
public static void main(String[] args) throws IOException{
// 创建输入输出流对象
FileReader fr = new FileReader("/Users/dengrunting/Desktop/java视频/test.txt");
FileWriter fw = new FileWriter("/Users/dengrunting/Desktop/java视频/test_copy.txt");
// 边读边写
int ch;
while ((ch = fr.read()) != -1) {
System.out.print((char)ch);
fw.write((char)ch);
}
// 刷新缓冲区
fw.flush();
fw.close();
fr.close();
}
}
4.文件复制(多字节)
package com.daigua;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;
public class FileCopy2 {
public static void main(String[] args) throws IOException{
// 创建输入输出流对象
FileReader fr = new FileReader("./src/com/daigua/FileCopy.java");
FileWriter fw = new FileWriter("./src/com/daigua/copy.FileCopy.java");
// 边读边写,一次读入1024字节
char[] chs = new char[1024];
int ch;
while ((ch = fr.read(chs)) != -1) {
System.out.print(chs);
fw.write((char)ch);
}
// 刷新缓冲区
fw.flush();
fw.close();
fr.close();
}
}
5.缓冲流复制文件
package com.daigua;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
public class BufferedReaderDemo {
public static void main(String[] args) throws IOException{
// 创建输入输出流对象
BufferedReader fr = new BufferedReader(new FileReader ("./src/com/daigua/BufferedReaderDemo.java"));
BufferedWriter fw = new BufferedWriter(new FileWriter("./src/com/daigua/copy.FileCopy2.java"));
// 边读边写,一次读入1024字节
char[] chs = new char[1024];
int len;
while ((len = fr.read(chs)) != -1) {
System.out.print(chs);
fw.write(chs, 0, len);
}
// 刷新缓冲区
fw.flush();
fw.close();
fr.close();
}
}
6.缓冲流特有方法复制文件
package com.daigua;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
public class BufferedStreamDemo {
public static void main(String[] args) throws IOException{
// 创建输入输出流对象
BufferedReader fr = new BufferedReader(new FileReader ("./src/com/daigua/BufferedReaderDemo.java"));
BufferedWriter fw = new BufferedWriter(new FileWriter("./src/com/daigua/copy.FileCopy3.java"));
// 边读边写,以此读取一行
String line;
while ((line = fr.readLine()) != null) {
System.out.print(line + "
");
// fw.write(line + "
");
fw.write(line);
fw.newLine();
fw.flush(); // 读一行写一行
}
fw.close();
fr.close();
}
}