字符编码:

 

 

1.FileReader与FileWriter

一次读取、写入多个字符

 1 package io.demo.chars;
 2 
 3 import java.io.FileReader;
 4 
 5 //使用FileReader读取文件,可读取英文也可以读取中文等各种文字
 6 public class filedemo01 {
 7     public static void main(String[] args) throws Exception {
 8         FileReader fileReader = new FileReader("/Users/yangasen/Downloads/hello.txt");
 9 //        int data = 0;
10 //        while ((data = fileReader.read()) != -1) {
11 //            System.out.print((char) data);
12 //        }
13         System.out.println("---------------------");
14 
15         char[] chars = new char[1024];
16         int count = 0;
17         while ((count = fileReader.read(chars)) != -1) {
18             System.out.print(new String(chars, 0, count));
19         }
20 
21         fileReader.close();
22     }
23 }
 1 package io.demo.chars;
 2 
 3 import java.io.FileWriter;
 4 
 5 //写入文件
 6 public class filedemo02 {
 7     public static void main(String[] args) throws Exception {
 8         FileWriter fileWriter = new FileWriter("/Users/yangasen/Downloads/hello2.txt");
 9         for (int i = 0; i < 10; i++) {
10             fileWriter.write("我2D天下无敌!\r\n");
11             fileWriter.flush();
12         }
13         fileWriter.close();
14     }
15 }
 1 package io.demo.chars;
 2 
 3 import java.io.FileReader;
 4 import java.io.FileWriter;
 5 
 6 //复制文件,只能复制文本文件
 7 public class filedemo03 {
 8     public static void main(String[] args) throws Exception {
 9         FileReader fileReader = new FileReader("/Users/yangasen/Downloads/hello2.txt");
10         FileWriter fileWriter = new FileWriter("/Users/yangasen/Downloads/hello3.txt");
11         int data = 0;
12         while ((data = fileReader.read()) != -1) {
13             fileWriter.write(data);
14         }
15         fileReader.close();
16         fileWriter.close();
17     }
18 }

 

2.BufferedReader与BufferedWriter

高效读写,支持输入换行符,可一次写一行、读一行。

 1 package io.demo.chars;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.FileReader;
 5 
 6 //BufferedReader读取文件
 7 public class buffereddemo01 {
 8     public static void main(String[] args) throws Exception {
 9         FileReader fileReader = new FileReader("/Users/yangasen/Downloads/hello2.txt");
10         BufferedReader bufferedReader = new BufferedReader(fileReader);
11         String str = "";
12         while ((str = bufferedReader.readLine()) != null) {
13             System.out.println(str);
14         }
15         bufferedReader.close();
16     }
17 }
 1 package io.demo.chars;
 2 
 3 import java.io.BufferedWriter;
 4 import java.io.FileWriter;
 5 import java.io.IOException;
 6 
 7 //BufferedWriter
 8 public class buffereddemo02 {
 9     public static void main(String[] args) throws IOException {
10         FileWriter fileWriter = new FileWriter("/Users/yangasen/Downloads/hello4.txt");
11         BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
12         for (int i = 0; i < 10; i++) {
13             bufferedWriter.write("莫非朕不知兵?");
14             bufferedWriter.newLine();
15         }
16         bufferedWriter.close();
17 
18     }
19 }

 

3.PrintWriter

 1 package io.demo.chars;
 2 
 3 import java.io.PrintWriter;
 4 
 5 public class printdemo {
 6     public static void main(String[] args) throws Exception {
 7         PrintWriter printWriter = new PrintWriter("/Users/yangasen/Downloads/print.txt");
 8         printWriter.println(97);
 9 
10         printWriter.println(true);
11 
12         printWriter.println(3.14);
13 
14         printWriter.println('x');
15 
16         printWriter.close();
17     }
18 }

 

4.InputStreamReader与OutputStreamWriter

