目录
1.利用字节流读取文件(汉字)
提供一个Demo1.txt文件,在里面添加内容:星期一到星期日
利用字节流读取文件:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Demo1 {
public static void main(String[] args) {
File file = new File("E:\\javatxt\\Demo1.txt");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
int data = -1;
byte[] b = new byte[1024];
while ((data = fis.read(b)) != -1) {
for (int i = 0; i < data; i++) {
System.out.print((char)b[i]);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
结果是乱码:
此时需要将文本文档的编码调节,用记事本打开,另存为ANSI编码,并替换原文件
然后打开java工程或文件将文件的格式设置为GBK(不同环境的默认可能不同)
由于英文为一个字节一个字母,中文是两个字节一个汉字,所以普通FileInputStream对象无法满足
然后调整代码,定义一个InputStreamReader类型的空变量isr,然后正常创建实例化字节流FileInputStream类型的fis对象,然后再实例化isr对象的时候引用fis对象
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class Demo1 {
public static void main(String[] args) {
// 1.定义InputStreamReader类型的fr变量
InputStreamReader isr = null;
try {
// 2.实例化InputStreamReader对象,获取对应路径下的文件
// 字节流的转中文
FileInputStream fis = new FileInputStream("E:\\javatxt\\demo1.txt");
isr = new InputStreamReader(fis, "GBK");
// 3.定义临时变量a,判断并读取
int a = -1;
while ((a = isr.read()) != -1) {
System.out.print((char) a);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4.关闭reader流对象
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
运行结果:
2.利用字节流复制文件:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo2 {
public static void main(String[] args) {
File file = new File("E:\\javatxt\\Demo1.txt");
File file_new = new File("E:\\javatxt\\Demo2.txt");
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file);
fos = new FileOutputStream(file_new);
int data = -1;
byte[] b = new byte[1024];
while ((data = fis.read(b)) != -1) {
fos.write(b, 0, data);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fos.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
复制结果:
同时两个文件大小相同:都是54字节
3.利用字符流读取文件
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Demo3 {
public static void main(String[] args) {
File file = new File("E:\\javatxt\\Demo1.txt");
FileReader fr = null;
try {
fr = new FileReader(file);
int data = -1;
char[] b = new char[1024];
while ((data = fr.read(b)) != -1) {
for (int i = 0; i < data; i++) {
System.out.print(b[i]);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
在转换完成后,结果为:
4.利用字符流复制文件
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Demo4 {
public static void main(String[] args) {
File file = new File("E:\\javatxt\\Demo1.txt");
File file_new = new File("E:\\javatxt\\Demo3.txt");
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader(file);
fw = new FileWriter(file_new);
int data = -1;
char[] b = new char[1024];
while ((data = fr.read(b)) != -1) {
fw.write(b, 0, data);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fw.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
输出结果为:
大小为54字节,相同。
5.字符流+缓冲流
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Demo4_copy {
public static void main(String[] args) {
// 1.定义FileReader字符流读取对象
FileReader fr = null;
// 2.定义BufferedReader缓冲字符流读取对象
BufferedReader br = null;
// 3.定义FileWriter字符流写出对象
FileWriter fw = null;
// 4.定义BufferedWriter缓冲字符流写出对象
BufferedWriter bw = null;
try {
// 5.全部实例化,将fr,fw作为参数实例化br,bw
fr = new FileReader("E:\\javatxt\\demo1.txt");
br = new BufferedReader(fr);
fw = new FileWriter("E:\\javatxt\\demo4.txt");
bw = new BufferedWriter(fw);
// 6.定义字符串str变量
String str = null;
// 7.循环遍历读取流中的数据,返回值给str赋值,直接读取接收,返回值为null
while ((str = br.readLine()) != null) {
// 8.通过流的方式写出在字符串中
bw.write(str + "\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 9.关闭流(注意顺序)
try {
if (bw != null) {
bw.close();
}
if (fw != null) {
fw.close();
}
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
此时内容相同:
但是字节数变为49字节,
查询原因后得知,由于文档中的回车是两个字节,\r\n,所以,在程序中也要添加两个字符,并且注意在文档中最后一行的回车,如果文中没有回车,但是在程序中会添加,所以复制后的文件会多两个字节。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Demo4_copy {
public static void main(String[] args) {
// 1.定义FileReader字符流读取对象
FileReader fr = null;
// 2.定义BufferedReader缓冲字符流读取对象
BufferedReader br = null;
// 3.定义FileWriter字符流写出对象
FileWriter fw = null;
// 4.定义BufferedWriter缓冲字符流写出对象
BufferedWriter bw = null;
try {
// 5.全部实例化,将fr,fw作为参数实例化br,bw
fr = new FileReader("E:\\javatxt\\demo1.txt");
br = new BufferedReader(fr);
fw = new FileWriter("E:\\javatxt\\demo5.txt");
bw = new BufferedWriter(fw);
// 6.定义字符串str变量
String str = null;
// 7.循环遍历读取流中的数据,返回值给str赋值,直接读取接收,返回值为null
while ((str = br.readLine()) != null) {
// 8.通过流的方式写出在字符串中
bw.write(str + "\r\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 9.关闭流(注意顺序)
try {
if (bw != null) {
bw.close();
}
if (fw != null) {
fw.close();
}
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
内容相同,但是字节多两个,是因为在Demo1中文末没有换行,但是在Demo5中由于程序的设计,会多出一个回车,两个字节