简体   繁体   English

如何在 android 工作室中将文件创建到新文件夹中

[英]How to create file into a new folder in android studio

I still dont see any folder or file created.我仍然没有看到创建的任何文件夹或文件。 what is wrong.怎么了。

            try {
                String rootPath = Environment.getExternalStorageDirectory()
                        .getAbsolutePath() + "/MyFolder/";
                File root = new File(rootPath);
                if (!root.exists()) {
                    root.mkdirs();
                }
                File f = new File(rootPath + "mttext.txt");
                if (f.exists()) {
                    f.delete();
                }
                f.createNewFile();

                FileOutputStream out = new FileOutputStream(f);

                out.flush();
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
    

Please what is wrong please.请问有什么问题。

try this and check!试试这个并检查!

try {
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/newfoldername/";    // it will return root directory of internal storage
        File root = new File(path);
        if (!root.exists()) {
            root.mkdirs();       // create folder if not exist 
        }
        File file = new File(rootPath + "log.txt");
        if (!file.exists()) {
            file.createNewFile();   // create file if not exist 
        }
        BufferedWriter buf = new BufferedWriter(new FileWriter(file, true));
        buf.append("hi this will write in to file");
        buf.newLine();  // pointer will be nextline
        buf.close();

    } 
 catch (Exception e) {
        e.printStackTrace();
    }

Add your AndroidManifest.xml添加您的 AndroidManifest.xml

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM