File class and IO streams

1, File class

Package Penalty for Lesson12;
 Import java.io.File;
 Import java.io.IOException;
 Import java.text.SimpleDateFormat;
 Import java.util.Date;
 public  class demo1 {
     / ** 
     * Windows file or directory represents the D: \ test \ 1.txt 
       represents D Java file or directory: \\ test \\ 1.txt, or D: /test/1.txt 
     * / 
    public  static  void main (String [] args) {
         // file: indicates that the disk a file or directory on the 
        file F1 = new new file ( "E: /aa/a/a.txt" );
         // file file new new F = ( "E: \\ \\ AA a / a.txt"); / / write is also OK 
        File F2 = new new File("E:\\aa\\a\\a.txt");
        
        File f3=new File("E:/aa/a","a.txt");
        File f4=new File("E:\\aa\\a","a.txt");
        
        File f5=new File("E:/aa/a");
        File f6=new File(f5,"a.txt");
        
        System.out.println(f1.getName());//获取文件对象名(文件夹或文件)
        System.out.println(f2.getName());
        System.out.println(f3.getName());
        System.out.println(f4.getName());
        System.out.println(f5.getName());
        System.out.println(f6.getName());
        
        // System.out.println (f.canRead ()); 
        System.out.println (f1.canWrite ()); // is readable 
        System.out.println (f1.canRead ()); // Can write
         // f1.delete (); // delete
         // f1.deleteOnExit (); // deleted when you exit the JVM. That program runs out of time to delete the file, the next at run-time files have been deleted 
        
        
        System.out.println ( "-------------------------" ) ; 
        System.out.println (f1.exists ()); 
        System.out.println (f1.getPath ()); // to absolute return type String path 
        System.out.println (f1.getAbsolutePath ()); // String path to absolute return type 
        System.out.println (f1.getAbsoluteFile ());// to return the absolute path File type 
        System.out.println (f1.getParent ()); // to return to the parent class String type 
        System.out.println (f1.getParentFile ()); // to File Returns the parent class type 
        System.out.println (f1.isFile ()); // if the file 
        System.out.println (f1.isDirectory ()); // is a directory 
        System.out.println (f1.isHidden ()); / / whether to hide 
        System.out.println (f1.length ()); // returns the file length of the object 
        
        
        System.out.println ( "-----------------" );
         long lastime = f1.lastModified (); // represents the last time the file was modified value long time, and with a point in time (January 1, 1970, 00: 00: 00 GMT) represents the number of milliseconds between
        System.out.println (lastime); // number of milliseconds 1561176411802 
        
        // public Date (Long DATE)
         // assignment Date object and initializes the object to represent the time since the reference standard (referred to as "Epoch (Epoch)", i.e., Specifies the number of milliseconds January 1 1970 00:00:00 GMT) since. 
        Time = String new new SimpleDateFormat ( "yyyy Year MM Month dd Day HH: mm: SS") format (. New new a Date (lastime)); 
        System.out.println (Time); 
        
        
        System.out.println ( "---- ----------------- " ); 
        File F7 = new new File (" E: / AA " ); 
        String strlist [] = f7.list (); // to a String other documents in the file object is returned 
        for ( int0 = I; I <strList.length; I ++ ) { 
            System.out.println (strlist [I]); // only the name 
        } 
        
        File the fileList [] = f7.listFiles (); // to return the file object File type other files 
        for ( int I = 0; I <fileList.length; I ++ ) { 
            System.out.println (the fileList [I]); 
            String sonList [] = the fileList [I] .list ();
             for ( int J 0 =; J <sonList.length; J ++ ) { 
                System.out.println (sonList [J]); // only the name 
            } 
            
        } 
        
        
        System.out.println ("-------------------------" ); 
        File file8 = new new File ( "E: /aaaa/aaa/aaa.txt" ); 
        the System .out.println (file8.getName ()); // files whether they can get the filename exists 
        System.out.println (file8.canRead ()); // when the file was not created false 
        
        file parentFile = file8.getParentFile ; ()
         IF (! parentFile.exists ()) {
             // parentFile.mkdir (); // create a single folder, such as when an error java.io.IOException use it to create file8: the system can not find the path specified. 
            parentFile.mkdirs (); // create a plurality of folders (subfolders created together with) 
        }
         the try {
            file8.createNewFile (); // Create File 
        } the catch (IOException E) {
             // the TODO Auto-Generated Block the catch 
            e.printStackTrace (); 
        } 
            
        File file9 = new new File ( "E: / AAA / AA" ); 
        file9. mkdirs (); // create a folder         
    } 
}

