6 days of reviewing java BASICS (I/O)

Source: Internet
Author: User

I. File class

• Input: Read external data (data from disks, CDs, and other storage devices) to the Program (memory. • Output: outputs Program (memory) data to disks, CDs, and other storage devices. • Java IO streams mainly include input and output IO streams, each input and output stream can be divided into two types: byte streams and two types of upstream streams: -byte streams are used to process input and output operations in bytes-character streams are used to process input and output operations. Note: input and output are program-based. • The File class represents platform-independent files and directories. • File can be used to create, delete, and rename files and directories, but File cannot access the File content. If you want to access the file content, you need to use the input/output stream. Part of the File operation method: exercise code:
Package com. shellway. io; import java. io. file; import java. io. IOException; import org. junit. test; public class IOtest {@ Testpublic void test () throws IOException {File file = new File ("helloo.txt"); // obtain the file name String fileName = File. getName (); System. out. println (fileName); // obtain the absolute path of the object String filePath = file. getAbsolutePath (); System. out. println (filePath); // rename a file. Not only does the file name change, but the original file path changes with the following path: // file. renameTo (new File ("D: \ test \ day03 \ helloo.txt"); String path = file. getPath (); System. out. println (path); // file detection method System. out. println (file. exists (); File dir = new File ("shellway"); System. out. println (dir. isDirectory (); System. out. println (dir. isFile (); // obtain the general information of the file System. out. println (file. length (); // The unit is byte. If a Chinese character is two bytes, two bytes will be added to the line feed. // File related operations File file2 = new File ("adc.txt "); file2.createNewFile (); // create an empty File file3 = new File ("adc"); file3.mkdir (); // create an empty directory file2.delete (); // delete an object }}

1. IO stream classification

• Stream-by-flow:-input stream-output stream • unit of processing:-byte stream (8-bit bytes)-bytes stream (16-bit bytes) • stream-based role-node stream: stream that reads/writes data from a specific IO Device-stream processing: connects and encapsulates an existing stream, data read/write operations through encapsulated streams 2. IO stream system: 2. InputStream & Reader • InputStream and Reader are the base classes of all input streams. • InputStream (typical implementation: FileInputStream):-int read ()-int read (byte [] B)-int read (byte [] B, int off, int len) • Reader (typical implementation: FileReader):-int read ()-int read (char [] c)-int read (char [] c, int off, int len) • the file I/O resources opened in the program do not belong to the memory resources, and the garbage collection mechanism cannot recycle the resources. Therefore, the file I/O resources should be explicitly disabled. Exercise code: 1 package com. shellway. io; 2 import java. io. file; 3 import java. io. fileInputStream; 4 import java. io. fileNotFoundException; 5 import java. io. fileReader; 6 import java. io. IOException; 7 import java. io. inputStream; 8 import java. io. reader; 9 import org. junit. test; 10 public class IOtest {11/** 12 * Test character input stream 13 * @ throws IOException 14 */15 @ Test16 public void testReader () throws IOException {17 Re Ader reader = new FileReader ("helloo.txt"); 18 char [] buffer = new char [10]; 19 int len = 0; 20 while (len = reader. read (buffer ))! =-1) {21 for (int I = 0; I <len; I ++) {22 System. out. print (buffer [I]); 23} 24} 25 reader. close (); 26} 27/** 28 * test byte input stream 29 * @ throws IOException30 */31 @ Test32 public void testInputStream () throws IOException {33 // 1. Create a character input stream 34 InputStream in = new FileInputStream ("helloo.txt "); 35 // 2. Read the file content 36 // 2. 1. Read a single character at a time, which is very inefficient. We do not recommend this reading.-1 indicates reading The End Of The file 37 int result = in. read (); 38 while (result! =-1) {39 System. out. print (char) result); 40 result = in. read (); 41} 42 // 2, 2 read a group of characters at a time, 43 byte [] buffer = new byte [10]; 44 int length = 0; 45 while (length = in. read (buffer ))! =-1) {46 for (int I = 0; I <length; I ++) {// do not add "=" 47 System. out. print (char) buffer [I]); 48} 49 // 2, 3 read the content to 50 byte [] result1 = new byte [1024*10] in the continuous element of the byte array; 51 // 10 is the beginning of the byte array, and the third is the actual length. 52 in. read (result1, 10, in. available (); 53} 54 // 3. Disable 55 in. close (); 56} 57}Test character and byte input stream

 

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.