Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Creating file using createNewFile() method

Tags:

android

As described in android documentation, to create a new file which will be situated in the application's directory there is the method in Context class: openFileOutput().

But where will be the file situated if I use simple createNewFile() method from File class.?

like image 592
teoREtik Avatar asked Aug 10 '11 13:08

teoREtik


People also ask

Which method is used to create the file createNewFile(); createfile(); createNew(); create();?

When we initialize File object, we provide the file name and then we can call createNewFile() method to create new file in Java. File createNewFile() method returns true if new file is created and false if file already exists. This method also throws java. io.

What is createNewFile () in Java?

The createNewFile() function is a part of File class in Java . This function creates new empty file. The function returns true if the abstract file path does not exist and a new file is created. It returns false if the filename already exists.

What is Java file in Android?

Java: The Java folder contains the Java source code files. These files are used as a controller for controlled UI (Layout file). It gets the data from the Layout file and after processing that data output will be shown in the UI layout. It works on the backend of an Android application.


1 Answers

CreateNewFile() is used like this:

File file = new File("data/data/your package name/test.txt");
if (!file.exists()) {
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
}

So you will tell the file where it should be created. Remember that you can only create new files on your package. That is "data/data/your package name/".

like image 135
Dominik Mohr Avatar answered Oct 20 '22 20:10

Dominik Mohr