Java 在指定目录中创建文件

Java 在指定目录中创建文件,java,android,eclipse,outputstream,fileoutputstream,Java,Android,Eclipse,Outputstream,Fileoutputstream,尝试在特定目录中创建文件,但显示错误FileNotFound。为什么? 我在使用不可能的路径吗?我真的不知道,但似乎代码应该能正常工作 String day=/1; String zn="/zn"; File_name=zn String root= Environment.getExternalStorageDirectory().toString(); File_path=root+day; File file1 = new

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

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

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


并选中

将代码更改为在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"/>

在清单文件上


谢谢…

以下是您的最新尝试:

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); 
如果您向我们展示了完整的stacktrace和错误消息,我们将更容易找出问题所在,但我可以想到以下几种可能性:

  • 您没有检查由
    f_dir.mkdirs()
    返回的值,它很可能返回
    false
    ,以指示未创建目录路径。这可能意味着:
    • 该目录已存在
    • 有些东西存在,但它不是目录
    • 无法创建目录路径的某些部分。。。原因有很多
  • 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消息应解释原因


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

    首先创建一个目录

    String root= Environment.getExternalStorageDirectory().toString();      
     String dirName =
         root+ "abc/123/xy"; 
         File newFile =  new File(dirName);
         newFile.mkdirs();
    
    然后在该目录中创建一个文件

    String root= Environment.getExternalStorageDirectory().toString();      
     String dirName =
         root+ "abc/123/xy"; 
         File newFile =  new File(dirName);
         newFile.mkdirs();
    
    String testFile=“test.txt”;
    File file1=新文件(dirName,testFile);
    如果(!file1.exists()){
    试一试{
    file1.createNewFile();
    }捕获(IOE异常){
    //TODO自动生成的捕捉块
    e、 printStackTrace();
    }
    }

    然后执行文件写入操作

    try { OutputStream fos= new FileOutputStream(file1);
    
    字符串l、d、p; l=lessum.getText().toString(); d=desc.getText().toString(); p=place.getText().toString(); 写(l.getBytes()); fos.write(d.getBytes()); fos.write(p.getBytes()); fos.close(); } 捕获(例外e){ //TODO自动生成的捕捉块 e、 printStackTrace(); }

    我想这会帮助你


    谢谢…

    是的,如果文件夹已经存在,并且我的所有路径都是两行,比如dirName=root+我应该放在这里吗?请添加文件夹路径:File.separator+day@user1872357:what is
    day
    它是dir还是file?@user1872357:您可以像day是SD卡上的文件夹一样使用
    file file1=新文件(root+file.separator+day+file.separator+file\u name)
    文件file1=新文件(root+“/”+day+“/”+文件名)日期-是文件夹,所以文件必须是:root/day/zn.txtFile_path=root+file.separator+day;文件f_dir=新文件(文件路径);f_dir.mkdirs();文件file1=新文件(f_dir,文件名);如果(!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();我想在目录:root/dayday-is文件夹中创建文件zn.txt,所以文件必须是:root/day/zn.txt