字符流
字符流
一个字符一个字符的读
mac系统下,一个中文字符占3个字节 默认使用UTF-8的编码表(通用的编码表)
Windows系统下,一个中文字符占2个字节 默认使用的GBK的编码表(简体中文)
注意:只能操作文本(不能写图片、音频、视频)
字符输出流
Writer(所有字符输出流的父类 抽象类)
FileWriter
构造方法(绑定写入的路径):
文件
字符串
注意:字符输出流在写入文件的时候需要调用刷新方法
代码实例:
FileWriter fWriter = new FileWriter("/Users/lanou/Desktop/level/haha.txt")
fWriter.write(100)
// 每次写入 最好都刷新一次
fWriter.flush()
// 字符数组写入
char[] c = {'l','o','n','g'}
fWriter.write(c)
fWriter.flush()
fWriter.write(c, 1, 3)
fWriter.flush()
// 使用字符串直接写入
fWriter.write("写一句古诗\n")
fWriter.flush()
fWriter.write("写一句古诗\n呃呃呃\n")
fWriter.flush()
fWriter.write("白日依山尽", 0, 2)
// 关闭资源前 会刷新
fWriter.close()
字符输入流
Reader(所有字符输入流的父类 抽象类)
读的时候不能直接读取字符串,因为字符串很难界定到哪结束,不太容易判断一个字符串
循环读取:
FileReader fr = new FileReader("/Users/lanou/Desktop/level/haha.txt");
int num = 0;
while ((num = fr.read()) != -1) {
System.out.println((char)num);
}
char[] c = new char[1024];
while ((num = fr.read(c)) != -1) {
System.out.println(new String(c, 0, num));
}
fr.close();
利用字符流复制文件
public class Demo {
public static void main(String[] args) {
File file1 = new File("/Users/lanou/Desktop/level/9.jpg");
File file2 = new File("/Users/lanou/Desktop/XTest/9.jpg");
name(file1, file2);
}
public static void name(File file1,File file2) {
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader(file1);
fw = new FileWriter(file2);
int len = 0;
char[] c = new char[1024];
while ((len = fr.read(c)) != -1) {
fw.write(c, 0, len);
}
} catch (FileNotFoundException e) {
throw new RuntimeException("找不到文件");
} catch (IOException e) {
throw new RuntimeException("文件复制失败");
} finally {
try {
if (fr != null) {
fr.close();
}
} catch (IOException e) {
throw new RuntimeException("关闭资源失败");
} finally {
try {
if (fw != null) {
fw.close();
}
} catch (IOException e) {
throw new RuntimeException("关闭资源失败");
}
}
}
}
}
转换流
OutputStreamWriter(字符流转向字节流)
作用:可以使用不同编码格式写入
需要使用FileOutPutStream类
InputStreamReader(字节流转向字符流)
作用:可以读取不同编码格式的文件
需要使用FileInputStream类
public class Demo {
public static void main(String[] args) throws IOException {
getUTF8();
getGBK();
readerGBK();
readUTF8();
}
public static void getUTF8() throws IOException {
FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/level/utf8.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos);
osw.write("SC");
osw.close();
}
public static void getGBK() throws IOException{
FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/level/gbk.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
osw.write("SC");
osw.close();
}
public static void readerGBK() throws IOException {
FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/level/gbk.txt");
InputStreamReader isr = new InputStreamReader(fis, "GBK");
int len = 0;
char[] c = new char[1024];
while ((len = isr.read(c)) != -1) {
System.out.println(new String(c, 0, len));
}
isr.close();
}
public static void readUTF8() throws IOException {
FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/level/utf8.txt");
InputStreamReader isr = new InputStreamReader(fis);
int len = 0;
char[] c = new char[1024];
while ((len = isr.read(c)) != -1) {
System.out.println(new String(c, 0, len));
}
isr.close();
}
}