简体   繁体   English

为什么我无法在Android中创建* .txt文件?

[英]Why i can't create *.txt file in android?

The below code creates a folder : 下面的代码创建一个folder

public class MainActivity extends FragmentActivity {
    private static final String DIR_SDCARD = Environment.getExternalStorageDirectory().getAbsolutePath();
    private static final String DIR_FILE = DIR_SDCARD + "/Android/data/com.project.google/Geo/";
    File myfile;
        myfile = new File(DIR_FILE+"GeoLocations.txt" );
        myfile.mkdirs();
}

But I need that GeoLocations.txt be txt file but it is folder when created. 但是我需要GeoLocations.txttxt file但是创建时是文件夹。

The line: 该行:

myfile.mkdirs();

Creates a directory based on the File object myfile . 基于File对象myfile创建目录。

If you wish to create a regular file, simply writing to myfile is sufficient, there is no need to call mkdirs . 如果要创建常规文件,只需写入myfile就足够了,无需调用mkdirs

If you are trying to create the directory that myfile will go into, then calling: 如果您尝试创建myfile将进入的目录,请调用:

myfile.getParent().mkdirs()

Will do that. 会做到的。

it's creating the folder because you're asking it to create the folder with the mkdirs() command. 它正在创建文件夹,因为您正在要求它使用mkdirs()命令创建文件夹。 What you want is: 您想要的是:

myfile.getParent().mkdirs();

so it creates the parent folder from your file. 因此它将根据您的文件创建父文件夹。

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

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