Monday, October 24, 2011

[android-developers] Re: Impossible to Create & Write to or Read From Files in the SD Card

Hi,

As the doc says - 'This method is not generally useful'

public boolean createNewFile ()

Since: API Level 1
Creates a new, empty file on the file system according to the path
information stored in this file. This method returns true if it
creates a file, false if the file already existed. Note that it
returns false even if the file is not a file (because it's a
directory, say).
This method is not generally useful. For creating temporary files, use
createTempFile(String, String) instead. For reading/writing files, use
FileInputStream, FileOutputStream, or RandomAccessFile, all of which
can create files.
Note that this method does not throw IOException if the file already
exists, even if it's not a regular file. Callers should always check
the return value, and may additionally want to call isFile().
Returns

true if the file has been created, false if it already exists.
Throws

IOException if it's not possible to create the file.

Regards


On Oct 24, 8:12 am, melis <melisoner2...@gmail.com> wrote:
> Thanks for all replies! Before reading the commands i went for a walk
> for 5 minutes. When i turned back tried this code snippet,it worked. I
> wanted to share for who will have the same problem with me.
> But i realized createNewFile() function is useless. It creates the
> file even if i use or not. Is there a key point of this function?
>
> File path = Environment.getExternalStorageDirectory();
> File myFile = new File (path,"file1.txt");
> FileWriter writer;
> try {
>                         writer = new FileWriter(textFile);
>                         BufferedWriter out = new BufferedWriter(writer);
>                         out.write("hey");
>                         out.close(); }
> or
> File myFile  = new File("/sdcard/file1.txt");
> myFile.createNewFile();
> try {
>                         FileOutputStream fos = new FileOutputStream(myFile);
>                         OutputStreamWriter out = new OutputStreamWriter(fos);
>                         out.close();
>                         fos.close();}
>
> But i realized createNewFile() function is useless. It creates the
> file even if i use or not. Is there a key point of this function?

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment