最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

文件操作

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

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é)為單位讀取文件,常用于讀二進(jìn)制文件,如圖片、聲音、影像等文件

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、寫文件

字符串寫入文件的幾個(gè)類中,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(); 
    } 
  } 

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

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

mkdir( ) 方法創(chuàng)建了一個(gè)目錄,成功返回 true ,創(chuàng)建失敗返回 false。失敗情況是指文件對(duì)象的路徑已經(jīng)存在了,或者無法創(chuàng)建目錄,因?yàn)檎麄€(gè)路徑不存在。
mkdirs( ) 方法創(chuàng)建一個(gè)目錄和它的上級(jí)目錄。
以下示例創(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 自動(dòng)按 UNIX 和 Windows 約定來處理路徑分隔符。如果在 Windows 版本的 Java 中使用正斜杠(/),仍然可以得到正確的路徑。

目錄列表
如下,你能夠用 File 對(duì)象提供的 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

相關(guān)文章

  • Java實(shí)現(xiàn)順序表和鏈表結(jié)構(gòu)

    Java實(shí)現(xiàn)順序表和鏈表結(jié)構(gòu)

    大家好,本篇文章主要講的是Java實(shí)現(xiàn)順序表和鏈表結(jié)構(gòu),感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-02-02
  • Java詳解聊天窗口的創(chuàng)建流程

    Java詳解聊天窗口的創(chuàng)建流程

    這篇文章主要介紹了怎么用Java來創(chuàng)建一個(gè)聊天窗口,聊天軟件我們經(jīng)常要用,但是你有想過自己怎么去實(shí)現(xiàn)它嗎,感興趣的朋友跟隨文章往下看看吧
    2022-04-04
  • 利用反射實(shí)現(xiàn)Excel和CSV 轉(zhuǎn)換為Java對(duì)象功能

    利用反射實(shí)現(xiàn)Excel和CSV 轉(zhuǎn)換為Java對(duì)象功能

    將Excel或CSV文件轉(zhuǎn)換為Java對(duì)象(POJO)以及將Java對(duì)象轉(zhuǎn)換為Excel或CSV文件可能是一個(gè)復(fù)雜的過程,但如果使用正確的工具和技術(shù),這個(gè)過程就會(huì)變得十分簡(jiǎn)單,在本文中,我們將了解如何利用一個(gè)Java反射的庫來實(shí)現(xiàn)這個(gè)功能,需要的朋友可以參考下
    2023-11-11
  • Windows下java、javaw、javaws以及jvm.dll等進(jìn)程的區(qū)別

    Windows下java、javaw、javaws以及jvm.dll等進(jìn)程的區(qū)別

    這篇文章主要介紹了Windows下java、javaw、javaws以及jvm.dll等進(jìn)程的區(qū)別,本文分別講解了它們的作用并給出代碼實(shí)例,最后做出了區(qū)別總結(jié),需要的朋友可以參考下
    2015-03-03
  • JavaAgent原理及實(shí)踐分享

    JavaAgent原理及實(shí)踐分享

    這篇文章主要介紹了JavaAgent原理及實(shí)踐,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • Android中比較常見的Java super關(guān)鍵字

    Android中比較常見的Java super關(guān)鍵字

    這篇文章主要為大家介紹了Android中比較常見的Java super關(guān)鍵字,具有一定的學(xué)習(xí)參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-01-01
  • java設(shè)計(jì)模式原型模式與享元模式調(diào)優(yōu)系統(tǒng)性能詳解

    java設(shè)計(jì)模式原型模式與享元模式調(diào)優(yōu)系統(tǒng)性能詳解

    這篇文章主要為大家介紹了java設(shè)計(jì)模式原型模式與享元模式調(diào)優(yōu)系統(tǒng)性能方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • 帶你了解Java的類和對(duì)象

    帶你了解Java的類和對(duì)象

    下面小編就為大家?guī)硪黄胬斫釰ava類和對(duì)象。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2021-09-09
  • 詳解SpringMVC中的@RequestMapping注解

    詳解SpringMVC中的@RequestMapping注解

    這篇文章主要介紹了SpringMVC中@RequestMapping注解,@RequestMapping注解是一個(gè)用來處理請(qǐng)求地址映射的注解,可用于映射一個(gè)請(qǐng)求或一個(gè)方法,可以用在類或方法上,需要的朋友可以參考下
    2023-07-07
  • 解決Java Calendar類set()方法的陷阱

    解決Java Calendar類set()方法的陷阱

    這篇文章主要介紹了解決Java Calendar類set()方法的陷阱,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03

最新評(píng)論

左贡县| 芒康县| 桓仁| 西峡县| 安国市| 治多县| 全椒县| 绥滨县| 遂昌县| 泽州县| 隆林| 吉林市| 忻州市| 寿光市| 阳原县| 东明县| 佛山市| 广安市| 东至县| 白河县| 宁远县| 南和县| 临高县| 长岛县| 若羌县| 常山县| 宁强县| 连州市| 镇平县| 永吉县| 鹤峰县| 二连浩特市| 新干县| 海原县| 原阳县| 精河县| 绥滨县| 阳西县| 古浪县| 芜湖市| 高唐县|