The Java self-study road-DAY15

Source: Internet
Author: User
Tags array length try catch

JAVA15

Ioio Overview

L I/O full name:

Input/output

L The previous program is in memory, once the end is gone

• Persistent storage is required if you need to use this data permanently

l need to use IO technology to put data into a persistent device

L Output Write Input read IO operation two ways

File class overview

L Data exists in file file in folder path

L Use the file class to manipulate these folders

function characteristics

L java.io in Bag

L Abstract representation of file and directory path names

L/linux drive letter \ Windows drive letter

• Encapsulate the file directory path in the operating system as a Document object

L provide the contents of the method operating system

L file-System-independent classes

L File folder directory directory paths path

Static member Variable PathSeparator

L String Type

L is actually a variant of the char type plus a + ""

L system-related path separators

L print the result as;

l; Indicates the separation of directories

L Linux directory separators are:

Separator

L system-related default name separators

L print result is \

L Linux Yes/

L Do not write dead separators

L This constant is required for system-independent programs

Construction method Three overloaded forms of file (String pathname)

L Pass path name

L can write to a folder or write to a file

l Example

L Print Results as path

L Windows System file names can be case insensitive

L Do not error even if the path is wrong because the file only changes the path to the file object to call the method is not resolved correctly

L have a special method exits ()

Relative path and absolute path

L Absolute Path

    1. Unique in the system

L Relative path

    1. Relative relationships between paths

    1. Parent Directory Uniqueness
    2. Subdirectories can be multiple
File (String parent,string Child)

L Pass Path

L Pass String Parent path string subpath

l Example

L Control parent and child paths individually

L such as user input sub-path and parent path

File (String parent,string Child)

L Pass Path

L PASS the parent path string sub-path of the file type

L Parent Path can directly use the function of file

L

File (Uri uri) file class creation and deletion capabilities for creating files

L Boolean CreateNewFile ()

L Create successful return true otherwise return false

L Need exception handling exception class in IO Package

l Example

Run results

L File name is Kanji

Create a folder

L Boolean mkdir ()

L Create successful return true otherwise return false

L can create a layer or multilevel directory Boolean mkdirs ()

l Example

Delete function of File class

L Boolean Delete ()

L Delete succeeds return true otherwise false

L REMOVE the Recycle Bin and remove it directly from the hard drive.

The FETCH function of the file class

L String GetName ()

    1. Returns the file or folder name represented in the path
    2. Example

L String GetPath ()

    1. Changes the encapsulated abstract path to a string
    2. Equivalent to ToString
    3. Example

L Long Length ()

    1. Returns the number of bytes of the file represented in the path
    2. Unable to get the number of bytes to the folder
    3. Example

L GetAbsolutePath () Getabsolutefile ()

    1. GetAbsolutePath () returns a string
    2. Getabsolutefile () returns the file object
    3. Get absolute path
    4. If you're writing a relative path, you get the project root directory.
    5. Example

L getParent () Getparentfile ()

    1. GetParent () returns a String object

Getparentfile () returns the file object

    1. Get Parent Path
    2. Example

The judgment function of the file class

L Boolean exists ()

    1. Determine if the encapsulation path exists in the construction method
    2. Presence returns True no return false exists
    3. Example

L isdirectory ()

    1. Returns a Boolean value
    2. Yes folder returns true not folder returns false
    3. Isfile () Determine if the file is not
    4. Example

File another fetch function

L List

    1. String[] List ()
    2. Gets the file and folder name in the path encapsulated in the file construction method (traversing a directory)
    3. The names of directories and files are returned.
    4. Example

L ListFile ()

    1. File[] Listfiles ()
    2. Gets the file and folder name in the path encapsulated in the file construction method (traversing a directory)
    3. A file array that returns the full path name of the directory and files
    4. Example

L Listroots ()

    1. Get to all the root directories of the system
    2. Returns a file array
File filter

L Traversal method Listfiles () overloaded form

L Listfiles (filefilter filter) interface type

L interface implementation class for passing FileFilter

L View document does not implement class custom implementation class

l Example

