java創建目錄,java創建目錄並寫入文件

本文目錄一覽:

如何用JAVA代碼創建一個文件夾?

File類裏面有兩個方法可以實現:\x0d\x0a一個是mkdir():創建此抽象路徑名指定的目錄。\x0d\x0a另外一個是mkdirs(): 創建此抽象路徑名指定的目錄,包括所有必需但不存在的父目錄。\x0d\x0a\x0d\x0a比如你想在A文件夾創建一個B文件夾,並在B文件夾下創建c和D文件夾,可以用下面的代碼實現:\x0d\x0a\x0d\x0aimport java.io.File;\x0d\x0a\x0d\x0apublic class Test {\x0d\x0a public static void main(String args[]) {\x0d\x0a File file = new File(“D:\\A\\B\\C”);\x0d\x0a file.mkdirs();\x0d\x0a \x0d\x0a file = new File(“D:\\A\\B\\D”);\x0d\x0a file.mkdir();\x0d\x0a }\x0d\x0a}

java常用命令位於哪個目錄

java常用命令位於創建目錄。計算機中的常用命令dir—-用來查看當前目錄下的所有的子目錄和子文件的tree—查看當前目錄下的樹狀目錄結構mkdir—-創建目錄—md創建目錄123:md123創建它的子目錄:md123\456則會在123目錄下生成它的子目錄456)rmdir—刪除目錄rd—是從計算機中徹底移除。如果當前目錄中有子文件或者子目錄,會刪除失敗del—-刪除文件。

如何用java程序在當前目錄下創建一個子目錄

用java程序在當前目錄下創建一個子目錄的方法是利用File對象的mkdirs方法。

完整代碼如下:

// 獲取當前圖片的路徑

String path = createImages.getAbsolutePath() + “/Images”;

//創建文件對象f,根據path路徑

File f = new File(path);

//如果當前不是一個目錄就進入if

if (!f.isDirectory()) {

boolean success = f.mkdirs(); //創建一個目錄

if (success) { //成功打印當前的路徑

System.out.println(“Created path: ” + f.getPath());

} else { //失敗的情況

System.out.println(“Could not create path: ” + f.getPath());

}

} else {

System.out.println(“Path exists: ” + f.getPath()); //子目錄已存在。

}

關於mkdir:

mkdir()創建此抽象路徑名稱指定的目錄(及只能創建一級的目錄,且需要存在父目錄),如果傳入的path是多級路徑,需要使用mkdirs()創建。

java如何在當前文件下創建目錄?

可以直接創建文件時用相對路徑,如:

File dir = new File(“aaa/bbb”);

dir.mkdirs();

這樣創建的目錄就是在當前目錄下。

如果要指定絕對路徑可以獲取當前class文件的路徑:

test.class.getResource(“”).getPath();

java項目目錄(src/main/java; src/main/resources)怎麼建立?

eclipse新建maven  webapp後無法添加src/main/java和src/main/test

1、右鍵,New新建項目,選擇Maven Project

2、Filter輸入web快速定位到maven-archetype-webapp,選中,再點擊next

3、填寫Group Id、Artifact Id

4、Finish完成之後,若發現Maven項目只顯示src/main/resources目錄

5、顯示完全的src/main/java、src/main/resources、src/test/java目錄

5.1、Eclipse-windowpreferences-java-compiler-選擇本地要用的Java版本

5.2、Eclipse-WindowPreferences-Java-Installed JREs-修改本地默認jdk

5.3、選中項目,右鍵-Build Path-Configure Build Path-點擊選項卡Libraries-選中JRE System Library-點擊edit

5.4、選中默認的Workspace default JRE (jdk1.8)

5.5、點擊finish,點擊ok,自動出現完全的src/main/java、src/main/resources、src/test/java目錄

最後,選中項目,右鍵-Maven-Update Projects

參考資料

百度知道.百度知道[引用時間2018-1-21]

JAVA 如何創建/刪除/修改/複製目錄及文件

import java.io.*;

public class FileOperate { 

  public FileOperate() { 

  }

  /** 

   * 新建目錄 

   * @param folderPath String 如 c:/fqf 

   * @return boolean 

   */ 

  public void newFolder(String folderPath) { 

    try { 

      String filePath = folderPath; 

      filePath = filePath.toString(); 

      java.io.File myFilePath = new java.io.File(filePath); 

      if (!myFilePath.exists()) { 

        myFilePath.mkdir(); 

      } 

    } 

    catch (Exception e) { 

      System.out.println(“新建目錄操作出錯”); 

      e.printStackTrace(); 

    } 

  }

  /** 

   * 新建文件 

   * @param filePathAndName String 文件路徑及名稱 如c:/fqf.txt 

   * @param fileContent String 文件內容 

   * @return boolean 

   */ 

  public void newFile(String filePathAndName, String fileContent) {

    try { 

      String filePath = filePathAndName; 

      filePath = filePath.toString(); 

      File myFilePath = new File(filePath); 

      if (!myFilePath.exists()) { 

        myFilePath.createNewFile(); 

      } 

      FileWriter resultFile = new FileWriter(myFilePath); 

      PrintWriter myFile = new PrintWriter(resultFile); 

      String strContent = fileContent; 

      myFile.println(strContent); 

      resultFile.close();

    } 

    catch (Exception e) { 

      System.out.println(“新建目錄操作出錯”); 

      e.printStackTrace();

    }

  }

