Java Foundation--I/O

Source: Internet
Author: User

Text I/O vs. binary I/O   

All the files in the computer are stored in binary form, so essentially all of the files are binary files.
Text I/O is based on binary I/O, which provides an abstraction of the encoding and decoding of the character hierarchy, and when writing a character, the Java virtual Opportunity translates the Uniform code into the encoding specified by the file, and the read file is the same. Binary I/O does not require conversions. If you use binary I/O to write a numeric value to a file, the exact value in memory is copied to the file.
Because binary I/O does not require encoding and decoding, binary I/O is more efficient than text I/O .
binary files are not related to the encoding scheme of the host, so it is portable. Java programs on any machine can read binary files created by Java programs.

Java text I/O

Java.io.File File = new Java.io.File ("Hello.java");
An abstract representation of the file and directory path names.
An instance of the file class is immutable; that is, once created, the abstract path name represented by the file object will never change.
--Create a file object in the current directory of files Hello.txt
can also be a definite absolute path, such as "/users/zhengbinmac/workspaces/hello.java"

Java.io.PrintWriter = new Java.io.PringWriter ("File file");
Creates a new printwriter that does not have an automatic row refresh using the specified file.
Can be used to create a file and write data to a text file.
Will report a FileNotFoundException exception.

Java.util.Scanner s = new Java.util.Scanner (File);
Creating a generated value is a scanner that is scanned from a specific file
Java.util.Scanner s = new Java.util.Scanner (String);
Creating a generated value is a scanner that is scanned from a specific string

1  Public classreplacedate{2      Public Static voidMain (String args[]) {3         Try{4Java.io.File F1 =NewJava.io.File ("/users/zhengbinmac/workspaces/revise Java/files and I:o/123.txt");5 System.out.println (F1.exists ());6Java.util.Scanner input =NewJava.util.Scanner (F1);7Java.io.File F2 =NewJava.io.File ("/users/zhengbinmac/workspaces/revise Java/files and I:o/321.txt");8 System.out.println (F2.getabsolutepath ());9Java.io.PrintWriter output =NewJava.io.PrintWriter (F2);TenString S1 =NewString (); One             if(Input.hasnext ()) { AS1 =input.nextline (); -String s2 = s1.replaceall ("Hello", "OK1"); - output.println (S2); the             } - input.close (); - output.close (); -}Catch(java.io.FileNotFoundException e) { + e.getstacktrace (); -         } +     } A}
Replacedate.java

Java binary I/O

BYTE stream
The Java byte stream is used to process 8-bit bytes of input and output.
Java.io.FileInputStream (file/string)
Java.io.FileOutputStream (file/string)

1  Public classCopyFile {2              Public Static voidMain (string[] args)throwsjava.io.IOException {3Java.io.FileInputStream in =NULL;4Java.io.FileOutputStream out =NULL;5                 Try{6in =NewJava.io.FileInputStream ("Input.txt");7out =NewJava.io.FileOutputStream ("Output.txt");8                     intC;9                      while((c = In.read ())! = 1){Ten Out.write (c); One                     } A}Catch(java.io.IOException e) { - e.getmessage (); -}finally{ the                     if(In! =NULL){ - in.close (); -                     } -                     if(Out! =NULL){ + out.close (); -                     } +                 } A             } at         
Copyfile.java

  

Character Stream
The Java byte stream is used to process 8-bit bytes of input and output, while a character stream is used to handle 16 bits, that is, FileReader and FileWriter read and write two bytes at a time.

File input stream
InputStream f = new FileInputStream ("/users/zhengbinmac/workspaces");
Or
File F = new file ("/users/zhengbinmac/workspaces");
InputStream f = new FileInputStream (f);

File output stream
OutputStream f = new FileOutputStream ("/users/zhengbinmac/workspaces");
Or
File F = new file ("/users/zhengbinmac/workspaces");
OutputStream f = new FileOutputStream (f);

1 ImportJava.io.*;2          Public classfilestreamtest{3             Public Static voidMain (String args[]) {4                Try{5                   byteBwrite [] = {11,21,3,40,5};6OutputStream OS =NewFileOutputStream ("Test.txt");7                    for(intx=0; x < bwrite.length; X + +){8Os.write (Bwrite[x]);//writes the bytes9                   }Ten os.close (); One  AInputStream is =NewFileInputStream ("Test.txt"); -                   intSize =is.available (); -  the                    for(inti=0; i< size; i++){ -System.out.print ((Char) Is.read () + ""); -                   } - is.close (); +}Catch(IOException e) { -System.out.print ("Exception"); +                }     A            } at}
Filestreamtest.java

Java Foundation--I/O

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.