File類簡介
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package com.file; import java.io.File; import java.io.IOException; /** * Created by elijahliu on 2017/2/10. */ public class filetest { public static void main(String[] args) { File file = new File( "hello.txt" ); //是否存在 if (file.exists()) { //文件 System.out.println(file.isFile()); //路徑(文件夾) System.out.println(file.isDirectory()); File nameto = new File( "new Hello.txt" ); file.renameTo(nameto); //這里就是重命名文件的操作,直接新建一個file對象然后使用renameTo方法可以重命名文件 } else { System.out.println( "文件不存在" ); try { file.createNewFile(); System.out.println( "文件已被創建" ); } catch (IOException e) { System.out.println( "文件無法創建" ); } } if (file.exists()) { //刪除文件 file.delete(); System.out.println( "刪除文件" ); } else { } } } |
文件夾操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package com.file; import java.io.File; /** * Created by elijahliu on 2017/2/11. */ public class HelloFolder { public static void main(String[] args) { File folder = new File( "my new folder" ); if (folder.mkdir()) { //創建文件夾 判斷是否成功 System.out.println( "文件夾創建完成" ); File newfolder = new File( "myn new foleder - new" ); folder.renameTo(newfolder); //這里重命名了文件夾 文件夾的重命名是可以單獨更改一級的文件夾名的 而這一級下面的文件夾不變 保存目錄結構 if (folder.delete()) { System.out.print( "done" ); //這里的刪除只能刪除空文件夾,如果文件夾中有東西,那么則不能刪除,不問三七二十一直接刪除一個非空文件夾是非常不負責任的 } else { System.out.println( "fail" ); } } else { if (folder.exists()) { System.out.println( "文件夾已經存在不用創建" ); } else { System.out.println( "文件夾創建失敗" ); } } File folders = new File( "my new folder/one/two/three/main" ); folders.mkdirs(); //在java中用mkdir只能創建一個,mkdirs可以創建多級目錄 } } |
文件屬性設置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package com.file; import java.io.File; /** * Created by elijahliu on 2017/2/11. */ public class SetFileProperty { public static void main(String[] args){ File file = new File( "test.file" ); if (file.exists()){ file.setWritable( true ); //可寫 file.setReadable( true ); //可讀 file.setReadOnly(); //只讀 } } } |
遍歷文件夾
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public void printFiles(File dir, int tab) { //tab為不同目錄結構的縮進量 if (dir.isDirectory()) { File next[] = dir.listFiles(); //判斷如果是目錄 則返回目錄所有的文件名數組用于遍歷文件夾 for ( int i = 0 ;i<next.length;i++) { //層次縮進輸出 System.out.print( "---" ); } for ( int i = 0 ;i<next.length;i++) { //這里用了遞歸獲取目錄結構 System.out.println(next[i].getName()); if (next[i].isFile()) { printFiles(next[i],++tab); } } } } |
文件簡單讀寫
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
package com.file; import java.io.*; /** * Created by elijahliu on 2017/2/11. */ public class ReadFile { public static void main(String[] args) { File file = new File( "new Hello.txt" ); if (file.exists()){ System.err.print( "exsit" ); try (FileInputStream fis = new FileInputStream(file)) { //文件輸入流 這是字節流 InputStreamReader isr = new InputStreamReader(fis, "UTF-8" ); //inputstreamReader是一個字節流,將字節流和字符流轉化的時候,就需要制定一個編碼方式,不然就會亂碼 BufferedReader br = new BufferedReader(isr); //字符緩沖區 String line; while ((line = br.readLine())!= null ){ //這里將緩沖區里的內容如果非空就讀出來打印 System.out.println(line); } br.close(); //最后將各個線程關閉 isr.close(); fis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } File newfile = new File( "newtext.txt" ); try { FileOutputStream fos = new FileOutputStream(newfile); //這里如果文件不存在會自動創建文件 OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8" ); //和讀取一樣這里是轉化的是字節和字符流 BufferedWriter bw = new BufferedWriter(osw); //這里是寫入緩沖區 bw.write( "厲害了我的哥" ); //寫入字符串 bw.close(); //和上面一樣 這里后打開的先關閉 先打開的后關閉 osw.close(); fos.close(); System.out.println( "done" ); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.jianshu.com/p/5770760f83c1#