Java IO流 (1)------保存文件與讀取文件


1、寫入讀取封裝步驟

/*
 * 1.寫入
 *  1)指定文件路徑以及文件名
 *  2)判斷文件夾以及文件是否存在
 *  3)創建文件以及文件夾
 *  4)輸出流
 *  5)寫入
 *  6)轉換
 * 2.讀取
 *  1)指定路徑
 *  2)判斷文件路徑是否存在
 *  3)判斷是否是文件名
 *  4)字符流
 *  5)read出來的是單個單個的字符,用StringBuilder來拼接
 *  6)while循環
 * 3.封裝
 *  1)new對象
 *  2)set裝載
*/

 

2、創建

 1 public static File createNote(String type, String fileName) {
 2     //new一個File類,並傳入路徑
 3     File file = new File(CommanPath.PATH.getValue());
 4 
 5     //寫入需要判斷文件夾是否存在,不存在則創建,讀取不需要
 6     if (!"read".equals(type)) {
 7         //判斷文件夾是否存在,不存在則先創建
 8         if (!file.exists()) {
 9             file.mkdirs();
10         }
11     }
12     //在new一個file類,傳入路徑以及文件名
13     File file1 = new File(CommanPath.PATH.getValue(), fileName);
14     if (!"read".equals(type)) {
15         //判斷文件是否存在,不存在則先創建
16         try {
17             if (!file1.isFile()) {
18                 file1.createNewFile();
19             }
20         } catch (IOException e) {
21             e.printStackTrace();
22         }
23     }
24 
25     return file1;
26 }

 

3、寫入

 1 /**
 2  * 寫入
 3  */
 4 private void writeMessage() {
 5     File file = IoUtil.createNote("write", Constant.MESSAGE_NAME);
 6     try {
 7         OutputStream outputStream = new FileOutputStream(file);
 8         IoMessageModel ioMessageModel = new IoMessageModel();
 9         outputStream.write(ioMessageModel.getMessageDescription().getBytes());
10         outputStream.close();
11     } catch (IOException e) {
12         e.printStackTrace();
13     }
14 }

 

4、讀取

 1 /**
 2  * 讀取記事本
 3  *
 4  * @return 字符流數據
 5  */
 6 public static String readNote(String fileName) {
 7     //找到文件地址以及文件名
 8     File file = createNote("read", fileName);
 9     /*
10         String 不可變字符串,字符串拼接時效率低,浪費內存
11         StringBuffer 可變字符串,線程安全,多線程操作
12         StringBuilder 可變字符串,線程不安全,單線程操作,速度最快。在操作可變字符串時一般用StringBuilder,
13         如果考慮線程安全則必須使用StringBuffer
14      */
15     StringBuilder result = new StringBuilder();
16     try {
17         /*
18          * 字節流,會出現亂碼
19          */
20        /* InputStream inputStream = new FileInputStream(file);
21         int data;
22         while ((data=inputStream.read())>0){
23             System.out.print((char) data);
24         }*/
25         /*
26          * 字符流
27          */
28         Reader reader = new FileReader(file);
29         int data;
30         while ((data = reader.read()) > 0) {
31             result.append((char) data);
32         }
33     } catch (IOException e) {
34         e.printStackTrace();
35     }
36     //將StringBuiler轉換為String
37     return result.toString();
38 }

 

5、枚舉類

 1 /**
 2  * @author liangd
 3  * date 2020-10-13 12:57
 4  * code 枚舉類
 5  */
 6 public enum CommanPath {
 7     //標識符
 8     AA("@#%"),
 9     //定義文件路徑
10     PATH("E:\\liangd\\note");
11 
12     private String value;
13 
14     //構造方法
15     CommanPath(String value) {
16         this.value = value;
17     }
18 
19     public String getValue() {
20         return value;
21     }
22 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM