Java input and output

Source: Internet
Author: User

Input and output:
The file class: is the only class in the IO package that represents the information for the disk file itself, not the contents of the file.
        defines a platform-agnostic approach to manipulating files, such as creating, deleting, renaming, and so on. Directories in
        Java are used as a special kind of file, and the list method can return all subdirectories and file names in the directory.
Randomaccessfile class: Supports random access mode. (For a stream-formatted file, you typically read from scratch, and through this class, simply move the file indicator to the location you want to read)
       1. There is a file indicator inside the class object, Data can be read from the specified location. (The network program in the extension of the breakpoint, the function of multi-threaded download FTP server, the first thread to download 0-1k, the second download 1-2k, and then combination)
       2. The Randomaccessfile class has a great advantage in random (relative to sequential) files that read and write long-form records.
       3. Randomaccessfile is limited to manipulating files, not other IO devices such as networks, memory images, and so on.
       file Indicators in a file only one, whether read or write, reads and writes from the location of the file indicator as the starting position.
            In Java, both Chinese and English characters are Unicode encoded, taking up two bytes. However, in the local computer,
    English uses ASCII encoding for only one byte, while Chinese occupies two bytes. When using Getbyes, English is represented locally in
    one byte, while Chinese is represented by two bytes

 Public Static void Main (string[] args) {        String Streng= "a";        String Strchi= "I";        System.out.println (Streng.getbytes (). Length+ "------>" +strchi.getbytes (). length);        System.out.println (Streng.tochararray (). Length+ "----->" +strchi.tochararray (). length);    }

classemployee{ Public Final Static intLen=8;    String name; intAge ;  PublicEmployee (String name,intAge ) {        if(Name.length () >LEN) This. name=name.substring (0, LEN); Else{             while(Name.length () <LEN) {Name+ = "\u0000"; }             This. name=name; }         This. age=Age ; }}     Public Static voidMain (string[] args)throwsexception{Employee E1=NewEmployee ("Zhangsan", 23); Employee E2=NewEmployee ("Lisi", 24); Employee E3=NewEmployee ("Wangwu", 25); Randomaccessfile RAF=NewRandomaccessfile ("Employee.txt", "RW");          Raf.write (E1.name.getBytes ());        Raf.writeint (E1.age);          Raf.write (E2.name.getBytes ());        Raf.writeint (E2.age);          Raf.write (E3.name.getBytes ());        Raf.writeint (E3.age);                Raf.close (); Randomaccessfile RAFB=NewRandomaccessfile ("Employee.txt", "R"); byte[] buff=New byte[Employee.len]; String name=""; intAge ; //read a secondRafb.skipbytes (12);        Rafb.read (Buff); Name=NewString (Buff); Age=Rafb.readint (); System.out.println (Name.trim ()+ "----------->" +Age ); //Read the firstRafb.seek (0);        Rafb.read (Buff); Name=NewString (Buff); Age=Rafb.readint (); System.out.println (Name.trim ()+ "----------->" +Age ); //reads a thirdRafb.skipbytes (12);        Rafb.read (Buff); Name=NewString (Buff); Age=Rafb.readint ();        Rafb.close (); System.out.println (Name.trim ()+ "----------->" +Age ); }

When it is Chinese:

classemployee{ Public Final Static intLen=8;    String name; intAge ;  PublicEmployee (String name,intAge ) {        if(Name.length () >LEN) This. name=name.substring (0, LEN); Else{             while(Name.length () <LEN) {Name+ = "\u0000";//and when Name= John Doe, it added six characters, and made up 8 characters.            }             This. name=name; }         This. age=Age ; }} Public classTestrandomaccessfile {/*in Java, both Chinese and English characters are Unicode encoded, taking up two bytes. However, in the local computer, English uses ASCII encoding for only one byte, while Chinese occupies two bytes. When using Getbyes, English is represented locally in one byte, while Chinese is represented by two bytes*/    //the cause of the problem in Chinese: when the number of characters is 8 o'clock, the number of bytes that are not necessarily written to the file when using GetBytes is 8, because the number of Chinese and English converts to bytes is different.     Public Static voidMain (string[] args)throwsexception{Employee E1=NewEmployee ("Zhangsan", 23); Employee E2=NewEmployee ("John Doe", 24); Employee E3=NewEmployee ("Wangwu", 25); Randomaccessfile RAF=NewRandomaccessfile ("Employee.txt", "RW");          Raf.writechars (E1.name);        Raf.writeint (E1.age); Raf.writechars (E2.name); //when it is Chinese, the number of bytes written is tenRaf.writeint (e2.age);          Raf.writechars (E3.name);        Raf.writeint (E3.age);                Raf.close (); Randomaccessfile RAFB=NewRandomaccessfile ("Employee.txt", "R"); String name=""; intAge ; //read a secondRafb.skipbytes (16+4); intI=0;  for(i=0;i<employee.len;i++) {Name+=Rafb.readchar (); } Age=Rafb.readint (); System.out.println (Name.trim ()+ "----------->" +Age ); //Read the firstRafb.seek (0); Name="";  for(i=0;i<employee.len;i++) {Name+=Rafb.readchar (); } Age=Rafb.readint (); System.out.println (Name.trim ()+ "----------->" +Age ); //reads a thirdRafb.skipbytes (16+4); Name="";  for(i=0;i<employee.len;i++) {Name+=Rafb.readchar (); } Age=Rafb.readint ();        Rafb.close (); System.out.println (Name.trim ()+ "----------->" +Age ); }}

Input and output:
File class: Is the only class in the IO package that represents the information for the disk file itself, not the contents of the file.
Defines a platform-independent approach to manipulating files, such as Create, delete, rename, and so on.
Directories in Java are used as a special type of file, and the list method can return all subdirectories and file names in the directory.
Randomaccessfile class: Supports random access mode. (For a stream-formatted file, you typically read from scratch, and through this class, simply move the file indicator to the location you want to read)
1. There is a file indicator inside the class object that can read the data from the specified location. (The network program in the extension of the breakpoint, the function of multi-threaded download FTP server, the first thread to download 0-1k, the second download 1-2k, and then combination)
2. The Randomaccessfile class has a great advantage in random (in relation to sequential) reading and writing of files in long record formats.
3. Randomaccessfile is limited to manipulating files, not other IO devices such as networks, memory images, etc.
The file indicator in a file has only one read or write, which reads and writes from the location of the file indicator to the beginning.

Java input and output

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.