欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java編程中最基礎的文件和目錄操作方法詳解

 更新時間:2015年11月17日 08:46:50   作者:小李飛刀8  
這篇文章主要介紹了Java編程中最基礎的文件和目錄操作方法詳解,是Java入門學習中的基礎知識,需要的朋友可以參考下

文件操作

平常經(jīng)常使用JAVA對文件進行讀寫等操作,這里匯總一下常用的文件操作。

1、創(chuàng)建文件

public static boolean createFile(String filePath){ 
  boolean result = false; 
  File file = new File(filePath); 
  if(!file.exists()){ 
    try { 
      result = file.createNewFile(); 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } 
  } 
   
  return result; 
} 

2、創(chuàng)建文件夾

public static boolean createDirectory(String directory){ 
  boolean result = false; 
  File file = new File(directory); 
  if(!file.exists()){ 
    result = file.mkdirs(); 
  } 
   
  return result; 
} 

3、刪除文件

public static boolean deleteFile(String filePath){ 
  boolean result = false; 
  File file = new File(filePath); 
  if(file.exists() && file.isFile()){ 
    result = file.delete(); 
  } 
   
  return result; 
} 

4、刪除文件夾

遞歸刪除文件夾下面的子文件和文件夾

public static void deleteDirectory(String filePath){ 
  File file = new File(filePath); 
  if(!file.exists()){ 
    return; 
  } 
   
  if(file.isFile()){ 
    file.delete(); 
  }else if(file.isDirectory()){ 
    File[] files = file.listFiles(); 
    for (File myfile : files) { 
      deleteDirectory(filePath + "/" + myfile.getName()); 
    } 
     
    file.delete(); 
  } 
} 

5、讀文件

(1)以字節(jié)為單位讀取文件,常用于讀二進制文件,如圖片、聲音、影像等文件

public static String readFileByBytes(String filePath){ 
  File file = new File(filePath); 
  if(!file.exists() || !file.isFile()){ 
    return null; 
  } 
   
  StringBuffer content = new StringBuffer(); 
   
  try { 
    byte[] temp = new byte[1024]; 
    FileInputStream fileInputStream = new FileInputStream(file); 
    while(fileInputStream.read(temp) != -1){ 
      content.append(new String(temp)); 
      temp = new byte[1024]; 
    } 
     
    fileInputStream.close(); 
  } catch (FileNotFoundException e) { 
    e.printStackTrace(); 
  } catch (IOException e) { 
    e.printStackTrace(); 
  } 
   
  return content.toString(); 
} 

 (2)以字符為單位讀取文件,常用于讀文本,數(shù)字等類型的文件,支持讀取中文

public static String readFileByChars(String filePath){ 
  File file = new File(filePath); 
  if(!file.exists() || !file.isFile()){ 
    return null; 
  } 
   
  StringBuffer content = new StringBuffer(); 
  try { 
    char[] temp = new char[1024]; 
    FileInputStream fileInputStream = new FileInputStream(file); 
    InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "GBK"); 
    while(inputStreamReader.read(temp) != -1){ 
      content.append(new String(temp)); 
      temp = new char[1024]; 
    } 
     
    fileInputStream.close(); 
    inputStreamReader.close(); 
  } catch (FileNotFoundException e) { 
    e.printStackTrace(); 
  } catch (IOException e) { 
    e.printStackTrace(); 
  } 
   
  return content.toString(); 
} 

(3)以行為單位讀取文件,常用于讀面向行的格式化文件

public static List<String> readFileByLines(String filePath){ 
  File file = new File(filePath); 
  if(!file.exists() || !file.isFile()){ 
    return null; 
  } 
   
  List<String> content = new ArrayList<String>(); 
  try { 
    FileInputStream fileInputStream = new FileInputStream(file); 
    InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "GBK"); 
    BufferedReader reader = new BufferedReader(inputStreamReader); 
    String lineContent = ""; 
    while ((lineContent = reader.readLine()) != null) { 
      content.add(lineContent); 
      System.out.println(lineContent); 
    } 
     
    fileInputStream.close(); 
    inputStreamReader.close(); 
    reader.close(); 
  } catch (FileNotFoundException e) { 
    e.printStackTrace(); 
  } catch (IOException e) { 
    e.printStackTrace(); 
  } 
   
  return content; 
} 

6、寫文件

字符串寫入文件的幾個類中,F(xiàn)ileWriter效率最高,BufferedOutputStream次之,F(xiàn)ileOutputStream最差。

(1)通過FileOutputStream寫入文件

public static void writeFileByFileOutputStream(String filePath, String content) throws IOException{ 
  File file = new File(filePath); 
  synchronized (file) { 
    FileOutputStream fos = new FileOutputStream(filePath); 
    fos.write(content.getBytes("GBK")); 
    fos.close(); 
  } 
} 

(2)通過BufferedOutputStream寫入文件

public static void writeFileByBufferedOutputStream(String filePath, String content) throws IOException{ 
  File file = new File(filePath); 
  synchronized (file) { 
    BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(filePath)); 
    fos.write(content.getBytes("GBK")); 
    fos.flush(); 
    fos.close(); 
  } 
} 

(3)通過FileWriter將字符串寫入文件

