字符输入流
package MONA.demo02_字符输入流;
import java.io.FileReader;
public class Demo {
public static void main(String[] args) throws Exception {
FileReader fr = new FileReader("2.txt");
//20320
//System.out.println((char)fr.read());
//单个读取
int i = 0;
while ((i = fr.read()) != -1){
System.out.println((char)i);
}
//读取到数组里面
char[] chars = new char[3];
int count = 0;
while ((count = fr.read(chars)) !=-1){
for (int j = 0; j < chars.length; j++) {
System.out.println(chars[j]);
}
}
}
}
字符输出流
package MONA.demo03_字符输出流;
import java.io.FileWriter;
import java.io.IOException;
public class Demo {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("3.txt");
fw.write("你好呀");
//刷新
fw.flush();
char[] chars = {'美','女'};
fw.write(chars);
//fw.flush();
//关闭流:关闭后会自动刷新
fw.close();
}
}
使用字符流复制文本文件
package MONA.demo04_练习题;
import java.io.FileReader;
import java.io.FileWriter;
public class Demo01 {
public static void main(String[] args) throws Exception {
//边读取边输出
FileReader fr = new FileReader("test.txt");
FileWriter fw = new FileWriter("testCopy.txt");
int c = 0;
while ((c = fr.read()) !=-1){
fw.write(c);
//刷新
fw.flush();
}
fw.close();
fr.close();
System.out.println("复制完毕");
}
}
字节流转换为字符流
FileOutputStream:将一个字节输出流转换成字符输出流,方便字节写入字符
package MONA.demo05_转换流;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
/**
* 字节流
* 字节输出流
* 字节输入流
* 字符流
* 字符输出流
* 字符输入流
*/
public class Demo {
public static void main(String[] args) throws Exception {
//将一个字节输出流转换成字符输出流,方便字节写入字符
FileOutputStream fos = new FileOutputStream("4.txt");
//转换流
OutputStreamWriter osw = new OutputStreamWriter(fos);
osw.write("你好");
osw.close();
fos.close();
System.out.println("转换完成");
}
}
InputStreamReader:将一个字节输入流包装成字符输入流,方便读取字符
package MONA.demo05_转换流;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Demo {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("1.txt");
//转换
InputStreamReader isr = new InputStreamReader(fis);
int c = isr.read();
System.out.println((char)c);
}
}
结构整理
字节输入流 InputStream FileInputStream 字节输出流 OutputStream FileOutputStream 字符输入流 Reader InputStreamReader(转换流) FileReader 字符输出流 Writer OutputStreamWriter(转换流) FileWriter
package MONA.demo07_字符编码;
import java.io.FileInputStream;
import java.util.TreeMap;
/**
* 统计一个文件中
* 字母出现的次数
* 如有文件char.txt,内容为:
* aaabbbcccdddeeefff
* 实现思路:
* 1.读取文件,将文件内容保存到String类型的变量中
* 2.使用Map集合的特性,完成统计功能
*/
public class Demo01 {
public static void main(String[] args) throws Exception {
// FileInputStream fis = new FileInputStream("2.txt");
// //将所有的内容都读取到,之后再遍历
// byte[] bytes = new byte[10];
// int count = 0;
// StringBuffer sb = new StringBuffer();
// while ((count = fis.read(bytes)) !=-1){
// sb.append(new String (bytes,0,count));
// }
// TreeMap<Character,Integer> treeMap = new TreeMap<>();
// for(char c :sb.toString().toCharArray()) {
// //进行统计
// //containsKey 是否包含指定的key
// treeMap.put(c, treeMap.containsKey(c) ? treeMap.get(c) + 1 : 1);
// }
// System.out.println(treeMap);
TreeMap<Character,Integer> treeMap = new TreeMap<>();
//边读取边统计
FileInputStream fis = new FileInputStream("2.txt");
int n = 0;
while((n = fis.read()) != -1){
char c = (char) n;
treeMap.put(c, treeMap.containsKey(c) ? treeMap.get(c) + 1 : 1);
}
System.out.println(treeMap);
}
}