2

I want to create a file in a defined directory, i tried this two codes but the first just creates folders and the other output an exception: no such file or directory: First code:

File file = new File(Environment.getExternalStorageDirectory()
                                 +File.separator
                                 +"carbu" 
                                 +File.separator
                                 +"install"); 
                            file.mkdir();

Then i added this code hopefully to create the file:

File file2 = new File("/carbu/install/","voitu");
file2.createNewFile();

Can anyone please try to help me ? Thank you very much :).

androniennn
  • 3,117
  • 11
  • 50
  • 107

5 Answers5

3

Try this in your Activity:

FileOutputStream fos = openFileOutput(YOUR_FILE_NAME, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeInt(5);
oos.flush();

This will create file if it isn't exist. Of course you should close oos and fos.

Onuray Sahin
  • 4,093
  • 4
  • 33
  • 34
  • And where can i put the path of "YOUR_FILE_NAME" ? – androniennn Jul 28 '11 at 15:11
  • Write "/carbu/install/voitu" instead of `YOUR_FILE_NAME` – Onuray Sahin Jul 28 '11 at 15:15
  • I have this logcat message(the file does not create :( ): 07-28 15:18:46.946: DEBUG/Excep_ERROR(10652): File /carbu/install/voitu/ contains a path separator – androniennn Jul 28 '11 at 15:20
  • Ok, right. Do you have to create file in a directory? If you just write `voitu` instead of `YOUR_FILE_NAME` it will work. If you have to create file in a directory let me know and I will search in my earlier codes. – Onuray Sahin Jul 28 '11 at 15:25
  • I just have 2 directories: /carbu/install and want to create on it an empty file. Waiting for your response.Thank you very much :). – androniennn Jul 28 '11 at 15:29
  • Sorry, I remembered wrong. You can't give path in `openFileOutput`. But why you want to use directories? Just use the method by passing filename. – Onuray Sahin Jul 28 '11 at 15:37
  • I really don't understand whay you mean, sorry. the method by passing filename ? how ? – androniennn Jul 28 '11 at 15:40
  • Try this: `openFileOutput("voitu", Context.MODE_PRIVATE);` File will be created in root directory of application. You can't see it physically. Then you can read the file by using `openFileInput("voitu")` – Onuray Sahin Jul 28 '11 at 15:46
  • I don't want to make that but when created i can then copy it the to desired path in SDCARD. Right ? – androniennn Jul 28 '11 at 15:49
  • 1
    You can't copy it physically. If you want to write file to sdcard try this:http://androidgps.blogspot.com/2008/09/writing-to-sd-card-in-android.html – Onuray Sahin Jul 28 '11 at 15:53
1

Here is the most simple solution, working at 100% ;)

File dir = new File (sdCard.getAbsolutePath() + "/jetpack/install");
                    dir.mkdirs();
                    File file = new File(dir, "wipe");
androniennn
  • 3,117
  • 11
  • 50
  • 107
0

have you tried:

  File f=new File("myfile.txt");
  if(!f.exists())
  {
    f.createNewFile();
  }

In you example you are only giving the pathname to the file but you are not defining the type and the name of the new file.

http://download.oracle.com/javase/6/docs/api/java/io/File.html

Also try following:

String FILENAME = "/carbu/install/test.txt";
String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close()

got the above example from: http://developer.android.com/guide/topics/data/data-storage.html

sap
  • 1,141
  • 6
  • 41
  • 62
  • I have this logcat error : 07-28 15:08:08.075: DEBUG/Excep_ERROR(8887): No such file or directory – androniennn Jul 28 '11 at 15:09
  • in your current directory for this particular example. you can change it to new File("/carbu/install/test.txt"); – sap Jul 28 '11 at 15:16
  • Why i have this error ? 07-28 15:23:02.646: DEBUG/Excep_ERROR(11227): File /carbu/install/test contains a path separator – androniennn Jul 28 '11 at 15:24
  • Can you please post your code and the entire error message you are receiving. – sap Jul 28 '11 at 15:31
  • Related question:http://stackoverflow.com/questions/2079766/how-to-create-write-file-in-the-root-of-the-android-device – sap Jul 28 '11 at 15:36
  • Thank you but this didn't help me :(. Don't know why it's stucking :\\ – androniennn Jul 28 '11 at 15:39
0
  try {
        File root = Environment.getExternalStorageDirectory();
        if (root.canWrite()){
            File gpxfile = new File(root, "gpxfile.gpx");
            FileWriter gpxwriter = new FileWriter(gpxfile);
            BufferedWriter out = new BufferedWriter(gpxwriter);
            out.write("Hello world");
            out.close();
        }
    } catch (IOException e) {
        Log.e(TAG, "Could not write file " + e.getMessage());
    }
androniennn
  • 3,117
  • 11
  • 50
  • 107
0

While you were careful in constructing the path correctly in the first segment, you just hard-coded the wrong path in the second part. Ensure you use the correct path, possibly as follows:

String path = Environment.getExternalStorageDirectory().getAbsolutePath()
                                 +File.separator
                                 +"carbu" 
                                 +File.separator
                                 +"install";
File file = new File(path); 
file.mkdir();

File file2 = new File(path + File.separator + "voitu");
file2.createNewFile();
ProjectJourneyman
  • 3,566
  • 1
  • 27
  • 37