2012-12-03 129 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(); }

我认为这将有助于你...

谢谢...

相关问题