File類簡(jiǎn)介
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); //這里就是重命名文件的操作,直接新建一個(gè)file對(duì)象然后使用renameTo方法可以重命名文件 } else { System.out.println( "文件不存在" ); try { file.createNewFile(); System.out.println( "文件已被創(chuàng)建" ); } catch (IOException e) { System.out.println( "文件無(wú)法創(chuàng)建" ); } } 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()) { //創(chuàng)建文件夾 判斷是否成功 System.out.println( "文件夾創(chuàng)建完成" ); File newfolder = new File( "myn new foleder - new" ); folder.renameTo(newfolder); //這里重命名了文件夾 文件夾的重命名是可以單獨(dú)更改一級(jí)的文件夾名的 而這一級(jí)下面的文件夾不變 保存目錄結(jié)構(gòu) if (folder.delete()) { System.out.print( "done" ); //這里的刪除只能刪除空文件夾,如果文件夾中有東西,那么則不能刪除,不問三七二十一直接刪除一個(gè)非空文件夾是非常不負(fù)責(zé)任的 } else { System.out.println( "fail" ); } } else { if (folder.exists()) { System.out.println( "文件夾已經(jīng)存在不用創(chuàng)建" ); } else { System.out.println( "文件夾創(chuàng)建失敗" ); } } File folders = new File( "my new folder/one/two/three/main" ); folders.mkdirs(); //在java中用mkdir只能創(chuàng)建一個(gè),mkdirs可以創(chuàng)建多級(jí)目錄 } } |
文件屬性設(shè)置
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為不同目錄結(jié)構(gòu)的縮進(jìn)量 if (dir.isDirectory()) { File next[] = dir.listFiles(); //判斷如果是目錄 則返回目錄所有的文件名數(shù)組用于遍歷文件夾 for ( int i = 0 ;i<next.length;i++) { //層次縮進(jìn)輸出 System.out.print( "---" ); } for ( int i = 0 ;i<next.length;i++) { //這里用了遞歸獲取目錄結(jié)構(gòu) System.out.println(next[i].getName()); if (next[i].isFile()) { printFiles(next[i],++tab); } } } } |
文件簡(jiǎn)單讀寫
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)) { //文件輸入流 這是字節(jié)流 InputStreamReader isr = new InputStreamReader(fis, "UTF-8" ); //inputstreamReader是一個(gè)字節(jié)流,將字節(jié)流和字符流轉(zhuǎn)化的時(shí)候,就需要制定一個(gè)編碼方式,不然就會(huì)亂碼 BufferedReader br = new BufferedReader(isr); //字符緩沖區(qū) String line; while ((line = br.readLine())!= null ){ //這里將緩沖區(qū)里的內(nèi)容如果非空就讀出來(lái)打印 System.out.println(line); } br.close(); //最后將各個(gè)線程關(guān)閉 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); //這里如果文件不存在會(huì)自動(dòng)創(chuàng)建文件 OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8" ); //和讀取一樣這里是轉(zhuǎn)化的是字節(jié)和字符流 BufferedWriter bw = new BufferedWriter(osw); //這里是寫入緩沖區(qū) bw.write( "厲害了我的哥" ); //寫入字符串 bw.close(); //和上面一樣 這里后打開的先關(guān)閉 先打開的后關(guān)閉 osw.close(); fos.close(); System.out.println( "done" ); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.jianshu.com/p/5770760f83c1#