1. 程式人生 > >Java檔案、輸入流和輸出流常用操作

Java檔案、輸入流和輸出流常用操作

/**
 * 對檔案輸入流的處理
 */
   private static void InputStreamOps() {
           File file = new File("/home/hadoop/learnJava/Spark/JavaFile/Spark.txt");
           try{
                InputStream inputStream = new FileInputStream(file);//建立一個向指定 File 物件表示的檔案中寫入資料的檔案輸出流。如果第二個引數為 true,則將位元組寫入檔案末尾處,而不是寫入檔案開始處。建立一個新 FileDescriptor 物件來表示此檔案連線。
                byte[] content = new byte[1024];
                inputStream.read(content);
                System.out.println(new String(content));
                inputStream.close();
           }catch (Exception e){
               e.printStackTrace();
           }


    }

/**
 * 對檔案輸出流的處理
 */
private static void outputStreamOps() {
    File file = new File("/home/hadoop/learnJava/Spark/JavaFile/Spark.txt");
    try{
         OutputStream out  = new FileOutputStream(file,true);//建立一個向指定 File 物件表示的檔案中寫入資料的檔案輸出流。如果第二個引數為 true,則將位元組寫入檔案末尾處,而不是寫入檔案開始處。建立一個新 FileDescriptor 物件來表示此檔案連線。
         byte[] data = "Hello BigData".getBytes();
         out.write(data);
         out.close();
    }catch (IOException e){
        e.printStackTrace();
 }

}
/**
 * 隨機的讀取檔案
 */
private static void randomAccessFileOps() {
    File file = new File("/home/hadoop/learnJava/Spark/JavaFile/Spark.txt");
    try {
        RandomAccessFile rdf  = new RandomAccessFile(file,"rw");
        rdf.writeBytes("Scala\n");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

/**
 * 迴圈遍歷目錄下的所有檔案
 * @param path
 */
private static void listAllFiles(String path) {
    File file = new File(path);
    //如果下一級是目錄,接著迴圈遍歷
    if (file.isDirectory()){
        File[] f = file.listFiles();
        if (f != null){
            for (File item :f){
                listAllFiles(item.getAbsolutePath());
            }
        }
    }else{ //如果是檔案,則輸出檔案
        System.out.println(file);
    }
}

/**
 * 判斷是否是路徑
 */
private static void isDir() {
    File file = new File("/home/hadoop/learnJava");
    if (file.isDirectory()){
        System.out.println(file.getPath() + " is a directory!");
    }
}

/**
 * 列出指定資料夾下的檔案
 */
private static void listDir() {
    //指定要列出的檔案路徑
    File file = new File("/home/hadoop/learnJava");
    // 列出當前路徑下的所有檔案
   /* String[] files = file.list();
    for (String item :files){
        System.out.println(item);
    }*/
    //列出完整路徑的檔案
    File[] files = file.listFiles();

    for(File fileName:files){
        System.out.println(fileName);
    }
}
/**
 * 創建制定為檔案目錄
 */
private static void creatDir() {
    //指定要建立的檔案路徑
    File file = new File("/home/hadoop/learnJava/Spark/JavaFile");
    if(!file.exists()){
        file.mkdir();//建立檔案
        //file.mkdirs();//建立多級目錄
    }

}

/**
 * 刪除指定的檔案
 */
private static void deleteFile() {
    //指定要刪除的檔案
    File file  = new File("/home/hadoop/learnJava/Java");
    //刪除指定檔案
    if (file.exists()){
       file.delete();
    }
}
/**
 * 對檔案操作,建立不存在的檔案
 */
private static void creatFile() {
    //指定建立的檔案物件
    File file = new File("/home/hadoop/learnJava/Java.txt");
    //判斷要建立的檔案是否存在,如果存在刪除後在建立
    if (file.exists()) {
        file.delete();
    }
    //建立檔案
    try {
        file.createNewFile();
    }catch (IOException e){
        e.printStackTrace();
    }

}