1. 程式人生 > 資訊 >與亞馬遜、迪士尼爭奪市場份額,Netflix 在印度大幅降價,降幅最高達 60%

與亞馬遜、迪士尼爭奪市場份額,Netflix 在印度大幅降價,降幅最高達 60%

常用的檔案物件相關構造器和方法

》》相關方法

1)newFile(Stringpathname)//根據路徑構建一個file物件

2)newFile(Fileparent,String child)//根據父目錄檔案+子路徑

3)newFile(Stringparent,Stringchild)//根據父目錄+子路徑構建

createNewFile建立新檔案

  //方式一new File(String pathname)
    public static void create01(){
        String filePath = "e:\\txt1.txt";
        File file = new File(filePath);
        try {
            file.createNewFile();
            System.out.println("建立成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //方式二new File(File parent,String child)
    public static void create02(){
        File parentFile = new File("e:\\");
        String fileName = "txt2.txt";
        File file = new File(parentFile,fileName);
        try {
            file.createNewFile();
            System.out.println("建立成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //方式三new File(String parent,String child)
    public static void create03(){
        String parent = "e:\\";
        String fileName = "txt3.txt";
        File file = new File(parent,fileName);
        try {
            file.createNewFile();
            System.out.println("建立成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

獲取檔案的相關資訊

getName、getAbsolutePath、getParent、length、exists、isFile、isDirectory

  public static void info(){
        File file = new File("e:\\txt1.txt");
        //獲取檔名
        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());//T
        //是不是一個檔案
        System.out.println("是不是一個檔案="+file.isFile());//T
        //是不是一個目錄
        System.out.println("是不是一個檔案="+file.isDirectory());//F
    }

目錄的操作和檔案刪除

mkdir建立一級目錄。mkdirs建立多級目錄、delete刪除空目錄和或檔案

JavaIO流原理

1)I/O是input/Output的縮寫,I/O技術非常使用的技術,使用者處理資料傳輸。如讀/寫檔案,網路通訊等

2)Java程式中,對於資料的輸入/輸出操作以"流(stream)"的方式進行。

3)java.io包提供了各種“流”類和介面,用以獲取不用型別的資料,並通過方法輸入或輸出資料

4)輸入input:讀取外部資料(磁碟、光碟等儲存裝置的資料)到程式(記憶體)中

5)輸出output:將程式(記憶體)資料輸出到磁碟、光碟等儲存裝置中

流的分類

按操作資料單位不同非為:位元組流(8bit),字元流(按字元)

按資料流的流向不同分為:輸入流,輸出流

按流的角色的不同分為:位元組流,處理流/包裝流

1)java的io流共涉及40多個類,實際上非常規則,都是從如上四個抽象基類派生的。

2)由這四個類派生出來的子類名稱都是以其父類名作為子類名字尾