字符流
OutputStream
把内存中的数据写入到硬盘的文件
1 写入单个字符
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("//Users/jiangcheng/Desktop/a.txt");
fos.write(97);//当写入硬盘的时候会把十进制的数据变成二进制 97---1100001
fos.close();
}
//当打开a.txt的时候,会发现写入的是a,这是因为任意文本编辑器打开文件都回去查询编码表,把字节转换为字符表示
2 写入多个字符
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("//Users/jiangcheng/Desktop/a.txt");
byte[] bytes = {65, 66, 67, 68, 69};//文件内容是ABCDE
//如果写的第一个字节为负数,则第一个负数和第二个字节会组成一个中文显示,查询系统默认GBK表
fos.write(bytes);
fos.close();
}
3 写入字符串
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("//Users/jiangcheng/Desktop/a.txt");
byte[] bytes = "你好".getBytes();//把字符串转为字节数组
fos.write(bytes);
fos.close();
}
4 追加写/续写
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("//Users/jiangcheng/Desktop/a.txt", true);
//true:创建对象不会覆盖文件,继续在文件末尾追加写数据
//false 创建新文件,覆盖原文件
byte[] bytes = "你好".getBytes();
fos.write(bytes);
fos.close();
}
5换行操作
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("//Users/jiangcheng/Desktop/a.txt", true);
byte[] bytes = "你好".getBytes();
fos.write(bytes);
String nextLine = System.getProperty("line.separator");
fos.write(nextLine.getBytes());
fos.write(bytes);
fos.close();
}
InputStream
把硬盘数据读取到内存中使用
1 读取单个字符
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("//Users/jiangcheng/Desktop/a.txt");
int len =0;
while ((len = fis.read()) != -1) {
System.out.println(len);
}
fis.close();
}
2 读取多个字符
public static void main(String[] args) throws IOException {
//文件内容为abc
FileInputStream fis = new FileInputStream("//Users/jiangcheng/Desktop/a.txt");
byte[] bytes = new byte[2];
int len=fis.read(bytes);//代表读取字符的长度
System.out.println(len);//2
System.out.println(Arrays.toString(bytes));//97 98
System.out.println(new String(bytes));//a b
fis.close();
}
3 读取多个字符
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("//Users/jiangcheng/Desktop/a.txt");
byte[] bytes = new byte[1024];
int len = 0;
while ((len = fis.read(bytes)) != 1) {
System.out.println(new String(bytes,0,len));
}
fis.close();
}
字节流
当使用字节流的时候,遇到中文字符,可能会显示不完整的字符,那是因为一个中文字符可能占多个字节存储,所以要用字符流来处理。
FileReader
把硬盘文件中的数据以字符的方式读取到内存中
1 读取单个字符
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("//Users/jiangcheng/Desktop/a.txt");
int len = 0;
while ((len = fr.read()) != -1) {
System.out.println((char) len);
}
}
2 读取多个字符
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("//Users/jiangcheng/Desktop/a.txt");
int len = 0;
char[] cs = new char[1024];
while ((len = fr.read(cs)) != -1) {
System.out.println(new String(cs,0,len));
}
}
FileWriter
把内存中的字符写入到磁盘文件中
1 写入单个字符
//1 创建FileWriter对象
//2 使用writer方法,把数据写入到内存缓冲区(字符转换为字节的过程)
//3 使用flush ,把内存缓冲区的数据刷新到文件
//4 释放资源 (会先把内存缓冲区的数据刷新到文件)
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("//Users/jiangcheng/Desktop/a.txt");
int len = 0;
fw.write(97);
fw.flush();
fw.write(98);
fw.close();
}
2 写入多个字符
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("//Users/jiangcheng/Desktop/a.txt");
char[] cs = {'a', 'b', 'c', 'd', 'e'};
fw.write(cs);
fw.flush();
fw.close();
}
缓冲流
BufferedOutputStream字节缓冲输出流
1 写入字符
//1 创建FileOutputStream对象,构造方法中绑定输出目的地
//2 创建BufferedOutputStream对象,构造方法中传递FileOutputStream对象,提高FileOutputStream效率
//3 使用BufferedOutputStream的write,把数据写到内部缓冲区
//4 使用BufferedOutputStream的flush,把内部缓冲区的数据刷新到文件
//5 释放资源 (会先调用flush方法,把内存缓冲区的数据刷新到文件)
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("//Users/jiangcheng/Desktop/a.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write("写入数据到内部缓冲区".getBytes());
bos.flush();
bos.close();
}
BufferedInputStream字节缓冲输入流
1 逐个字符读取
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("//Users/jiangcheng/Desktop/a.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
int len=0;
while ((len = bis.read()) != -1) {
System.out.println(len);
}
bis.close();
}
2 读取多个字符
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("//Users/jiangcheng/Desktop/a.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
int len=0;
byte[] bytes = new byte[1024];
while ((len = bis.read(bytes)) != -1) {
System.out.println(new String(bytes, 0, len));
}
bis.close();
}
BufferedWriter字符缓冲输出流
1 逐行一个字符串写入
//1 创建字符缓冲输出流对象
//2 调用writer方法,把数据写到缓冲区
//3 调用flush把内存缓冲区数据写到磁盘
//4 释放资源
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("//Users/jiangcheng/Desktop/a.txt"));
for (int i = 0; i < 10; i++) {
bw.write("你好");
bw.newLine();//换行
}
bw.flush();
bw.close();
}
BufferedReader字符缓冲输入流
1 逐行读取字符串
//给FileReader增加一个缓冲区,提高读取速度
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("//Users/jiangcheng/Desktop/a.txt"));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}