Java BASICS (12) IO input and output, java basics io Input and Output

Source: Internet
Author: User

Java BASICS (12) IO input and output, java basics io Input and Output
I. IO Overview 1. IO concepts

IO: I represents Input; O represents Output.

In Java, IO is input and output based on a stream. AllSerializing data(SAVE) write to the output stream or read from the input stream.

Note:Serializing dataThe object state is saved to a stream in a specific form (such as byte []) and written in a stream.

2. Functions of IO

1. A text file can write data to a file in a specific way, and can also read the content in the file.

2. Save the information to the disk file.

3. Java operation File 1. File object Creation Method

Three methods to test file creation:

1 import org. junit. test; 2 3 import java. io. file; 4 5/** 6 * @ author zt1994 7 * @ date 2018/3/2 8 */9 public class CreateFile {10 11/** 12 * test the three methods for creating files, only create the File object 13 */14 @ Test15 public void testCreateFile () {16 // 1, File (String pathname) in the program) create a new File instance by converting a given path name string to an abstract path name. 17 File file = new File ("test1.txt"); // relative path (relative to the current project) 18 File file1 = new File ("D: \ file \ test1.txt "); // absolute path 19 20 // 2. File (String parent, String child) creates a new File instance based on the parent path name String and the child path name String. 21 File file2 = new File ("D: \ file \", "test2.txt"); 22 23 // 3, File (File parent, String child) creates a new File instance based on the abstract path name and child path name string of the parent. 24 File file3 = new File ("D: \ file \"), "test3.txt"); 25 26 System. out. println (file); // test1.txt27 System. out. println (file1); // D: \ file \ test1.txt28 System. out. println (file2); // D: \ file \ test2.txt29 System. out. println (file3); // D: \ file \ test3.txt30} 31}
2. Create and delete files and folders

1. Create a file

1 import org. junit. test; 2 3 import java. io. file; 4 import java. io. IOException; 5 6/** 7 * @ author zt1994 8 * @ date 2018/3/2 9 */10 public class CreateNewFile {11 12/** 13 * create files on the disk 14*1, the path to the created file must exist, otherwise, an exception 15*2 is thrown. If the file already exists, false16 * 3 is returned. This method can only create files, but cannot create folders 17 */18 @ Test19 public void testCreateNewFile () throws IOException {20 File file1 = new File ("D: \ test.txt"); // absolute path 21 boolean B = file1.createNewFile (); 22 System. out. println (B); 23} 24}

2. Create and delete folders

Test the creation and deletion of folders:

1 import java. io. file; 2 3/** 4 * @ author zt1994 5 * @ date 2018/3/2 11: 27 6 */7 public class CreateDir {8/** 9 * test and delete the create folder 10 */11 @ test12 public void testMakeDir () {13 // create a folder 14 File dir = new File ("E: \ testIo"); 15 System. out. println (dir); 16 // 1. Create a single-layer folder 17 boolean mkdir = dir. mkdir (); 18 System. out. println (mkdir); 19 20 // 2. Create a multi-layer folder 21 File dirs = new File ("E: \ Demo \ test \ file "); 22 boolean mkdirs = dirs. mkdirs (); 23 System. out. println (mkdirs); 24 boolean deleteDirs = dirs. delete (); // delete 25 System. out. println (deleteDirs); 26 27 // delete the folder public boolean delete () 28 boolean delete = dir. delete (); 29 System. out. println (delete); 30} 31}

3. Other common methods

Boolean exists () to test whether a file or directory exists;

String getName () to get the name of the file or directory;

String getParent () returns the path String of the abstract path name parent directory. If this path name does not specify the parent directory, null is returned.

Boolean isFile () to test whether it is a file;

Boolean isDirectory to test whether a directory exists;

3. Delete all contents of the folder directory (recursively delete)
1 import org. junit. test; 2 3 import java. io. file; 4 import java. io. IOException; 5 6 public class TestDeleteAll {7/** 8 * Delete all files in the folder, recursively Delete 9 */10 @ Test11 public void testDeleteAll () {12 // create a multi-layer folder 13 File file = new File ("E: \ Demo \ test \ file"); 14 file. mkdirs (); 15 16 File file1 = new File ("E: \ Demo"); 17 deleteAll (file1 ); 18 19} 20 21 // delete all files in the specified directory 22 public static void deleteAll (File file) {23 if (file. isFile () | file. list (). length = 0) {24 file. delete (); 25} else {26 File [] files = file. listFiles (); 27 for (File f: files) {28 deleteAll (f); // call the method itself 29 f. delete (); 30} 31} 32} 33}
Ii. IO stream read/write files 1. IO stream classification 1. Stream-based classification

The flow direction of the input stream and output stream is relative.

2. Classification by data unit

Byte stream and byte stream.

Test IO stream code:

1 import org. junit. test; 2 3 import java. io. *; 4 5/** 6 * output stream and input Flow Test 7 */8 public class TestIoStream {9/** 10*1. read a single character and convert it to 11 */12 @ Test 13 public void test1 () throws IOException {14 // create a File object 15 file File = new File ("f1.txt "); 16 17 FileInputStream fileInputStream = new FileInputStream (file); 18 19 int I = fileInputStream. read (); 20 System. out. println (I); 21 22 // character encoding conversion 23 char x = (char) I; 24 System. out. println (x); 25} 26 27 28/** 29*2. read multiple characters 30 */31 @ Test 32 public void test2 () throws Exception {33 File file = new File ("f1.txt "); 34 FileInputStream fileInputStream = new FileInputStream (file); 35 36 byte [] fs = new byte [(int) file. length ()]; 37 while (fileInputStream. read (fs )! =-1) {38 System. out. println (new String (fs); 39 for (byte B: fs) {40 System. out. print (char) B); 41} 42} 43 44 fileInputStream. close (); 45} 46 47 48/** 49*3. the output stream will overwrite 50 * @ throws IOException 51 */52 @ Test 53 public void test3 () throws IOException {54 File file File = new File ("f1.txt "); 55 56 FileOutputStream fileOutputStream = new FileOutputStream (file); 57 58 String str = "Hello, world! "; 59 60 // get the string array object 61 byte [] bytes = str. getBytes (); 62 63 fileOutputStream. write (bytes); 64 fileOutputStream. flush (); 65 fileOutputStream. close (); 66} 67 68/** 69*4. input 70 */71 @ Test 72 public void test4 () throws IOException {73 FileReader fileReader = new FileReader ("f1.txt"); 74 75 boolean f = true; 76 while (f) {77 78 int read = fileReader. read (); 79 System. out. print (char) read); 80 if (read =-1) {81 f = false; 82} 83} 84} 85 86 87/** 88*5. audio Stream output 89 */90 @ Test 91 public void test5 () throws IOException {92 FileWriter fileWriter = new FileWriter ("f1.txt"); 93 94 fileWriter. write ("convenient output"); 95 96 fileWriter. close (); 97} 98 99 100/** 101*6. byte Transfer history stream 102 */103 @ Test104 public void test6 () throws IOException {105 FileInputStream inputStream = new FileInputStream ("f1.txt"); 106 InputStreamReader reader = new InputStreamReader (inputStream, "UTF-8"); 107 108 boolean f = true; 109 while (f) {110 int I = reader. read (); 111 System. out. print (char) I); 112 if (I =-1) {113 f = false; 114} 115} 116} 117}
3. Differences between byte streams and byte streams

① The operation unit is different. One is a byte and the other is a character;

② It is nice to use the livestream for Chinese operations;

③ The output stream of the callback stream can directly write a String write (String msg );

④ Execution process:

Byte output stream -- program -- File

Character output stream-Program-Cache-File

Test:

A: After A byte stream calls the Data Writing Method, if it is not closed, the data will still be written to the file;

B: If the callback Stream does not close the Data Writing Method after it calls the Data Writing Method, it will not write the data to the file;

 

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.