Home android Working with files on Android

Working with files on Android

Author

Date

Category

How to check if a file exists or not? If exists, then make it read, otherwise create, and record …

tried to create a file in this way:

file myf = new file (environment.getdatadirectory (), "file.txt");
Try.
{
  MyF.CreateNewFile ();
  text.settext (MyF.getAbsolutepath ());
}
Catch (IoException E1)
{
  text.settext (e1.tostring ());
}

As a result, writes java.io.ioException: Permission Denied

Yes, and in any case, when using the CreateNewFile () method, it gets out this …

After a long wandering on the Internet, I found a solution to my problem …

Initially, the application manifest file needed to add the following line

& lt; uses-permission android: name = "android.permission.write_external_storage" & gt; & lt; / uses-permission & gt ;

Next, everything happens like this:

file myf = new file (environment.getexternalstoreAgedirectory () + "/ Data /", "file.txt") ;
if (! myf.exists ())
{
  Try.
  {
    String str = "0";
    MyF.CreateNewFile ();
    Try.
    {
      FileoutPutStream Fout = OpenFileOutput (Myf.getAbsolutepath (), 0);
    OutputStreamwriter Outstr = New OutputStreamWriter (Fout);
      Outstr.Write (STR);
      Outstr.Flush ();
      Outstr.Close ();
    }
    Catch (Throwable T)
    {
      Toast.maketext (getApplicationContext (), "Exception:" + t.tostring (), toast.length_long) .show ();
    }
  }
  Catch (IoException E1)
  {
    text.settext (e1.tostring ());
  }
}
ELSE.
{
   text.settext ("File is!");
}

But now gets out:

java.lang.illegAlarGumentException: File MNT // SDCard // Data // File.txt Contains A Path Separator

Polaser found that “OpenFileoutput cannot receive the path, but only the file name ..”

But how then will turn to the created file?


Answer 1, Authority 100%

File File = New File (Path);
if (! file.exists ()) {
  file.createnewFile ()
  // and write
  FileoutPutStream Fout = OpenFileoutput (file.getAbsolutepath (), Mode_World_readable);
  OutputStreamwriter OSW = New OutputStreamwriter (Fout);
  osw.write ("Here to write info");
  osw.flush ();
  osw.close ();
} else {
  // read
  FileInputStream Fin = OpenFileInput (file.getabsolutepath ());
  InputStreamReader ISR = New InputStreamReader (FIN);
  char [] inputbuffer = new char [bufsize];
  ISR.READ (INPUTBUFFER);
  String Read = New String (InputBuffer);
}

Mode_World_Readable flag allows other applications to access a file with reading. There is still Mode_Private – only the creator gets access, and Mode_World_Writeable – all applications can write to this file. Flags are defined in the CONTEXT class.

How to read and write to file


Answer 2, Authority 50%

As a result, there was such an output:

file myf = new file (environment.getexternalstoreAgedirectory () + "/ Data /", "file.txt") ;
  if (! myf.exists ())
  {
    Try.
    {
      String str = "500";
      MyF.CreateNewFile ();
      Try.
      {
        FileWriter FWR = New FileWriter (MYF);
        FWR.WRITE (STR);
        FWR.Flush ();
        FWR.Close ();
      }
      Catch (Throwable T)
      {
        Toast.maketext (getApplicationContext (), "Exception:" + t.tostring (), toast.length_long) .show ();
      }
      text.settext ("Budget -" + Str + "R.");
    }
    Catch (Exception E1)
    {
      text.settext (e1.tostring ());
    }
  }
  ELSE.
  {
    Try {
        FileReader FRD = NEW FILEREADER (MYF); 
BufferedReader Reader = New BufferedReader (FRD);
         String str;
         STRINGBUFFER BUFFER = NEW STRINGBUFFER ();
         While ((str = Reader.ReadLine ())! = NULL)
         {
           buffer.append (STR + "\ N");
         }
         FRD.Close ();
         text.settext ("Budget -" + buffer.tostring () + "p.");
       }
     Catch (Throwable T)
     {
       Toast.maketext (getApplicationContext (), "Exception:" + t.tostring (), toast.length_long) .show ();
     }
   }

Thank you all for your attention!

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions