導航:首頁 > 編程語言 > java路徑不存在創建

java路徑不存在創建

發布時間:2024-06-08 14:33:21

java怎樣創建不存在文件夾下的文件 file

import java.io.File;
import java.io.IOException;

public class Admin {

public static void main(String... args) {
String path0 = "D:/aa/bb/cc/";
String path1 = "D:/aa/bb/cc/kkk.java";

File f = new File(path0);

// 創建文件夾
if (!f.exists()) {
f.mkdirs();
}

f = new File(path1);

// 創建文件
if (!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

② java.io.FileNotFoundException: (系統找不到指定的路徑。)怎麼處理

  1. 可以上代碼嗎?

  2. 這個異常是Java常見的IO異常,這個異常導致的是文件路徑找不到。

  3. 通常你可以使用try{}catch(FileNotFoundException e){……}來捕獲異常。

  4. 可以找到具體的文件進行讀取,而不是到文件夾。

  5. 也可以判斷路徑是否存在,如果不存在則mkdir.

  6. 最後給你個例子

  7. publicstaticbooleancreateDir(StringdestDirName){
    Filedir=newFile(destDirName);
    if(dir.exists()){
    System.out.println("創建目錄"+destDirName+"失敗,目標目錄已經存在");
    returnfalse;
    }
    if(!destDirName.endsWith(File.separator)){
    destDirName=destDirName+File.separator;
    }
    //創建目錄
    if(dir.mkdirs()){
    System.out.println("創建目錄"+destDirName+"成功!");
    returntrue;
    }else{
    System.out.println("創建目錄"+destDirName+"失敗!");
    returnfalse;
    }

③ java判斷文件是否存在

java判斷來文件是否自存在:

1、判斷文件是否存在,不存在創建文件

④ 用java創建文件時錯誤,請問是怎麼回事

需要先【存在】文件夾,才能再創建文件。
當然,如果文件夾【不存在】,那麼就需要先創建文件夾,再創建文件
比如 : c盤已經存在,所以才能創建C盤下的文件

1
2

File file = new File("c:\\abc.txt");
file.createNewFile();//創建文件

File file = new File("c:\\test\\abc.txt");
file.createNewFile();//創建文件

如果文件夾不存在會出現異常

Exception in thread "main" java.io.IOException: 系統找不到指定的路徑。
at java.io.WinNTFileSystem.createFileExclusively

解決辦法,先創建文件夾,在創建文件

File file = new File("c:\\test\\abc.txt");
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();//創建"c:\\test"文件夾
}
file.createNewFile();//創建c:\\test\\abc.txt文件

⑤ 文件和文件夾的建立 JAVA

第一個問題:
File file =new File(String pathname);只是在內存中創建了一個File實例。pathname可以是路徑也可以是文件。然後調用file.createNewFile();才會創建文件。所以不是樓主是所說的ile has never been read。
而執行PrintWriter out = new PrintWriter(file);後發現創建了文件,這是由於PrintWriter構造方法執行機制所決定的,如果文件為空,則會自動掉用file的createNewFile()方法創建一個文件(詳見api PrintWriter)。所以樓主又看到了所創建的文件。異常用try catch捕獲FileNotFoundException。
第二個問題:創建多級目錄要用file.mkdirs();你那個只能創建一級目錄。

我寫個例子 經過檢驗的 你參考一下 多級目錄的文件創建:
import java.io.*;
public class FileTest{
public File fileCreate(String fileFoder, String fileName){
File foder = new File(fileFoder);
File file = new File(fileFoder+fileName);
//如果文件夾不存在,則創建文件夾
if(foder.exists()==false){
foder.mkdirs();//多級目錄
//foder.mkdir();//只創建一級目錄
}

//如果文件不存在,則創建文件
if(file.exists()==false){
try{
file.createNewFile();
}catch(IOException e){
e.printStackTrace();
}
}
return file;
}
public static void main(String [] args){
FileTest fileTest= new FileTest();
String fileFoder="D://test1//test2";
String fileName="//test_io.txt";

File file= fileTest.fileCreate(fileFoder,fileName);
System.out.println(file.getPath());
}
}

至於文件讀寫的內容很多 給你個參考的鏈接
http://www.jb51.net/article/16396.htm

⑥ 請問Java的File類,創建文件時,報異常制定文件路徑不存在的原因

去看看E盤下to1這個文件夾有沒有,沒有就建一個。
或者在代碼里創建文件的父目錄。

閱讀全文

與java路徑不存在創建相關的資料

熱點內容
iphone4下拉通知欄 瀏覽:427
javasocket接收數據包 瀏覽:137
如何給類數據成員變數設置默認值 瀏覽:885
密集app怎麼推廣 瀏覽:866
WP7屏幕保護程序 瀏覽:568
win10雙硬碟老藍屏 瀏覽:986
怎麼設置網站描述 瀏覽:274
微信壓縮包可以發送多大文件 瀏覽:916
u盤打開很卡無法復制裡面文件 瀏覽:143
app怎麼刪不掉 瀏覽:324
農村工作一號文件圖片 瀏覽:598
錘子手機怎麼樣升級 瀏覽:706
微信朋友圈哲理說說 瀏覽:649
jscss模板 瀏覽:472
圖片文件夾里多了一個 瀏覽:305
索尼a7r2配置文件 瀏覽:706
獲取jsp中a中text 瀏覽:396
電腦word文件怎麼找回來 瀏覽:846
測量網路圖 瀏覽:596
為什麼高考報名網站點不進去 瀏覽:386

友情鏈接