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();
4

5 回答 5

1

更改代码以在 sdcard 上创建文件

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"/>
于 2012-12-03T11:25:43.930 回答
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,p; 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(); }

我想这会帮助你...

谢谢...

于 2012-12-04T03:42:57.923 回答
0

您需要通过将以下行添加到您的清单中,为您的应用程序提供写入 SD 卡的正确权限:

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

并检查http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory%28%29

于 2012-12-03T11:16:16.053 回答
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 消息应说明原因。

请注意,我还更正了您在选择变量名时所犯的样式错误。

于 2012-12-03T13:10:16.773 回答
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"/> 在清单文件中...

谢谢...

于 2012-12-03T11:31:42.230 回答