转换流:可以将字节流转换为字符流,可设置字符编码方式。

 1 package io.demo.chars;
 2 
 3 
 4 import java.io.FileInputStream;
 5 import java.io.InputStreamReader;
 6 
 7 public class streamdemo01 {
 8     public static void main(String[] args) throws Exception {
 9         FileInputStream fileInputStream = new FileInputStream("/Users/yangasen/Downloads/hello3.txt");
10         InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8");
11         int data = 0;
12         while ((data = inputStreamReader.read()) != -1) {
13             System.out.print((char) data);
14         }
15         inputStreamReader.close();
16     }
17 }
 1 package io.demo.chars;
 2 
 3 import java.io.FileOutputStream;
 4 import java.io.OutputStreamWriter;
 5 
 6 public class streamdemo02 {
 7     public static void main(String[] args) throws Exception {
 8         FileOutputStream fileOutputStream = new FileOutputStream("/Users/yangasen/Downloads/hello5.txt");
 9         OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "utf-8");
10         for (int i = 0; i < 10; i++) {
11             outputStreamWriter.write("君不见董卓,丁原乎?\r\n");
12         }
13         outputStreamWriter.close();
14     }
15 }

 

5.File类

 

 

 1 /*
 2  * File类的使用
 3  * (1)分隔符
 4  * (2)文件操作
 5  * (3)文件夹操作
 6  */
 7 public class demo04 {
 8     public static void main(String[] args) throws Exception {
 9         //separator();
10         
11         //fileOpe();
12         
13         directoryOpe();
14     }
15     
16     //分隔符
17     public static void separator() {
18         System.out.println("路径分隔符"+File.pathSeparator);//Windows下是:  ;
19         System.out.println("名称分隔符"+File.separator);//Windows下是:  /
20     }
21     
22     //文件操作
23     public static void  fileOpe() throws Exception {
24         //1创建文件
25         File file = new File("d:\\file.txt");
26         if(!file.exists()) {
27             boolean b = file.createNewFile();        
28             System.out.println(file.toString()+"创建结果:"+b);
29         }else {
30             System.out.println(file.toString()+"文件已存在");
31         }
32         
33         
34         //2删除文件
35         //2.1直接删除
36         //System.out.println("删除结果:"+file.delete());
37         
38         //2.2由JVM退出时删除
39         //file.deleteOnExit();
40         //Thread.sleep(2000);
41         
42         
43         //3获取文件信息
44         System.out.println("绝对路径:"+file.getAbsolutePath());
45         System.out.println("路径:"+file.getPath());
46         System.out.println("文件名称:"+file.getName());
47         System.out.println("父级目录:"+file.getParent());
48         System.out.println("文件长度:"+file.length());
49         System.out.println("文件最后修改时间:"+new Date(file.lastModified()).toLocaleString());
50         
51                 
52         //4判断
53         System.out.println("文件执行权限:"+file.canExecute());
54         System.out.println("是否是文件:"+file.isFile());
55         System.out.println("是否是隐藏文件:"+file.isHidden());
56     }
57     
58     //文件夹操作
59     public static void directoryOpe() throws Exception {
60         //1创建文件夹
61         File dir = new File("d:\\aaa\\bbb\\ccc");
62         if(!dir.exists()) {
63             //dir.mkdir();//创建单级目录
64             dir.mkdirs();//创建多级目录
65             System.out.println(dir.toString()+"已创建成功");
66         }else {
67             System.out.println(dir.toString()+"目录已存在");
68         }
69         
70         //2删除文件夹
71         //2.1直接删除,目录必须为空才能删除
72         //System.out.println("删除结果:"+dir.delete());
73         
74         
75         //2.2JVM退出时删除
76         //dir.deleteOnExit();
77         //Thread.sleep(2000);
78                 
79         
80         //3获取文件夹信息
81         System.out.println("文件夹绝对路径:"+dir.getAbsolutePath());
82         System.out.println("文件夹路径:"+dir.getPath());
83         System.out.println("文件夹名称:"+dir.getName());
84         System.out.println("获取父目录:"+dir.getParent());
85         System.out.println("获取最后修改时间:"+new Date(dir.lastModified()).toLocaleString());
86         
87         //4判断
88         System.out.println("是否是文件夹:"+dir.isDirectory());
89         System.out.println("是否是隐藏文件夹:"+dir.isHidden());
90         
91         
92         //5遍历文件夹
93         System.out.println("遍历文件夹");
94         File dir2 = new File("d:\\que\\BGM\\");
95         for(String str : dir2.list()) {
96             System.out.println(str);
97         }
98     }
99 }
 1 public class demo05 {
 2     public static void main(String[] args) throws Exception {
 3         fileFilterOpe();
 4     }
 5     
 6     //演示FileFileter类的使用
 7     public static void fileFilterOpe() throws Exception {                
 8         File dir2 = new File("d:\\que\\BGM\\");
 9         for(String str : dir2.list()) {
10             System.out.println(str);
11         }
12         
13         //过滤,只包含.mp3结尾的文件
14         File[] files = dir2.listFiles(new FileFilter() {            
15             @Override
16             public boolean accept(File pathname) {
17                 if(pathname.getName().endsWith(".mp3")) {
18                     return true;
19                 }
20                 return false;
21             }
22         });
23         for(File f:files) {
24             System.out.println(f.getName());
25         }
26     }
27 }

 

