JAVA basics I/O Review

Source: Internet
Author: User
Tags string back

I. Files:
The only File-related class in the IO package is the File class. Common constants and methods in the File class
1. Create a file:Specify the path and the name and type of the file to be created. Then call the createNewFile () method.

File file = new File ("D:" + File. separator + "MyJavaProgram" + File. separator + "hello. java"); file. createNewFile ();

2. delete an object:Specify the path and file, including the type attribute, and then call the delete () method.
File file = new File ("D:" + File. separator + "MyJavaProgram" + File. separator + "hello. java"); file. delete ();

3. Create a file directory:After setting the path, call the mkdir () method. For example, to create a folder named "hello" under the path "D: \ MyJavaProgram", the program is as follows:
File file = new File ("D:" + File. separator + "MyJavaProgram" + File. separator + "hello"); file. mkdir ();
4. list all objects in the specified directory:There are two methods in the File class:
  • public String[] list()
  • public File[] listFiles()
The first one is to list the names of all the files in the directory, excluding the attributes. That is to say, what I see is the hello file name, but I don't know whether it is word or txt text; the second is in the form of a directory, including attributes and names, in the form of a File array, the first example is as follows:
Public class FileDemo2 {public static void main (String [] args) {File file = new File ("D:" + File. separator + "MyJavaProgram" + File. separator); String [] str = file. list (); for (int I = 0; I <str. length; I ++) {System. out. println (str [I]) ;}}
The second example is as follows:
Import java. io. *; public class FileDemo2 {public static void main (String [] args) {File file = new File ("D:" + File. separator + "MyJavaProgram" + File. separator); File [] str = file. listFiles (); for (int I = 0; I <str. length; I ++) {System. out. println (str [I]) ;}}
Running result:

Ii. Use RandomAccessFile to write and read data at the specified locationThe above File is only used to create management files, but to write and read data to files, you still need to use other class objects. The RandomAccessFile class is the RandomAccessFile class used to write data that reads data at the specified location. It provides the random read function to read the content at the specified location.


In the file, all content is stored in bytes. For example, an int integer occupies 4 bytes.
The preceding common method table shows that the RandomAccessFile class has two constructor methods. For example, now we use the first constructor to write strings and integers --
Import java. io .*;Public class RandomAccessFileDemo {Public static void main (String args []) throws Exception { File file = new File ("d:" + File. separator + "MyJavaProgram" + File. separator + "test.txt ");// Read/write mode. If the file does not exist, it is automatically created.RandomAccessFile ramdomFile = new RandomAccessFile (file, "rw ");// Write 2 data recordsString name = "girl ";Int love = 25257758;// Write the string using the writeBytes MethodRamdomFile. writeBytes (name );// Write the integer number, using the writeInt MethodRamdomFile. writeInt (love );// Remember to close the file after the read/write operations are completedRamdomFile. close (); }}

Result:
Now, we will demonstrate how to read data at the specified position. Now, we can skip the four bytes of the previous girl and directly read the subsequent numbers.
Import java. io. File;Import java. io. RandomAccessFile;Public class RandomAccessFileDemo { Public static void main (String args []) throws Exception {File file = new File ("d:" + File. separator + "MyJavaProgram" + File. separator + "test.txt ");// Read/write mode. If the file does not exist, it is automatically created.RandomAccessFile randomFile = new RandomAccessFile (file, "rw ");// Read the number following the string. You must skip the previous string (4 bytes) first)RandomFile. skipBytes (4 );Int I = randomFile. readInt ();// Output the read valueSystem. out. println ("the number read is:" + I );// Read the string back and use the seek method to set the pointer position.RandomFile. seek (0 );Byte [] temp = new byte [4];For (int j = 0; j <temp. length; j ++ ){Temp [j] = randomFile. readByte ();}// Convert to a stringString s = new String (temp );System. out. println ("the read string is:" + s );// System. out. println ("the read string is" + s );// Remember to close the file after the read/write operations are completedRandomFile. close ();}};

The running result is:
In this case, a problem occurs: The System. out. println () is used to print the output of Chinese characters, always reporting the encoding warning:

Later, it was indeed a problem with the encoding format. The solution was to open the java source file in notepad, save it as, select ANSI encoding, overwrite it, and compile it again to eliminate warnings or errors.


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.