java1.7 and file I/O operations in Android (draft)

Source: Internet
Author: User

Description: The key analogy of file I/O is more, here is a summary.

Reference: "Java 7 Getting Started classic"

1. Java input Stream

Figure 1,java Structure diagram of common input and output streams (first edition)

1) Standard Flow

Standard flow

Type

Method

System.in

InputStream

System.out

PrintStream

For example: printf ("b=%2$-15.2f", A, B)

System.err


2) byte type

Typical Way-1: read from a file

File File = ...   InputStream in = null;   try {in     = new Bufferedinputstream (new FileInputStream (file));     ...    Finally {     if (in! = null) {       in.close ();}}   }

Typical Way-2: Read from a file (java1.7)

Files.newinputstream (path file) path file = Path.get ("D:/cao.java")   inputstream in = null;   try {in    = new Bufferedintputstream (files.newinputstream (file));     ...    Finally {     if (out! = null) {       out.close ();}}   }
Typical mode-3 reads from the console
in = new Bufferedinputstream (system.in);

3) Character type

Typical Way-1: Read from a file (* * * * * * * * * *)

BufferedReader buf=new BufferedReader (new FileReader (String file)//cannot encode bufferedreader buf = new BufferedReader (new FileReader ("File.java"));

Typical Way-2: read from the file (* * * * * * * *)

BufferedReader buf=new BufferedReader (New InputStreamReader (new FileInputStream (string file), String charsetname))  //Can encode InputStreamReader (InputStream in, String charsetname) file File = ... BufferedReader buf = new BufferedReader (new InputStreamReader (New FileInputStream (String file), "Utf-8");

Typical Way-3: read from a file (***java1.7 Best * *)

InputStream newinputstream (Path path,openoption ... options) path File=paths.get (); Bufferedinputstream in=new Bufferedinputstream (files.newinputstream (File, Charset.forname ("UTF-8"))
4) channel input stream (--pending-) 2, Java output stream
1) byte type

Typical Way-1: Open the output stream of the file

FileOutputStream (File file,boolean append), append is true append mode, no new

File File = ...   OutputStream out = null;   try {out    = new Bufferedoutputstream (new FileOutputStream (file));     ...    Finally {     if (out! = null) {       out.close ();}}   }

Typical Way-2: Open the file's output stream (java1.7)

Files.newoutputstream (pathfile,create,append), No then new, append mode

Path file = Path.get ("D:/caoyanfeng.java")   outputstream out = null;   try {out    = new Bufferedoutputstream (files.newoutputstream (file));     ...    Finally {     if (out! = null) {       out.close ();}}   }
2) Character type

Typical Way-1: Open the output stream of the file (* * * * * * * * * * * )

FileWriter (File file,boolean append), append is true append mode, no is new. You cannot specify the encoding method.

File File = ...   writer writer = null;   try {    writer = new BufferedWriter (new FileWriter (file));     ...    Finally {     if (writer! = null) {       writer.close ();}}   }

Typical Way-2: Open the output stream of the file (* * * * * * * * * * *)

OutputStreamWriter (new FileOutputStream (file file, booleanappend), "UTF-8" ), append is true append mode, no new,utf-8 encoded

File File = ...  writer writer = null;   try {    writer = new OutputStreamWriter (new FileOutputStream (file), "UTF-8");     ...   Writer.write ("Cao Yanfong");   Writer.flush;    Finally {     if (writer! = null) {       writer.close ();}}   }

Typical Way-3: Open the output stream of the file (* * * * * * * best )

Files.newbufferedwriter (Path file,charset.forname ("UTF-8"), Create,append), No then new, append mode , Utf-8 Coding

Path file = Path.get ("D:/cao.java")  writer writer = null;   try {    writer = new BufferedWriter (Files. Newbufferedwriter ("Path file,charset.forname (" UTF-8 "), create,append));    c13/> ... Finally {     if (writer! = null) {       writer.close ();}}   }
3) Channel output stream(--pending completion-)
3. Android input stream

Android's input and output stream is basically the same as Java, except that the path needs to use the phone path. In addition, the context provides a two-byte stream under the operating procedure path.

fileinputstream openfileinput (String name)

fileoutputstream openfileoutput (String name,int mode)

1) byte type

Get the fileinputstreamof the file and then encapsulate it

Bufferedinputstream inch = New Bufferedinputstream (new fileinputstream(file file));

Typical example-1: operation of the program Data folder IO

The context method of Android

FileInputStream openfileinput (String name)

Typical example-2: Specify IO under Path (same as Java)

File File=new file (dir, "/data/com.iteye.androidtoast/androidtoast.txt"); Bufferedinputstream in  = new Bufferedinputstream (new FileInputStream (file file));

2) Character type 3, Android output stream 1) byte type

Get the FileOutputStream of the file and then encapsulate it

The Fileoutputstreamfos is then encapsulated into PrintStream or Bufferedoutputstream

bufferedoutputstream out=new Bufferedoutputstream (openfileinput (String name) or

printstream out=new Bufferedoutputstream (openfileinput (String name))

Typical example-1: operation of the program Data folder IO

FileOutputStream openfileoutput (string name, int mode) is FileOutputStream fos= openfileinput (string name)

Typical example-2: Operation the next IO of the specified folder

FileOutputStream (File File, Boolean append), same as Java byte stream file Dir = environment.getdatadirectory ();//Get Data directory   file File=new File (dir, "/data/com.iteye.androidtoast/androidtoast.txt");   FileOutputStream out=new fileoutputstream (file, true);  
2) Character type

Typical example-1: Open the output stream of the file (* * * with java, bad way * * *)

File Dir = environment.getdatadirectory ();//Get Data Directory file File=new file (dir, "/data/com.iteye.androidtoast/ Androidtoast.txt ");   FileWriter fw=new FileWriter (file,true);   

It can be written directly to the file, or it can be encapsulated again

BufferedWriter bw=new BufferedWriter (New FileWriter (file,true));

Typical Way-2: Open the output stream of the file (* * * with java, better way * * *)

File File=new file (dir, "/data/com.iteye.androidtoast/androidtoast.txt"); OutputStreamWriter (New FileOutputStream ( File file,  boolean append), "UTF-8")//Append to True append mode, no new, UTF-8 encoded

























java1.7 and file I/O operations in Android (draft)

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.