JAVA in the flow - Introduction (a)

flow

File / file

java.io package

format:

 File file = new File("路径");
path
  1. Absolute path:

    1. Looking down from the letter;

    2. From the project root directory to start looking

  2. relative path:

    1. Edit the file from the current path relative to the path of the file you're looking for

      ../ upper level

    2. Try occasions usually find in the project file

Common methods:
 // Returns the parent directory path abstract 
.getparentFile ();
  // if the file exists 
.exists ();
  // create an empty file which will throw an exception. 
.CreateNewFile ();
  // delete 
.delete ();
  // get the path 
.getAbsolutepath ();
  // get the file name 
.getName ();
  // get the file's parent, returns String 
.getParent ();
  // determine whether the folder 
.isDirectory ();
  // get the current All file names in the path 
 .list ();

In real life we ​​often need to extract some files in a folder, if it is to get all the files in the judgment, this is very troublesome, java provide such a method for us

 .list(FilenmeFilter in);

This is a list () method is overloaded, incoming FilenmeFilter an excuse, we can implement this interface by way of anonymous inner classes,

 .list(new FilenmeFilter(){
     public boolean accept((File file),(String name)){
         if(name.endWith(".clsaa"))
             return ture;
         return false;
     }
 });

Classification stream

Classification Class A Class B
Transmission direction Input stream Output stream
Number of types of data transmission Byte stream Jifuryu
Operation method Node Stream Flow filtration

Other: commutations


Byte stream - the input stream

Role: to read the file.

类名:FileInputSteam

format:

FIS = FileInputSteam new new FileInputSteam ( "file path" );
  // 1.FileInputSteam ( "File Path") will throw an exception
  // 2. If the file is a relative path, the folder will only need to write the file name, the SRC next you need to add SRC folder.
 // the Java class at compile time will be automatically added to the package name.

method:

 int n = fis.read ();
  // 1. reads a byte, integer return (for Chinese characters can not be read) and stored in the n
  @ 2 each use, a cursor backward
  @ -1 3. At the end the
 int n = FIS ((. byte [] bytes));
  // in bytes the length of the byte 1. The number of bytes read, store
  // valid data length n is 2. Return an array of bytes read
  // -1 3. At the end the

Byte stream - the output stream

Fos = FileOutputSteam new new FileOutputSteam ( "file path" );
  // 1.FileOutputSteam ( "File Path") will throw an exception
  // 2. If the file is a relative path, the folder will only need to write the file name, the SRC next you need to add SRC folder.
 // the Java class at compile time will be automatically added to the package name.
 // 3. If the path of the last file does not exist, it will automatically be added, (but the middle of the path must exist, or will be error)

method:

fos.write ();
  // Use analog .read (), including overload .write ((byte [] bytes) );

Stream closed

  1. Written finally in

  2. First determine whether the air

  3. Close .close function ();

  4. Also throw an exception when closing the stream, remember to shut down a stream, a try / catch

  5. Since the independence of the try / catch the language, so to flow statement in the try / catch statement outside


Jifuryu

The upper layer of abstraction
  1. Reader / from operator input, common implementation class: FileReader

  2. Writer / output from the symbol, common implementation class: FileWriter

Features: read a character - two bytes

Usage is similar to the byte stream


Flow filtration

Buffer flow

Class Name:

  1. Byte Input: BufferedInputStream

  2. Byte outputs: BufferedOutputStream

  3. Character Input: BufferedReader

  4. Character output: BufferedWriter

Constructor:

BufferedInputStream (InputStream in);
  // buffer is circulated into subclasses implement InputStream interface
  // while closing the stream, it is closed, which also closes the internal flow

characteristic:

  1. Read several times faster

  2. Without closing the buffer flow, part of the data will remain in the "buffer tube", resulting in missing data

    Performs the closed flow .flush (); (Wash Buffer Tube Method)

  3. There is a special character input method stream

    .readline ();
      // 1. read a line of characters, not including newline (\ n-)
      // 2. read completion return null
      // 3. corresponding .write (..... + "\ n" ), you need to add "\ n"

Print streams

Class Name: PrintWriter

method:

.println ((String str)); // Print and line feed

 

Guess you like

Origin www.cnblogs.com/-Archenemy-/p/12026769.html