Android-file storage-text Storage

Source: Internet
Author: User

Android-file storage-text Storage

[Return directory]

If you want to perform file input or output operations, you need to perform stream operations.


Activity Support for file operations

Public FileInputStream openFileInput (String name)Set the file input stream to open

Public FileOutputStream openFileOutput (String name, int mode)Set the output stream of the file to be opened. The operation mode can be 0, MODE_APPEND, MODE_PRIVATE, MODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE.

Public Resources getResources ()Returns the Resources object.


Text storage can be divided into mobile phone space and sdcard space. If mobile phone space is used, read and write are performed using the method provided by Activity. For sdcard space, traditional I/O operations are used.


Output example stored in the mobile phone Space (the file name does not need to be written, and is saved directly under/data/package name/files)

Package com. example. testtext; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. printStream; import android. app. activity; import android. OS. bundle; public class MainActivity extends Activity {private final String FILENAME = "potato.txt"; // file name @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); FileOutputStream fos = null; try {fos = super. openFileOutput (FILENAME, Activity. MODE_PRIVATE); PrintStream ps = new PrintStream (fos); ps. println ("potato"); ps. println ("22"); ps. println ("true"); ps. close (); // The resource must be closed fos. close ();} catch (Exception e) {e. printStackTrace ();}}}

Read files stored in mobile phone Space
package com.example.testtext;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.util.Scanner;import android.app.Activity;import android.os.Bundle;import android.util.Log;public class MainActivity extends Activity {private final String FILENAME="potato.txt";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);FileInputStream fis=null;try {fis=super.openFileInput(FILENAME);Scanner scanner=new Scanner(fis);while(scanner.hasNext()){Log.e("Potato", scanner.next());}scanner.close();fis.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

Stored in sdcard

Review the Operation steps of IO streams in Java

1. Use the File class to define a File to be operated

2. instantiate the parent class using the byte stream or the child stream subclass (because the four operation streams are abstract classes)

3. Complete input or output functions

4. Close the stream

Example

package com.example.testtext;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.PrintStream;import java.util.Scanner;import android.app.Activity;import android.os.Bundle;import android.util.Log;public class MainActivity extends Activity {private final String FILENAME="/mnt/sdcard/potato.txt";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);File file=new File(FILENAME);if(!file.getParentFile().exists()){file.getParentFile().mkdirs();}try {PrintStream ps=new PrintStream(new FileOutputStream(file));ps.println("potato");ps.println("tomato");ps.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {Scanner sn=new Scanner(new FileInputStream(file));while(sn.hasNext()){Log.e("Potato", sn.next());}sn.close();} catch (FileNotFoundException e) {e.printStackTrace();}}}


Note: The read/write sdcard must be declared in the AndroidManifest. xml file. Otherwise, an error is reported.

 
 








Related Article

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.