java之File

1、文件创建、重命名、删除

code:

 1 package com.test;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 
 6 public class File1 {
 7 
 8     public static void main(String[] args) {
 9         File file =  new File("new hello.txt"); 
10         if(file.exists()){//判断文件是否存在
11             
12             System.out.println(file.isFile());//判断是否为文件
13             System.out.println(file.isDirectory());//判断是否为路径(文件夹)
14             //file.delete();文件的删除
15             File nameto = new File("new hello.txt");
16             file.renameTo(nameto);//文件的重命名
17             
18             
19             
20             
21         }else{//如果文件不存在,则创建文件
22             System.out.println("文件不存在");
23             try {
24                 file.createNewFile();//创建文件
25                 System.out.println("文件已经成功创建");
26             } catch (IOException e) {
27                 
28                 System.out.println("文件无法被创建");
29             }
30         }
31         
32         
33     }
34 }

2、文件属性的读取

 1 package com.test;
 2 
 3 import java.io.File;
 4 
 5 public class ReadFile {
 6 
 7     public static void main(String[] args) {
 8         File file = new File("text.txt");
 9         //判断文件是否存在
10         System.out.println("判断文件是否存在 :"+file.exists());
11         //读取文件名称
12         System.out.println("读取文件名称: "+file.getName());
13         //读取文件路径
14         System.out.println("读取文件路径 :"+file.getPath());
15         //读取文件绝对路径
16         System.out.println("读取文件绝对路径: "+file.getAbsolutePath());
17         //读取文件父级路径
18         System.out.println("读取文件父级路径: "+new File(file.getAbsolutePath()).getParent());
19         //读取文件大小
20         System.out.println("读取文件大小: "+file.length()+"byte");//返回byte值
21         //判断文件是否被隐藏
22         System.out.println("判断文件是否被隐藏: "+file.isHidden());
23         //判断文件是否可读
24         System.out.println("判断文件是否可读: "+file.canRead());
25         //判断文件是否可写
26         System.out.println("判断文件是否可写 :"+file.canWrite());
27         //判断文件是否为文件夹
28         System.out.println("判断文件是否为文件夹 :"+file.isDirectory());
29     }
30 
31 }

3、文件属性的设置

 1 package com.test;
 2 
 3 import java.io.File;
 4 
 5 public class setFileProperty {
 6 
 7     public static void main(String[] args) {
 8         File file = new File("text.txt");
 9         //设置文件可写
10         file.setWritable(true);//true为可写,false为不可写
11         //设置文件可读
12         file.setReadable(false);//true为可读,false为不可读
13         //设置文件只读
14         file.setReadOnly();
15     }
16 }

4、文件的简单读写

 1 package com.test;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.BufferedWriter;
 5 import java.io.File;
 6 import java.io.FileInputStream;
 7 import java.io.FileNotFoundException;
 8 import java.io.FileOutputStream;
 9 import java.io.IOException;
10 import java.io.InputStreamReader;
11 import java.io.OutputStream;
12 import java.io.OutputStreamWriter;
13 import java.io.UnsupportedEncodingException;
14 
15 public class ReadWriteTextFile {
16 
17     public static void main(String[] args) {
18         
19         File file = new File("text.txt");//采用相对路径
20         
21         if(file.exists()){//判断文件是否存在
22             System.out.println("exits");
23             try {
24                 FileInputStream fis = new FileInputStream(file);
25                 InputStreamReader isr = new InputStreamReader(fis,"utf-8");
26                 BufferedReader br = new BufferedReader(isr);
27                 String line;
28                 while((line = br.readLine())!=null){
29                     System.out.println(line);
30                 }
31                 br.close();
32                 isr.close();
33                 fis.close();
34             } catch (FileNotFoundException e) {
35                 e.printStackTrace();
36             } catch (UnsupportedEncodingException e) {
37                 e.printStackTrace();
38             } catch (IOException e) {
39                 e.printStackTrace();
40             }
41             
42         }
43         File newfile = new File("newtext.txt");
44         try {
45             
46             FileOutputStream fos = new FileOutputStream(newfile);
47             OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
48             BufferedWriter bw = new BufferedWriter(osw);
49             
50             bw.write("abc
");
51             bw.write("def
");
52             bw.close();
53             osw.close();
54             fos.close();
55             System.out.println("写入完成");
56             
57         } catch (FileNotFoundException e) {
58             e.printStackTrace();
59         } catch (UnsupportedEncodingException e) {
60             e.printStackTrace();
61         } catch (IOException e) {
62             e.printStackTrace();
63         }
64         
65 
66     }
67 
68 }
原文地址:https://www.cnblogs.com/UniqueColor/p/5708546.html