【问题标题】:Create file in specified directory在指定目录创建文件
【发布时间】:2012-11-20 20:24:06
【问题描述】:

尝试在特定目录中创建文件,但它显示错误 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();

【问题讨论】:

    标签: java android eclipse outputstream fileoutputstream


    【解决方案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();
          }
    } 
    

    在当前您还缺少带有文件名的文件扩展名,因此将字符串 zn 更改为 zn="/zn.txt";

    并确保您已在AndroidManifest.xml 中添加了 SD 卡权限:

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

    【讨论】:

    • 是的,如果文件夹已经退出,还添加文件夹路径
    • 所以,如果我的所有路径都是 2 个字符串,例如 dirName = root + 我应该放在这里:File.separator +day ;
    • @user1872357 : day 是什么目录或文件?
    • @user1872357 :您可以将日期视为 SD 卡上的文件夹 File file1 = new File(root+ File.separator + day + File.separator + File_name);File file1 = new File(root+ "/" + day + "/" + File_name);
    • day - 是文件夹,所以文件必须是:root/day/zn.txt
    【解决方案2】:

    首先你创建一个目录

    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(); } 捕获(异常 e){ // TODO 自动生成的 catch 块 e.printStackTrace(); }

    我想这会对你有所帮助...

    谢谢...

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

        【解决方案4】:
        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();
                    }
                }
        

        &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/&gt; 在清单文件上...

        谢谢...

        【讨论】:

        • File_path=root+File.separator+day;文件 f_dir = 新文件(文件路径); f_dir.mkdirs();文件 file1 = new File(f_dir,File_name); if(!file1.exists()) { 尝试 { file1.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } 尝试 { OutputStream fos= new FileOutputStream(file1);字符串 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();
        • 我想在目录中创建文件 zn.txt : root/day
        • day - 是文件夹,所以文件必须是:root/day/zn.txt
        【解决方案5】:

        这是你最近的尝试:

        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. 如果 anything 存在且具有对象给出的路径名,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-11-28
          • 2016-08-04
          • 2021-12-21
          • 1970-01-01
          • 2011-07-07
          • 2021-01-13
          相关资源
          最近更新 更多