2, stream

     Flow is a set of ordered, there is a set of start and end byte , is a general term for computer data transmission or abstract.
     That data transmission between two devices called flow, the essence is the carrier stream data transmission.  
    Data stream in the sequence may not be processed raw data (number of bytes of binary data), the data may be in line with some predetermined format encoded, Java provides the different classes of flows to process them.
3, IO streams

The output is the input for the program, not the data source. Input i.e. input data to the program is read from the data stream, the output that is output from the program data is written into the data stream.

The input stream (the InputStream)
    In Java, a program can open the input stream, the input stream information sources may be located in a file, memory, or a network socket (Socket) and other places, the type of the information source may include objects, characters, images, any type of sound, including. Once you opened the program input stream from the input stream can be serially read the data.


Output stream (the OutputStream)
    Similarly, the programs can also open a stream and sequentially outputs the write data to the information to the destination.

4, byte stream

Concept of the byte stream
    data transmission units are bytes , the byte stream also means that can handle any kind of document
composition byte stream
    input stream of bytes InputStream
    byte output stream OutputStream

Input stream of bytes FileInputStream

Package Lesson12;
 Import java.io.File;
 Import a java.io.FileInputStream;
 Import java.io.FileNotFoundException;
 public  class demo2 {
     public  static  void main (String [] args) throws Exception {
         // create a byte input stream object 
        FileInputStream = FIS new new the FileInputStream ( new new File ( "E: \\ \\ a.txt ABC" ));
         // read data
         // int I = fis.read (); // . 1 to read a single character
         // the System. Out.println (I);
 //         System.out.println (I);
//         System.out.println (I);
 //         System.out.println (I); 
        
//         byte B [] = new new byte [1024]; // 2
 //         fis.read (B); // to the length of the array of bytes read data
 //         System.out.println (new new String (B) .trim ()); 
                
        byte B [] = new new  byte [20 is]; // . 3
         // int read (byte [] BS after, int off, int len) off of an offset, i.e., how many bytes the like and then starts reading
         // OFF should not exceed the length len + byte array, or will be reported java.lang.IndexOutOfBoundsException abnormal 
        fis.read (b, 0 , to b.length); 
        
        System.out.println ( new new String (B)); 
        fis.close ();// off flow         
    } 
}

Output stream of bytes FileOutputStream

package LESSON12;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class demo3 {    
    public static void main(String[] args) throws Exception {
        File file8=new File("E:xyz/xyz.txt");
        File parentFile=file8.getParentFile();
        if(!parentFile.exists()){
            //parentFile.mkdir();//Create a single folder, such as when an error java.io.IOException create file8 use it: The system can not find the path specified. 
            parentFile.mkdirs (); // create a plurality of folders (along with creating subfolders) 
        }
         the try { 
            file8.createNewFile (); // Create File 
        } the catch (IOException E) {
             // the TODO Auto-Generated the catch Block 
            E .printStackTrace (); 
        } 
        
        // create output byte stream object 
        a FileOutputStream fos = new new a FileOutputStream (file8, to true ); // to true: additional, i.e., after the program executed once finished the second run 1 also adds the same content 
        fos. write (100); // write data        
        fos.write ( "Today Saturday" .getBytes ()); // write data in the form of a byte array     
        byte B [] = "Tomorrow rest" .getBytes (); 
        fos.write (B, 0 , B. length); 
        
        fos.close ();         
    } 
}

 

Guess you like

Origin www.cnblogs.com/qfdy123/p/11070177.html