2012-12-03 104 views
3

嘗試在特定目錄中創建文件,但它顯示錯誤FileNotFound。爲什麼? 我使用不可能的路徑嗎?我真的不知道,但似乎代碼應該工作。在指定目錄中創建文件

String day=/1; 
String zn="/zn"; 
    File_name=zn 
String root= Environment.getExternalStorageDirectory().toString();    
    File_path=root+day; 

     File file1 = new File(File_path,File_name); 
     file1.mkdirs(); 
     if(!file1.exists()) { 
      try { 
       file1.createNewFile(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 


     try { 
      OutputStream fos= new FileOutputStream(file1); 
      String l,d,p; 
      l = lessnum.getText().toString(); 
      d = desc.getText().toString(); 
      p = place.getText().toString(); 

      fos.write(l.getBytes()); 
      fos.write(d.getBytes()); 
      fos.write(p.getBytes()); 

      fos.close(); 

回答

1

更改您的代碼用於在SD卡上創建文件

String root= Environment.getExternalStorageDirectory().getAbsolutePath(); 
String File_name = "File_name.Any_file_Extension(like txt,png etc)"; 


File file1 = new File(root+ File.separator + File_name); 
if(!file1.exists()) { 
    try { 
     file1.createNewFile(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
} 

在當前你你是人與文件名,以便丟失的文件擴展名所以更改字符串znzn="/zn.txt";

,並確保你已經添加sd卡的權限在AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
+0

是,也可以添加與它的文件夾路徑,如果文件夾已經退出 –

+0

看到我的編輯答案 –

+0

所以如果我所有的路徑是2 strigs像 dirName =根+我應該把這裏:File.separator +天; – CVS

0
String root= Environment.getExternalStorageDirectory().toString();  
    String dirName = 
     root+ "abc/123/xy"; 
     File newFile = new File(dirName); 
     newFile.mkdirs(); 

     String testFile = "test.txt"; 
     File file1 = new File(dirName,testFile); 
     if(!file1.exists()){ 
      try { 
       file1.createNewFile(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

而且和<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>清單文件 ...

謝謝...

+0

File_path = root + File.separator +天; \t \t \t文件f_dir = new File(File_path); \t \t \t f_dir.mkdirs(); \t \t \t \t \t \t文件file1 =新文件(f_dir,File_name); \t \t \t if(!file1。存在()){ \t \t \t \t嘗試{ \t \t \t \t \t file1.createNewFile(); \t \t \t \t}趕上(IOException的發送){ \t \t \t \t \t e.printStackTrace(); \t \t \t \t} \t \t \t} \t \t \t \t \t \t \t \t \t嘗試{ \t \t \t \t的OutputStream FOS =新FileOutputStream中(文件1); \t \t \t \t String l,d,p; \t \t \t \t l = lessnum.getText()。toString(); \t \t \t \t d = desc.getText()。toString(); \t \t \t \t p = place.getText()。toString(); \t \t \t \t \t \t \t \t fos.write(l.getBytes()); \t \t \t \t fos.write(d.getBytes()); \t \t \t \t fos.write(p.getBytes()); \t \t \t \t \t \t \t \t fos.close(); – CVS

+0

完成一切仍然不起作用 – CVS

+0

你想創建一個文件或文件夾(目錄)? – user4232

0

這是您的最新消息嘗試:

File_path = root + File.separator + day; 
File f_dir = new File(File_path); 
f_dir.mkdirs(); 
File file1 = new File(f_dir, File_name); 
if (!file1.exists()) { 
    try { 
     file1.createNewFile(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
try { 
    OutputStream fos= new FileOutputStream(file1); 

如果您向我們展示了完整堆棧跟蹤和錯誤信息會更容易弄清楚是怎麼回事錯的,但我能想到幾個可能性:

  1. 你沒有檢查由f_dir.mkdirs()返回的值,並且很可能返回false以指示目錄路徑未被創建。這可能意味着:
    • 該目錄已存在。
    • 存在的東西,但它不是一個目錄。
    • 目錄路徑的某些部分無法創建...出於多種可能原因之一。
  2. file1.exists()調用將返回true如果什麼與對象因爲路徑存在。事實存在並不一定意味着你可以打開它來寫作:
    • 它可能是一個目錄。
    • 它可能是應用程序沒有寫入權限的文件。
    • 它可能是隻讀文件系統上的文件。
    • 還有其他一些事情。

如果我在寫這一點,我會寫這樣的事:

File dir = new File(new File(root), day); 
if (!dir.exists()) { 
    if (!dir.mkdirs()) { 
     System.err.println("Cannot create directories"); 
     return; 
    } 
} 
File file1 = new File(dir, fileName); 
try (OutputStream fos= new FileOutputStream(file1)) { 
    ... 
} catch (FileNotFoundException ex) { 
    System.err.println("Cannot open file: " + ex.getMessage()); 
} 

我只是嘗試,如果需要創建目錄...,並檢查創造成功。 然後我只是試圖打開文件寫入它。如果文件不存在,它將被創建。如果它不能被創建,那麼FileNotFoundException消息應該解釋原因。

請注意,我也更正了您在選擇變量名稱時所做的樣式錯誤。

+0

Okey我會在稍後嘗試它,不過無論如何謝謝 – CVS

1

首先你要的目錄

String root= Environment.getExternalStorageDirectory().toString();  
String dirName = 
    root+ "abc/123/xy"; 
    File newFile = new File(dirName); 
    newFile.mkdirs(); 

然後創建該目錄中的文件

String testFile = "test.txt"; File file1 = new File(dirName,testFile); if(!file1.exists()){ try { file1.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

然後做你的文件的寫入操作

try { OutputStream fos= new FileOutputStream(file1); 

字符串L,d頁; l = lessnum.getText()。toString(); d = desc.getText()。toString(); p = place.getText()。toString(); os.write(l.getBytes()); fos.write(d.getBytes()); fos.write(p.getBytes()); fos.close(); } catch(Exception e){TODO自動生成的catch塊 e.printStackTrace(); }

我認爲這將有助於你...

謝謝...

相關問題