0

The below code creates a 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.

2
  • java101: read the doc of the methods you are using.
    – njzk2
    Aug 5, 2014 at 13:47
  • You're making a folder called "GeoLocations.txt" because mkdirs makes folders along the entire path you concatenated, including the last entity. Cut "GeoLocations.txt" out from your path, call mkdirs and then open up a regular file.
    – webuster
    Aug 5, 2014 at 13:48

2 Answers 2

3

The line:

myfile.mkdirs();

Creates a directory based on the File object myfile.

If you wish to create a regular file, simply writing to myfile is sufficient, there is no need to call mkdirs.

If you are trying to create the directory that myfile will go into, then calling:

myfile.getParent().mkdirs()

Will do that.

2
  • When i remove myfile.mkdirs(); dont create any thing.
    – A.A
    Aug 5, 2014 at 13:50
  • 1
    Java won't create your .txt file until you write something to it. Aug 5, 2014 at 13:58
1

it's creating the folder because you're asking it to create the folder with the mkdirs() command. What you want is:

myfile.getParent().mkdirs();

so it creates the parent folder from your file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.