Chapter14 Simple Input and Output File class Text input and output (emphasi s) Binary Input and Output (empha sis) Object Input and Output Java高级程序设计(下) 1 Class File Most applications need to exchange data with external devices. External devices: disks, networks, etc. IO: Data input and output from an application to an external device. Located in: java.io package. File class: An abstract representation of file and directory pathnames. 1. The only class in the IO package that represents the information of the disk file itself, not the co ntent of the file. 2. File class can be used to create, delete and rename files, determine the permission to read and write files and whether they exist, and set and query the latest modification period of files. 3. The directory in Java is used as a special file. The list method can return all subdirectories and f ile names in the directory. 4. The path separator under Unix is (/), and under Dos is (\). Java can handle the path separator of UNIX and DOS correctly. Example: Determine whether a file exists, delete it if it exists, and create it if it does not exist. Java高级程序设计(下) 2 Class File Import java.io. *; Class file { Public static void main (String [] args) {File f = new File ("g:\\\ lesson1\ f ile. txt"); If (f. exists ()) F.delete (); Else Try{ F. createNewFile ();} Catch (Exception){ E. printStackTrace ();} } } Note: The "" in Java is a special character, which is represented by "\" or "/". Java高级程序设计(下) 3 Class File Be careful: In order to make the program run normally on different platfor ms, do not use absolute directory names and file names. Such as: Do not use: g: lesson lesson my. txt; Should be changed to: // Get the path to the current directory New File ("."). getCanonical File () // Representation of \my.txt File. separator + "my. txt" Note: getCanonicalFile () throws an I0Exception exception. Java高级程序设计(下) 4 The Difference between Text File and Binary File The difference between text files and binary files: Coding standards contain two meanings: 1. Which characters to use?Characters include text, punctuation, graphic symbols, n umbers, etc. 2. How to store.It is called encoding to specify whether each character in a character set is stored in one byte or multiple bytes, and which bytes to store. Common coding standards are: ASCII (American Standard Code for Information Inter change, American Standard Code for Information Interchange).It is the most gen eral single-segment (8-bit) coding system.GB2312.Our country was formulated i n 1980. GBK.China formulated it in 1995.GBK is an extension of GB2312. It contains 21806 ch aracters, including 21003 Chinese characters. It mainly expands the support of t raditional Chinese characters.GB18030.Our country was formulated in 2000.It c ontains 27484 Chinese characters, covering the languages of Japanese, Korean a nd Chinese minorities.Big5, JIS.The coding standards for traditional Chinese cha racters and Japanese codes developed by Japan are formulated in Taiwan. Java高级程序设计(下) 5 Java handles input and output Input class: Contains methods for reading data. Output class: Contains methods for writing data. Stream: An abstract concept of byte sequences.Such as: files, input and output devices, ne tworks, memory, internal process communication pipelines, TCP/IP and other data se quences can be streamed. Streams provide a way to read and write data from various input and output devices. File is the static storage form of data and stream is the form of data transmission. Files are only one of the streamable IO devices.File stream, network stream, memory strea m, tape stream, etc. Data stream: A collection of continuous data. Flows are classified into two categories: node flow class and filter flow class (processing fl ow class). Node Flow Class: Class used to directly manipulate the target device.The target device is ca lled a stream node. Filter Flow Class: Class that indirectly operates the target device. Java高级程序设计(下) 6 Java handles input and output Example: FileReader input = new FileReader ("temp.txt"); Int code = input. read (); System. out. println ((char) code); 程序 用输入类创建 的输入对象 用输出类创建 的输出对象 输入流 文件 文件 输出流 FileWriter output = new FileWriter ("temp. txt"); Output. write ("Java 101"); Output. close (); Java高级程序设计(下) 7 Text Output Processing Reader and Writer Classes: - - Input and Output Classes of Text The abstract classes of all character stream classes define the commonness of character op
java io
Tips: Current document can only be previewed at most page8,If the total number of pages in the document exceeds page 8,please download the document。
Uploaded by admin on 2019-10-16 00:29:15