Principle
    1. ListFile () Gets the full path to the file while traversing the directory
    2. The method that calls the filter at the same time the accept passes the full path to the parameter of the Accept method pathname

    1. The Accept method accepts the parameter pathname, and the parameter is passed by Listfiles.
    2. And then go in and make judgments
    3. Returns true if the path is a Java file otherwise false
    4. Once the method returns True, the path is placed in the array
    5. Once the method returns false, the path is not placed in the array

Recursively traverse the full directory

L TRAVERSE the demo directory first

L then traverse the A directory under Demo

L files cannot traverse only folders

L so determine if the path is not a folder isdirectory

If you are iterating through this directory recursively call this method

L Print this directory directly if it is not a directory

L need to print the following path in order to print out the original parent directory

L

Recursive

L Concept

    1. Method itself calls itself
    2. The principal of the operation in the method is the same, but the parameters of the method participating in the operation will change when running.
    3. Direct recursive a calls a method
    4. Indirect recursive a call B calls C C call a

L Note

    1. Recursion must have an exit to get the program to stop.
    2. Not too many recursion times
    3. Construction method cannot be recursive

L Practice

    1. Sum calculation

Slow Execution 100 times

A) Multiple calls to the method into the stack

b) End of return method encountered

A) seeking factorial

a) int can only reach 13-14

    1. Recursive calculation of Perponado series

public static double sum (double n) {

if (n==1| | n==2) {

return 1;

}else{

return sum (n-1) +sum (n-2);

}

}

Io Stream object input and output

L file to program input

L program to file output

byte stream outputstream byte output stream overview

L operation 1 bytes 8 bits bit at a time

L Write file arbitrary file

L Abstract class

L Java.io Bag

L represents the superclass of all classes that output a byte stream

The L method is the way to write the file

Method

L write (int b) writes a byte

L Write (byte[] b) Write byte array

L Write (byte[] b,int off,int len) write byte array off start write index len write number

The close () method closes the stream object to release resources related to this stream otherwise it cannot be deleted

L Use sub-class required

Sub-class

L FileOutputStream

    1. Writing data files
    2. Construction method overload of 5
    3. The constructor function is to bind an output
    4. The file name of the string string that the parameter files encapsulate
    5. Use steps

A) Create a stream subclass object, binding the output data for the purpose

b) Call Stream object method write write to file for output

c) Close Release resources

    1. Files can be created according to the path, if the file exists, directly overwrite
    2. Example

N writes a single byte

A) The Text tool opens before encoding the table

b) So the 100 written is the corresponding d in the Code table.

c) Write 49 48 48 corresponds to 100

N writes an array of bytes

If it's a negative number

Is the Chinese character negative represents two negative numbers representing a Chinese character a Chinese character two bytes a number one byte

Less write a negative number will be missing

N writes a subset of byte arrays

Only the BC is written


n an easy way to write a byte array

Continuation and newline symbols for n files

    1. Continue writing

A) The second parameter in the construction method of the FileOutputStream is added true

b) Example

    1. Line break

a) \ t \ r

b) Symbol line break

c) write at the end of the previous line or the beginning of the next line

D

InputStream byte Input Stream overview

L Superclass of all byte input streams

Read any file read only one byte at a time

L Abstract class

Method

Reading method Read ()

    1. return value int
    2. Read one byte

L reading Method Read (byte[] b)

    1. The return value is int
    2. Reads a certain amount of bytes stored in the array
Sub-class

L FileInputStream

N Construction method binds a data source to this stream object

N Read Step

    1. Create a subclass object for the byte input stream
    2. Call the Read method reading
    3. Close Resource

n Example

    1. The next byte is automatically read as soon as it executes a read. The return value is the byte that is returned to the read

    1. Reading to the end of the data will return-1

    1. If there is 1 in the file, it's not affected because it's two bytes.

    1. If the file is too large, define Len (the Read method return value) to iterate through the

    1. Cannot directly write Fish.read () instead of Len because the next byte is automatically read every time a read is executed

N Read (byte[] b) Read

    1. The returned int represents the array length
    2. Example turns an array into a string method newstring (byte[] b)

Continue reading without reading finished

Read to the end of the data is-1

N Principle Analysis Diagram

    1. Program run by JVM virtual machine virtual machine without this function call system function
    2. First, a byte, a because there are two positions, so it's loaded with a byte B.
    3. The pointer points to who put who in the array
    4. And then the C D covered the original a B.
    5. And then I put an e covering C, so it's output e D.
    6. And then continue pretending that there are no elements but an end tag.
    7. After reading to the end tag returned to the JVM, the JVM returns-1 to the Read method