  /** 

   * 刪除文件 

   * @param filePathAndName String 文件路徑及名稱 如c:/fqf.txt 

   * @param fileContent String 

   * @return boolean 

   */ 

  public void delFile(String filePathAndName) { 

    try { 

      String filePath = filePathAndName; 

      filePath = filePath.toString(); 

      java.io.File myDelFile = new java.io.File(filePath); 

      myDelFile.delete();

    } 

    catch (Exception e) { 

      System.out.println(“刪除文件操作出錯”); 

      e.printStackTrace();

    }

  }

  /** 

   * 刪除文件夾 

   * @param filePathAndName String 文件夾路徑及名稱 如c:/fqf 

   * @param fileContent String 

   * @return boolean 

   */ 

  public void delFolder(String folderPath) { 

    try { 

      delAllFile(folderPath); //刪除完裏面所有內容 

      String filePath = folderPath; 

      filePath = filePath.toString(); 

      java.io.File myFilePath = new java.io.File(filePath); 

      myFilePath.delete(); //刪除空文件夾

    } 

    catch (Exception e) { 

      System.out.println(“刪除文件夾操作出錯”); 

      e.printStackTrace();

    }

  }

  /** 

   * 刪除文件夾裏面的所有文件 

   * @param path String 文件夾路徑 如 c:/fqf 

   */ 

  public void delAllFile(String path) { 

    File file = new File(path); 

    if (!file.exists()) { 

      return; 

    } 

    if (!file.isDirectory()) { 

      return; 

    } 

    String[] tempList = file.list(); 

    File temp = null; 

    for (int i = 0; i  tempList.length; i++) { 

      if (path.endsWith(File.separator)) { 

        temp = new File(path + tempList[i]); 

      } 

      else { 

        temp = new File(path + File.separator + tempList[i]); 

      } 

      if (temp.isFile()) { 

        temp.delete(); 

      } 

      if (temp.isDirectory()) { 

        delAllFile(path+”/”+ tempList[i]);//先刪除文件夾裏面的文件 

        delFolder(path+”/”+ tempList[i]);//再刪除空文件夾 

      } 

    } 

  }

  /** 

   * 複製單個文件 

   * @param oldPath String 原文件路徑 如:c:/fqf.txt 

   * @param newPath String 複製後路徑 如:f:/fqf.txt 

   * @return boolean 

   */ 

  public void copyFile(String oldPath, String newPath) { 

    try { 

      int bytesum = 0; 

      int byteread = 0; 

      File oldfile = new File(oldPath); 

      if (oldfile.exists()) { //文件存在時 

        InputStream inStream = new FileInputStream(oldPath); //讀入原文件 

        FileOutputStream fs = new FileOutputStream(newPath); 

        byte[] buffer = new byte[1444]; 

        int length; 

        while ( (byteread = inStream.read(buffer)) != -1) { 

          bytesum += byteread; //位元組數 文件大小 

          System.out.println(bytesum); 

          fs.write(buffer, 0, byteread); 

        } 

        inStream.close(); 

      } 

    } 

    catch (Exception e) { 

      System.out.println(“複製單個文件操作出錯”); 

      e.printStackTrace();

    }

  }

  /** 

   * 複製整個文件夾內容 

   * @param oldPath String 原文件路徑 如:c:/fqf 

   * @param newPath String 複製後路徑 如:f:/fqf/ff 

   * @return boolean 

   */ 

  public void copyFolder(String oldPath, String newPath) {

    try { 

      (new File(newPath)).mkdirs(); //如果文件夾不存在 則建立新文件夾 

      File a=new File(oldPath); 

      String[] file=a.list(); 

      File temp=null; 

      for (int i = 0; i  file.length; i++) { 

        if(oldPath.endsWith(File.separator)){ 

          temp=new File(oldPath+file[i]); 

        } 

        else{ 

          temp=new File(oldPath+File.separator+file[i]); 

        }

        if(temp.isFile()){ 

          FileInputStream input = new FileInputStream(temp); 

          FileOutputStream output = new FileOutputStream(newPath + “/” + 

              (temp.getName()).toString()); 

          byte[] b = new byte[1024 * 5]; 

          int len; 

          while ( (len = input.read(b)) != -1) { 

            output.write(b, 0, len); 

          } 

          output.flush(); 

          output.close(); 

          input.close(); 

        } 

        if(temp.isDirectory()){//如果是子文件夾 

          copyFolder(oldPath+”/”+file[i],newPath+”/”+file[i]); 

        } 

      } 

    } 

    catch (Exception e) { 

      System.out.println(“複製整個文件夾內容操作出錯”); 

      e.printStackTrace();

    }

  }

  /** 

   * 移動文件到指定目錄 

   * @param oldPath String 如:c:/fqf.txt 

   * @param newPath String 如:d:/fqf.txt 

   */ 

  public void moveFile(String oldPath, String newPath) { 

    copyFile(oldPath, newPath); 

    delFile(oldPath);

  }

  /** 

   * 移動文件到指定目錄 

   * @param oldPath String 如:c:/fqf.txt 

   * @param newPath String 如:d:/fqf.txt 

   */ 

  public void moveFolder(String oldPath, String newPath) { 

    copyFolder(oldPath, newPath); 

    delFolder(oldPath);

  } 

}

原創文章,作者:WPYC,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/144802.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
WPYC的頭像WPYC
上一篇 2024-10-26 11:52
下一篇 2024-10-26 11:52

相關推薦

發表回復

登錄後才能評論