3.2 File Storage

Source: Internet
Author: User
Tags gettext

1. Writing files

FileOutputStream out = Openfileoutput ("Info1.txt", mode_private);               = Contentet.gettext (). toString ();               byte [] bytes = content.getbytes ();              Out.write (bytes);              Out.close ();

First, declare a file output stream, use the system-provided Openfileoutput to get the output stream, and get the output stream to indicate the file name of the output stream and the usage mode of the file . Usage patterns are divided into mode_append that is, write data to the end of the file,mode_private only open the file can write data,mode_world_readable All programs can read the file data,Mode_world_ Writable that is, all programs can write data.

2. Read the file

FileInputStream in = Openfileinput ("Info1.txt");               byte New byte [1024x768];              In.read (buffer);               = encodingutils.getstring (buffer, "UTF-8");                This . Contentet.settext (Str.tostring ());              In.close ();

another: static file or into an embedded file, the file is part of the program, at the time of writing into the program's Resource directory, the program compiles the file, as part of the program compiled together, the file is generally stored in the Res/raw folder, The name of the file can be accessed through r.raw.filename. This type of file is read as follows:

InputStream in = Getresources (). Openrawresource (R.raw.test);

Read SD on the file

SDcard file refers to the files stored on the SD card, before the SD card operation, please make sure that the SD card is installed and is configured to allow the operation of the SD card. The SD-open permissions are configured in the file Androidmanifest.xml, you need to write the code:

// Create Delete file permissions <uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>     //  Write Permissions

Before operating the SD card in the program , it is also necessary to determine whether the SD card is installed, the method is as follows:

if (Environment.getexternalstoragedirectory (). Equals (environment.media_mounted)) {......}

the code for writing file data to SD is as follows:

File Sdcdir = environment.getexternalstoragedirectory ();   New File (Sdcdir, "Info.txt");   Try {      new  fileoutputstream (file);       = Contentet.gettext (). toString ();      Out.write (Content.getbytes ());      Out.close ();   Catch (FileNotFoundException e) {      ...}

Read The method similar to the above, the code is as follows:

 File sdcdir = Environment.getexternalstoragedirectory ();  File File  = new  file (sdcdir, "Info.txt"  try   {in  = new   FileInputStream (file);  byte  [] buffer = new  byte  [1024];      In.read (buffer);      String str  = encodingutils.getstring (buffer, "UTF-8"  this  .contentet.settext (str.tostring ())      ;  In.close ();  catch   (FileNotFoundException e) {...} 

3.2 File Storage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.