常见字符集:

1. 常见字符集 底层字符的编码是什么样的?
- 英文和数字等在任何国家的字符集中都占1个字节
- GBK字符中一个中文字符占2个字节
- UTF-8编码中一个中文1般占3个字节
2. 编码前的字符集和解码时的字符集有什么要求?
- 必须一致,否则会出现字符乱码
- 英文和数字不会乱码
FileInputStream & FileOutputStream
- public class Test {
- public static void main(String[] args) {
-
- try (
- // 这里面只能放置资源对象,用完会自动关闭:自动调用资源对象的close方法关闭资源(即使出现异常也会做关闭操作)
- // 1、创建一个字节输入流管道与原视频接通
- InputStream is = new FileInputStream("file-io-app/src/out04.txt");
- // 2、创建一个字节输出流管道与目标文件接通
- OutputStream os = new FileOutputStream("file-io-app/src/out05.txt");
-
- // int age = 23; // 这里只能放资源
- MyConnection connection = new MyConnection(); // 最终会自动调用资源的close方法
- ) {
-
- // 3、定义一个字节数组转移数据
- byte[] buffer = new byte[1024];
- int len; // 记录每次读取的字节数。
- while ((len = is.read(buffer)) != -1) {
- os.write(buffer, 0, len);
- }
- System.out.println("复制完成了!");
-
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- }
- }
-
- class MyConnection implements AutoCloseable {
- @Override
- public void close() throws IOException {
- System.out.println("连接资源被成功释放了!");
- }
- }
FileReader & FileWriter
- public static void main(String[] args) throws Exception {
- // 1、创建一个文件字符输入流与源文件接通
- Reader fr = new FileReader("file-io-app/src/data07.txt");
-
- // 2、用循环,每次读取一个字符数组的数据。 1024 + 1024 + 8
- char[] buffer = new char[1024]; // 1K字符
- int len;
- while ((len = fr.read(buffer)) != -1) {
- String rs = new String(buffer, 0, len);
- System.out.print(rs);
- }
-
- }
-
- public static void main(String[] args) throws Exception {
- // 1、创建一个字符输出流管道与目标文件接通
- // Writer fw = new FileWriter("file-io-app/src/out08.txt"); // 覆盖管道,每次启动都会清空文件之前的数据
- Writer fw = new FileWriter("file-io-app/src/out08.txt", true); // 覆盖管道,每次启动都会清空文件之前的数据
-
- fw.write(98);
- fw.write('a');
- fw.write('徐'); // 不会出问题了
- fw.write("\r\n"); // 换行
-
- // b.public void write(String c)写一个字符串出去
- fw.write("abc我是中国人");
- fw.write("\r\n"); // 换行
-
-
- // c.public void write(char[] buffer):写一个字符数组出去
- char[] chars = "abc我是中国人".toCharArray();
- fw.write(chars);
- fw.write("\r\n"); // 换行
-
-
- // d.public void write(String c ,int pos ,int len):写字符串的一部分出去
- fw.write("abc我是中国人", 0, 5);
- fw.write("\r\n"); // 换行
-
-
- // e.public void write(char[] buffer ,int pos ,int len):写字符数组的一部分出去
- fw.write(chars, 3, 5);
- fw.write("\r\n"); // 换行
-
-
- // fw.flush();// 刷新后流可以继续使用
- fw.close(); // 关闭包含刷线,关闭后流不能使用
-
- }
BufferedInputStream & BufferedOutputStream
- public static void main(String[] args) {
-
- try (
- // 这里面只能放置资源对象,用完会自动关闭:自动调用资源对象的close方法关闭资源(即使出现异常也会做关闭操作)
- // 1、创建一个字节输入流管道与原视频接通
- InputStream is = new FileInputStream("D:\\resources\\newmeinv.jpeg");
- // a.把原始的字节输入流包装成高级的缓冲字节输入流
- InputStream bis = new BufferedInputStream(is);
- // 2、创建一个字节输出流管道与目标文件接通
- OutputStream os = new FileOutputStream("D:\\resources\\newmeinv222.jpeg");
- // b.把字节输出流管道包装成高级的缓冲字节输出流管道
- OutputStream bos = new BufferedOutputStream(os);
-
- ) {
-
- // 3、定义一个字节数组转移数据
- byte[] buffer = new byte[1024];
- int len; // 记录每次读取的字节数。
- while ((len = bis.read(buffer)) != -1){
- bos.write(buffer, 0 , len);
- }
- System.out.println("复制完成了!");
-
- } catch (Exception e){
- e.printStackTrace();
- }
-
- }
BufferedReader & BufferedWriter
- public static void main(String[] args) {
- try(
- // 1、创建缓冲字符输入流管道与源文件接通
- BufferedReader br = new BufferedReader(new FileReader("io-app2/src/csb.txt"));
-
- // 5、定义缓冲字符输出管道与目标文件接通
- BufferedWriter bw = new BufferedWriter(new FileWriter("io-app2/src/new.txt"));
- ) {
-
- // 2、定义一个List集合存储每行内容
- List
data = new ArrayList<>(); - // 3、定义循环,按照行读取文章
- String line;
- while ((line = br.readLine()) != null){
- data.add(line);
- }
- System.out.println(data);
-
- // 4、排序
- // 自定义排序规则
- List
sizes = new ArrayList<>(); - Collections.addAll(sizes, "一","二","三","四","五","陆","柒","八","九","十","十一");
-
- Collections.sort(data, new Comparator
() { - @Override
- public int compare(String o1, String o2) {
- // o1 八.,....
- // o2 柒.,....
- return sizes.indexOf(o1.substring(0, o1.indexOf(".")))
- - sizes.indexOf(o2.substring(0, o2.indexOf(".")));
- }
- });
- System.out.println(data);
-
- // 6、遍历集合中的每行文章写出去,且要换行
- for (String datum : data) {
- bw.write(datum);
- bw.newLine(); // 换行
- }
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
当读取固定字符集,或者输出为固定字符集时
- public static void main(String[] args) throws Exception {
- // 代码UTF-8 文件 GBK "D:\\resources\\data.txt"
- // 1、提取GBK文件的原始字节流。 abc 我
- // ooo oo
- InputStream is = new FileInputStream("D:\\resources\\data.txt");
- // 2、把原始字节流转换成字符输入流
- // Reader isr = new InputStreamReader(is); // 默认以UTF-8的方式转换成字符流。 还是会乱码的 跟直接使用FileReader是一样的
- Reader isr = new InputStreamReader(is , "GBK"); // 以指定的GBK编码转换成字符输入流 完美的解决了乱码问题
-
- BufferedReader br = new BufferedReader(isr);
- String line;
- while ((line = br.readLine()) != null){
- System.out.println(line);
- }
- }
-
- public static void main(String[] args) throws Exception {
- // 1、定义一个字节输出流
- OutputStream os = new FileOutputStream("io-app2/src/out03.txt");
-
- // 2、把原始的字节输出流转换成字符输出流
- // Writer osw = new OutputStreamWriter(os); // 以默认的UTF-8写字符出去 跟直接写FileWriter一样
- Writer osw = new OutputStreamWriter(os , "GBK"); // 指定GBK的方式写字符出去
-
- // 3、把低级的字符输出流包装成高级的缓冲字符输出流。
- BufferedWriter bw = new BufferedWriter(osw);
-
- bw.write("我爱中国1~~");
- bw.write("我爱中国2~~");
- bw.write("我爱中国3~~");
-
- bw.close();
- }
将对象写入到磁盘,或者从磁盘读取到内存。需要对象实现序列化
- public static void main(String[] args) throws Exception {
- // 1、创建学生对象
- Student s = new Student("陈磊", "chenlei","1314520", 21);
-
- // 2、对象序列化:使用对象字节输出流包装字节输出流管道
- ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("io-app2/src/obj.txt"));
-
- // 3、直接调用序列化方法
- oos.writeObject(s);
-
- // 4、释放资源
- oos.close();
- System.out.println("序列化完成了~~");
-
- }
-
- public static void main(String[] args) throws Exception {
- // 1、创建对象字节输入流管道包装低级的字节输入流管道
- ObjectInputStream is = new ObjectInputStream(new FileInputStream("io-app2/src/obj.txt"));
-
- // 2、调用对象字节输入流的反序列化方法
- Student s = (Student) is.readObject();
-
- System.out.println(s);
- }
序列化的版本需要和反序列时一致,否则报错
PrintStream & PrintWriter
作用:可以方便、高效打印数据到文件中去,例如直接打印整性,Boolean,都可以直接写入流
- public static void main(String[] args) throws Exception {
- // 1、创建一个打印流对象
- // PrintStream ps = new PrintStream(new FileOutputStream("io-app2/src/ps.txt"));
- // PrintStream ps = new PrintStream(new FileOutputStream("io-app2/src/ps.txt" , true)); // 追加数据,在低级管道后面加True
- // PrintStream ps = new PrintStream("io-app2/src/ps.txt" );
- PrintWriter ps = new PrintWriter("io-app2/src/ps.txt"); // 打印功能上与PrintStream的使用没有区别
-
- ps.println(97);
- ps.println('a');
- ps.println(23.3);
- ps.println(true);
- ps.println("我是打印流输出的,我是啥就打印啥");
-
- ps.close();
- }
- public static void main(String[] args) throws Exception {
- // 需求:使用Properties把键值对信息存入到属性文件中去。
- Properties properties = new Properties();
- properties.setProperty("admin", "123456");
- properties.setProperty("dlei", "003197");
- properties.setProperty("heima", "itcast");
- System.out.println(properties);
-
- /**
- 参数一:保存管道 字符输出流管道
- 参数二:保存心得
- */
- properties.store(new FileWriter("io-app2/src/users.properties")
- , "this is users!! i am very happy! give me 100!");
-
- }
-
- public static void main(String[] args) throws Exception {
- // 需求:Properties读取属性文件中的键值对信息。(读取)
- Properties properties = new Properties();
- System.out.println(properties);
-
- // 加载属性文件中的键值对数据到属性对象properties中去
- properties.load(new FileReader("io-app2/src/users.properties"));
-
- System.out.println(properties);
- String rs = properties.getProperty("dlei");
- System.out.println(rs);
- String rs1 = properties.getProperty("admin");
- System.out.println(rs1);
- }
简单的API说明
- public static void main(String[] args) throws Exception {
-
- // // 1.完成文件复制!
- IOUtils.copy(new FileInputStream("D:\\resources\\hushui.jpeg"),
- new FileOutputStream("D:\\resources\\hushui2.jpeg"));
-
-
- // // 2.完成文件复制到某个文件夹下!
- FileUtils.copyFileToDirectory(new File("D:\\resources\\hushui.jpeg"), new File("D:/"));
-
-
- // 3.完成文件夹复制到某个文件夹下--包含子目录文件!
- FileUtils.copyDirectoryToDirectory(new File("D:\\resources") , new File("D:\\new"));
- FileUtils.deleteDirectory(new File("D:\\new"));
-
- //删除文件夹包含子文件,目录
- FileUtils.deleteDirectory(new File("D:\\new"));
- }
更多API,可以参考