天天看點

JAVA的File對象

檔案

1.File對象

java封裝的一個操作檔案及檔案夾(目錄)的對象。可以操作磁盤上的任何一個檔案和檔案夾。

2.建立檔案

方式一:根據路徑建構一個File對象new File(path)

<pre style="margin: 0px; padding: 0px; transition-duration: 0.2s; transition-property: background-color, border-color, border-radius, padding-top, padding-bottom, margin-top, margin-bottom, color, opacity; overflow: auto; font-family: "Courier New", serif; font-size: 12px; overflow-wrap: break-word;">//方式一
 @Test public void create01(){ try {
            String path = URLDecoder.decode("D:\\部落格園\\wjj1.txt","UTF-8");//解決中文亂碼,轉UTF-8
            File file = new File(path);
            file.createNewFile();
            System.out.println("建立成功01");
        } catch (UnsupportedEncodingException e) {//decode方法需要抛異常或捕獲異常
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }      

方式二:根據父目錄檔案和子目錄路徑建構一個File對象new File(File,Spath)

<pre style="margin: 0px; padding: 0px; transition-duration: 0.2s; transition-property: background-color, border-color, border-radius, padding-top, padding-bottom, margin-top, margin-bottom, color, opacity; overflow: auto; font-family: "Courier New", serif; font-size: 12px; overflow-wrap: break-word;">//方式二
    @Test public void create02(){
        String path = null; try {
            path = URLDecoder.decode("D:\\部落格園","UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        File parentFile = new File(path);//父目錄檔案
        String fileName = "wjj2.txt";//子路徑
        File file = new File(parentFile, fileName); try {
            file.createNewFile();
            System.out.println("建立成功02");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }      

方式三:根據父目錄路徑和子目錄路徑建構一個File對象new File(Fpath,Spath)

<pre style="margin: 0px; padding: 0px; transition-duration: 0.2s; transition-property: background-color, border-color, border-radius, padding-top, padding-bottom, margin-top, margin-bottom, color, opacity; overflow: auto; font-family: "Courier New", serif; font-size: 12px; overflow-wrap: break-word;">//方式三
 @Test public void create03() throws Exception{//抛異常
        String path = URLDecoder.decode("D:\\部落格園","UTF-8");
        String filePath = "wjj3.txt";
        File file = new File(path, filePath);
        file.createNewFile();
        System.out.println("建立成功03");
    }      

運作結果:

JAVA的File對象

3.檔案的相關操作

檔案的路徑相關和判斷功能的構造方法

<pre style="margin: 0px; padding: 0px; transition-duration: 0.2s; transition-property: background-color, border-color, border-radius, padding-top, padding-bottom, margin-top, margin-bottom, color, opacity; overflow: auto; font-family: "Courier New", serif; font-size: 12px; overflow-wrap: break-word;"> @Test public void info() throws Exception{ //建立檔案對象
        String path = URLDecoder.decode("D:\\部落格園\\wjj1.txt","UTF-8");
        File file = new File(path);
        System.out.println("檔案名:"+file.getName());
        System.out.println("檔案絕對路徑:"+file.getAbsolutePath());
        System.out.println("檔案父目錄:"+file.getParent());
        System.out.println("檔案大小(位元組):"+file.length());
        System.out.println("檔案是否存在:"+file.exists());
        System.out.println("是否是檔案:"+file.isFile());
        System.out.println("是否是目錄:"+file.isDirectory());
    }      

UTF-8一個英文一個位元組,一個漢字三個位元組

運作結果:

JAVA的File對象

檔案删除操作的構造方法

<pre style="margin: 0px; padding: 0px; transition-duration: 0.2s; transition-property: background-color, border-color, border-radius, padding-top, padding-bottom, margin-top, margin-bottom, color, opacity; overflow: auto; font-family: "Courier New", serif; font-size: 12px; overflow-wrap: break-word;"> @Test public void fileDelete() throws Exception{
        String path = URLDecoder.decode("D:\\部落格園\\wjj1.txt","UTF-8");
        File file = new File(path); if (file.exists()){ if (file.delete()){
                System.out.println(path+"删除成功");
            }else {
                System.out.println(path+"删除失敗");
            }
        }else {
            System.out.println("檔案不存在");
        }
    }      
<pre style="margin: 0px; padding: 0px; transition-duration: 0.2s; transition-property: background-color, border-color, border-radius, padding-top, padding-bottom, margin-top, margin-bottom, color, opacity; overflow: auto; font-family: "Courier New", serif; font-size: 12px; overflow-wrap: break-word;"> @Test public void isMkdir() throws Exception{
        String path = URLDecoder.decode("D:\\部落格園\\wjj1","UTF-8");
        File file = new File(path); if (file.exists()){
            System.out.println(path+"該目錄已存在");
        }else { if (file.mkdirs()){
                System.out.println("建立成功");
            }else {
                System.out.println("建立失敗");
            }
        }
    }