public static void writeFileByFileWriter(String filePath, String content) throws IOException{ 
    File file = new File(filePath); 
    synchronized (file) { 
      FileWriter fw = new FileWriter(filePath); 
      fw.write(content); 
      fw.close(); 
    } 
  } 

目錄操作
目錄是一個文件可以包含其他文件和目錄的列表。你想要在目錄中列出可用文件列表,可以通過使用 File 對象創(chuàng)建目錄,獲得完整詳細的能在 File 對象中調(diào)用的以及有關目錄的方法列表。

創(chuàng)建目錄
這里有兩個有用的文件方法,能夠創(chuàng)建目錄:

mkdir( ) 方法創(chuàng)建了一個目錄,成功返回 true ,創(chuàng)建失敗返回 false。失敗情況是指文件對象的路徑已經(jīng)存在了,或者無法創(chuàng)建目錄,因為整個路徑不存在。
mkdirs( ) 方法創(chuàng)建一個目錄和它的上級目錄。
以下示例創(chuàng)建 “/ tmp / user / java / bin” 目錄:

import java.io.File;

public class CreateDir {
  public static void main(String args[]) {
   String dirname = "/tmp/user/java/bin";
   File d = new File(dirname);
   // Create directory now.
   d.mkdirs();
 }
}

編譯并執(zhí)行以上代碼創(chuàng)建 “/ tmp /user/ java / bin”。

提示:Java 自動按 UNIX 和 Windows 約定來處理路徑分隔符。如果在 Windows 版本的 Java 中使用正斜杠(/),仍然可以得到正確的路徑。

目錄列表
如下,你能夠用 File 對象提供的 list() 方法來列出目錄中所有可用的文件和目錄

import java.io.File;

public class ReadDir {
  public static void main(String[] args) {

   File file = null;
   String[] paths;

   try{   
     // create new file object
     file = new File("/tmp");

     // array of files and directory
     paths = file.list();

     // for each name in the path array
     for(String path:paths)
     {
      // prints filename and directory name
      System.out.println(path);
     }
   }catch(Exception e){
     // if any error occurs
     e.printStackTrace();
   }
  }
}

基于你/ tmp目錄下可用的目錄和文件,將產(chǎn)生以下結(jié)果:

test1.txt
test2.txt
ReadDir.java
ReadDir.class

相關文章

  • java實現(xiàn)快速排序圖文詳解

    java實現(xiàn)快速排序圖文詳解

    網(wǎng)上關于快速排序的算法原理和算法實現(xiàn)都比較多,不過java是實現(xiàn)并不多,而且部分實現(xiàn)很難理解,和思路有點不搭調(diào)。所以整理了這篇文章。如果有不妥之處還請建議
    2021-08-08
  • 如何使用Java生成具有安全哈希的QR碼

    如何使用Java生成具有安全哈希的QR碼

    這篇文章主要介紹了如何使用Java生成具有安全哈希的QR碼,這是關于如何在Java中使用salt生成QR代碼和安全散列字符串的分步教程。,需要的朋友可以參考下
    2019-06-06
  • Spring Boot如何使用HikariCP連接池詳解

    Spring Boot如何使用HikariCP連接池詳解

    這篇文章主要給大家介紹了關于Spring Boot如何使用HikariCP連接池的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者使用springboot具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-03-03
  • 識別率很高的java文字識別技術(shù)

    識別率很高的java文字識別技術(shù)

    這篇文章主要為大家詳細介紹了識別率很高的java文字識別技術(shù),親測,希望對大家有幫助,感興趣的小伙伴們可以參考一下
    2016-08-08
  • java stream中Collectors的用法實例精講

    java stream中Collectors的用法實例精講

    這篇文章主要為大家介紹了java stream中Collectors的用法實例精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • Java設計模式之java命令模式詳解

    Java設計模式之java命令模式詳解

    這篇文章主要介紹了Java設計模式編程中命令模式的使用,在一些處理請求響應的場合經(jīng)??梢杂玫矫钅J降木幊趟悸?需要的朋友可以參考下
    2021-09-09
  • Apache Commons Math3學習之數(shù)值積分實例代碼

    Apache Commons Math3學習之數(shù)值積分實例代碼

    這篇文章主要介紹了Apache Commons Math3學習之數(shù)值積分實例代碼,涉及使用辛普森積分的例子,這里分享給大家,供需要的朋友參考。
    2017-10-10
  • SpringBoot之Java配置的實現(xiàn)

    SpringBoot之Java配置的實現(xiàn)

    這篇文章主要介紹了SpringBoot之Java配置的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • Java數(shù)據(jù)類型的全面剖析

    Java數(shù)據(jù)類型的全面剖析

    這篇文章主要介紹了Java基本數(shù)據(jù)類型,結(jié)合實例形式詳細分析了java基本數(shù)據(jù)類型、數(shù)據(jù)類型范圍、易錯題等相關原理與操作技巧,需要的朋友可以參考下
    2021-10-10
  • Java如何接收前端easyui?datagrid傳遞的數(shù)組參數(shù)

    Java如何接收前端easyui?datagrid傳遞的數(shù)組參數(shù)

    這篇文章分享一下怎么在easyui的datagrid刷新表格時,在后端java代碼中接收datagrid傳遞的數(shù)組參數(shù),本文通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧
    2023-11-11

最新評論