13-本地文件操作

一.文件的增删改查

View Code

二.文件夹的增删改查

View Code

三.文件属性的读取

 

四.文件属性设置

五.遍历文件夹

六.文件的简单读写

 1 package com.example;
 2 
 3 
 4 import java.io.BufferedReader;
 5 import java.io.BufferedWriter;
 6 import java.io.File;
 7 import java.io.FileInputStream;
 8 import java.io.FileNotFoundException;
 9 import java.io.FileOutputStream;
10 import java.io.IOException;
11 import java.io.InputStreamReader;
12 import java.io.OutputStreamWriter;
13 import java.io.UnsupportedEncodingException;
14 
15 public class MyClass {
16     public static void main(String []args){
17         File testFile = new File("testFile");
18         if (testFile.exists()){
19             try {
20                 FileInputStream fis = new FileInputStream(testFile);
21                 InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
22                 BufferedReader br = new BufferedReader(isr);
23 
24                 String line;
25                 while ((line = br.readLine())!= null){
26                     System.out.println(line);
27                 }
28 
29                 br.close();
30                 isr.close();
31                 fis.close();
32 
33             } catch (FileNotFoundException e) {
34                 e.printStackTrace();
35             } catch (UnsupportedEncodingException e) {
36                 e.printStackTrace();
37             } catch (IOException e) {
38                 e.printStackTrace();
39             }
40 
41         }
42         else {
43             try {
44                 testFile.createNewFile();
45             } catch (IOException e) {
46                 e.printStackTrace();
47             }
48             FileOutputStream fos = null;
49             try {
50                 fos = new FileOutputStream(testFile);
51                 OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
52                 BufferedWriter bw = new BufferedWriter(osw);
53 
54                 bw.write("h\n");
55                 bw.write("e\n");
56                 bw.write("l\n");
57                 bw.write("l\n");
58                 bw.write("o\n");
59 
60                 bw.close();
61                 osw.close();
62                 fos.close();
63             } catch (FileNotFoundException e) {
64                 e.printStackTrace();
65             } catch (UnsupportedEncodingException e) {
66                 e.printStackTrace();
67             } catch (IOException e) {
68                 e.printStackTrace();
69             }
70 
71 
72         }
73     }
74 }
View Code

 

posted @ 2017-02-06 21:25  894316728  阅读(161)  评论(0编辑  收藏  举报