在指定目录创建文件

3
尝试在特定目录创建文件,但出现了文件未找到的错误。为什么呢?我使用了不可能的路径吗?我真的不知道,但代码应该是可以工作的。
    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();
5个回答

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

目前你也缺少文件名的扩展名,所以请将字符串 zn 改为 zn="/zn.txt";

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

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

如果文件夹已经存在,返回已翻译的文本和文件夹路径。 - ρяσѕρєя K
如果我的路径全都是两个字符串,比如: dirName = root + 这里应该放上:File.separator + day; - ilbets
@user1872357:day是目录还是文件? - ρяσѕρєя K
@user1872357:你可以将日期作为SD卡上的文件夹来使用,例如 File file1 = new File(root+ File.separator + day + File.separator + File_name); 或者 File file1 = new File(root+ "/" + day + "/" + File_name); - ρяσѕρєя K
日 - 是文件夹,因此文件必须为:root/day/zn.txt - ilbets

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 Auto-generated catch block e.printStackTrace(); }

我认为这会对你有所帮助...

谢谢...


0

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"/>

谢谢...


String 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); 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(); - ilbets
我想在目录root/day中创建文件zn.txt。 - ilbets
日 - 是文件夹 因此文件必须是:根/日/zn.txt - ilbets

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消息应解释原因。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接