递归遍历和删除文件夹:

 1 //递归遍历和删除文件夹
 2 public class demo06 {
 3     public static void main(String[] args) throws Exception {
 4         listDir(new File("d:\\que"));
 5 
 6         // deleteDir(new File("d:\\que"));
 7     }
 8 
 9     // 递归遍历
10     public static void listDir(File dir) throws Exception {
11         File[] files = dir.listFiles();
12         if (files != null && files.length > 0) {
13             for (File file : files) {
14                 if (file.isDirectory()) {
15                     listDir(file);
16                 } else {
17                     System.out.println(file.getAbsolutePath());
18                 }
19             }
20         }
21     }
22 
23     // 递归删除
24     public static void deleteDir(File dir) throws Exception {
25         File[] files = dir.listFiles();
26         if (files != null && files.length > 0) {
27             for (File file : files) {
28                 if (file.isDirectory()) {
29                     deleteDir(file);
30                 } else {
31                     if (file.exists()) {
32                         System.out.println(file.getAbsolutePath() + "删除:" + file.delete());
33                     }
34                 }
35             }
36         }
37     }
38 }

 

Properties的使用:

 1 //演示Properties的使用
 2 public class demo07 {
 3     public static void main(String[] args) throws Exception {
 4         Properties properties = new Properties();
 5         properties.setProperty("username", "zhangsan");
 6         properties.setProperty("age", "24");
 7         System.out.println(properties.toString());
 8                 
 9         //遍历
10         Set<String> pronameSet = properties.stringPropertyNames();
11         for (String string : pronameSet) {
12             System.out.println(string);
13         }
14         
15         //与流相关的方法
16         //1.list()
17         PrintWriter pWriter = new PrintWriter("d:\\print.txt");
18         properties.list(pWriter);
19         pWriter.close();
20         
21         //2.store()
22         FileOutputStream fileOutputStream=new FileOutputStream("d:\\store.properties");
23         properties.store(fileOutputStream, "comment");
24         fileOutputStream.close();
25         
26         //3.load()
27         Properties properties2 = new Properties();
28         FileInputStream fileInputStream = new FileInputStream("d:\\store.properties");
29         properties2.load(fileInputStream);
30         fileInputStream.close();
31         System.out.println(properties2.toString());        
32     }    
33 }

 

posted on 2020-12-19 14:51  Sempron2800+  阅读(113)  评论(0编辑  收藏  举报