java to read the full version of the file

public  class ReadFromFile {  
     / **  
     * reads the file in bytes, used for reading the binary file, such as images, sound, video and other documents. 
     * /   
    Public  static  void readFileByBytes (String fileName) {   
        File File = new new File (fileName);   
        the InputStream in = null ;  
         the try {   
            System.out.println ( "bytes to read the contents of a file, read one byte at: " );  
             // read one byte at   
            in = new new the FileInputStream (File);  
             int tempbyte;  
             the while ((tempbyte = in.read ())! = -1) {   
                System.out.write (tempbyte);   
            }   
            in.close ();   
        } the catch (IOException E) {   
            e.printStackTrace ();   
            return ;   
        }   
        the try {   
            System.out.println ( "bytes read file content, a plurality of bytes read: " );  
             // time reading bytes   
            byte [] = tempbytes new new  byte [100 ];  
             int byteread 0 = ;   
            in = new new the FileInputStream (fileName);   
            ReadFromFile.showAvailableBytes (in );  
            // number of bytes read into a byte array into a plurality of bytes, byteread once read   
            the while ((byteread = in.read (tempbytes))! = -1 ) {   
                System.out.write (tempbytes, 0 , byteread);   
            }   
        } the catch (Exception E1) {   
            e1.printStackTrace ();   
        } the finally {  
             IF (in =! null ) {  
                 the try {   
                    in.close ();   
                } the catch (IOException E1) {   
                }   
            }   
        }   
    }   
  
    / * * 
     * In units of character reading file, commonly used to read text, and other types of digital files 
     * /   
    public  static  void readFileByChars (String fileName) {   
        File File = new new File (fileName);   
        Reader Reader = null ;  
         the try {   
            the System.out. println ( "in characters read the contents of the file, read one byte at:" );  
             // once read a character   
            Reader = new new InputStreamReader ( new new FileInputStream (file));  
             int tempchar;  
             the while ((tempchar = reader.Read ())! = -1 ) {  
                 //For the windows, \ r \ n together these two characters, it represents a line break.  
                // But if these two characters are shown separately, it will change two lines.  
                // Thus, masked \ R & lt, or shield \ n. Otherwise, it will be a lot more blank lines.  
                IF ((( char !) tempchar) = '\ R & lt' ) {   
                    of System.out.print (( char ) tempchar);   
                }   
            }   
            reader.Close ();   
        } the catch (Exception E) {   
            e.printStackTrace ();   
        }   
        the try {   
            System.out.println ( "read in units of characters in the file content, a plurality of bytes read:" );  
             // read multiple characters   
            char[] = Tempchars new new  char [30 ];  
             int charread = 0 ;   
            Reader = new new the InputStreamReader ( new new the FileInputStream (fileName));  
             // read the plurality of characters in the character array, charread once the number of characters read   
            the while ((charread ! = reader.Read (tempchars)) = -1 ) {  
                 // the same masked \ r is not displayed   
                IF ((== charread tempchars.length)  
                         && (tempchars [tempchars.length -. 1] = '\ r'! ) ) {   
                    of System.out.print (tempchars);   
                } the else {  
                    for (int i = 0; i < charread; i++) {  
                        if (tempchars[i] == '\r') {  
                            continue;  
                        } else {  
                            System.out.print(tempchars[i]);  
                        }  
                    }  
                }  
            }  
  
        } catch (Exception e1) {  
            e1.printStackTrace();  
        } finally {  
            if (reader != null) {  
                 The try {   
                    reader.Close ();   
                } the catch (IOException E1) {   
                }   
            }   
        }   
    }   
  
    / **  
     * read in units of files, used for reading row-oriented format file 
     * /   
    public  static  void readFileByLines (String fileName ) {   
        file file = new new file (fileName);   
        the BufferedReader Reader = null ;  
         the try {   
            System.out.println ( "read in units of the contents of the file, to read a whole line time:" );   
            Reader= New new BufferedReader ( new new FileReader (File));   
            String tempString = null ;  
             int Line = 1 ;  
             // read into line until the end of the file is read into the null   
            the while ((tempString = reader.readLine ()) =! Null ) {  
                 // display line numbers   
                System.out.println ( "line" line + + ":" + tempString);   
                line ++ ;   
            }   
            reader.Close ();   
        } the catch (IOException E) {   
            e.printStackTrace ();  
        } The finally {  
             IF (Reader =! Null ) {  
                 the try {   
                    reader.Close ();   
                } the catch (IOException E1) {   
                }   
            }   
        }   
    }   
  
    / **  
     * random access file contents 
     * /   
    public  static  void readFileByRandomAccess (String fileName) {   
        randomfile a RandomAccessFile = null ;  
         the try {   
            System.out.println ( "random access file contents paragraph:" );  
             // open a file stream of random access, read - only  
            = randomfile new new a RandomAccessFile (fileName, "R & lt" );  
             // file length, the number of bytes   
            Long fileLength = randomFile.length ();  
             // start position of the file read   
            int the beginIndex = (fileLength>. 4). 4: 0? ;  
             // the start position of the read files moved to beginIndex position.  
            randomFile.seek (the beginIndex);  
             byte [] bytes = new new  byte [10 ];  
             int byteread = 0 ;  
             // a read 10 bytes, if the file content is less than 10 bytes, the remaining bytes read.  
            // number of bytes is assigned to a read byteread  
            the while ((byteread = randomFile.read (bytes)) = -1! ) {   
                System.out.write (bytes, 0 , byteread);   
            }   
        } the catch (IOException E) {   
            e.printStackTrace ();   
        } the finally {  
             IF ( ! randomfile = null ) {  
                 the try {   
                    randomFile.close ();   
                } the catch (IOException E1) {   
                }   
            }   
        }   
    }   
  
    / **  
     * show the number of bytes remaining in the input stream 
     * /  
     Private static void showAvailableBytes(InputStream in) {  
        try {  
            System.out.println("当前字节输入流中的字节数为:" + in.available());  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
  
    public static void main(String[] args) {  
        String fileName = "C:/temp/newTemp.txt";  
        ReadFromFile.readFileByBytes(fileName);  
        ReadFromFile.readFileByChars(fileName);  
        ReadFromFile.readFileByLines(fileName);  
        ReadFromFile.readFileByRandomAccess(fileName);  
    }  
}

 

Guess you like

Origin www.cnblogs.com/JonaLin/p/11057398.html