Array action buffer file byte increase efficiency

The int return value indicates the number of valid bytes read to

So instead of looping the read

Result one more d so the array length needs to be changed to 5

Variability is required for further improvement

The array should be set to an integer multiple of 1024 because of the variable size of the file

Exception Handling in IO

L do not choose to throw an exception

L Use Try Catch

L Close FA writes to finally still requires exception handling

L can only throw run-time exceptions if an exception fails to process

l Example

If you do not need to close a resource to establish a file, you need to determine whether the convection object is null before the Close method

Analysis of the principle of file copying

L InputStream Read OutputStream write

L copy can be extended to upload download

L

L Step (read one byte at a time)

    1. Defining an object variable with a stream
    2. Byte input stream bound data source read source file content input Program
    3. Byte output stream binding Data purpose output source file contents from program to another file
    4. Input read one byte
    5. Output Write one byte

l Example

    1. Exception handling Required
    2. Description

    1. Binding

    1. Copy

    1. Handling Exceptions

L Step (read byte array)

    1. Array buffering improves efficiency
    2. FileInputStream Reading byte arrays
    3. FileOutputStream Write byte array
    4. Define a variable of two streams

    1. Copy

    1. Handling Exceptions

Measuring time

Character encoding table

L above is the processing of digital letters

L character Stream is intended to process text files

L therefore need to understand the coding table

L ASCII Code table 7 bits in a byte can indicate that the corresponding byte is an integer

L iso-8859-1 Latin Code table with one byte of 8 bits

L GB2312 English Code table contains 6000-7000 Chinese and Chinese characters, two bytes means two bytes The first byte is 1, the second byte may be 0

    1. GBK currently the most commonly used Chinese code table 20,000 Chinese and the symbol two bytes means two bytes the first byte begins with 1, the second byte may start with 0
    2. GB18030 The latest Chinese code table has not been formally used

L Unicode International Standard Code Table No matter what the text is two bytes

    1. The char type in Java is the Code table Char c= ' A ' occupies two bytes
    2. The strings in Java are parsed according to the system default Code table, the default code table for the Simplified Chinese version is GBK

L UTF-8 base Unicode One byte can store data do not use two bytes storage post-development is often used and this code table more standardized in each byte header added encoded information (later to the API lookup)

L can recognize the Chinese Code table: GBKUTF-8 Because the identification Code table is not the only performance of the negative number is different, so it involves the decoding problem

L Development-Common coding GBK utf-8iso-8859-1

L encoded text variable number 97 98 99

L decode digital Variable type a b C

Writer Character Output stream overview

L Java.io.Writer

L Super class for all character output streams

L can only write text files

Method Write

L write (int c) writes a character

L Write (char[] c) An array of uppercase characters

L Write (Char[],int,int) a portion of an array of write characters starts indexing several

L Write (string s) writes a string

Sub-class

L FileWriter

L Construction Method writes the data purpose

The parameter type that the constructor method receives is a file type object or a string file name

L character output stream write data must use Flush Refresh method otherwise it will not be written

l Example

    1. Write a character

    1. Write a character array

If you do not write flush only at the end of writing a close can also be written in, but it is best to write a character array refresh because if you close the flush will be in memory, thus consuming system resources this will save system resources

    1. Close also has the ability to refresh

    1. Write part of a character array

    1. Write String

    1. Same as Byte-stream write data except that the parameter in the Write method becomes the char type
Character input stream read text file Reader class overview

L can only read Notepad to open the Read file text file

L Java.io.reader

L Super class for all character input streams

Method

L read () reads a character

l read (char [] c) reading character array

L and Byte stream are the differences between bytes and Char

L do not have the ability to read strings

Because the concept of a string cannot be determined by a single string more than just a string

Sub-class

L FileReader

L Construction Method

Binding Data sources

L parameter file type variable and string file name

l Example

    1. Read one character

can read text files

    1. Reads a character array

The difference between flush and close

Flush refreshes the stream can also be used

L Close refreshes the stream and closes it.

L Flush only write text need to use other unnecessary

Character Stream copy text file

Example

L Default Code table GBK

L

The Java self-study road-DAY15

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.