[Java] How to read files
Reprinted http://www.cnblogs.com/lovebread/archive/2009/11/23/1609122.html
1. Read File Content by byte
2. Read File Content by character
3. Read File Content by row
4. Randomly Read File Content
Public class ReadFromFile {/*** reads files in bytes. It is often used to read binary files, files, sounds, images, and other files. */Public static void readFileByBytes (String fileName) {File file = new File (fileName); InputStream in = null; try {System. out. println ("Read file Content in bytes, one byte at a time:"); // read one byte at a time in = new FileInputStream (file); int tempbyte; while (tempbyte = in. read ())! =-1) {System. out. write (tempbyte);} in. close ();} catch (IOException e) {e. printStackTrace (); return;} try {System. out. println ("Read File Content in bytes, read multiple bytes at a time:"); // read multiple bytes at a time byte [] tempbytes = new byte [100]; int byteread = 0; in = new FileInputStream (fileName); ReadFromFile. showAvailableBytes (in); // read multiple bytes into the byte array. byteread is the number of bytes read at a time. while (byteread = in. read (tempbytes ))! =-1) {System. out. write (tempbytes, 0, byteread) ;}} catch (Exception e1) {e1.printStackTrace () ;}finally {if (in! = Null) {try {in. close ();} catch (IOException e1) {}}}/*** reads a file in characters and is often used to read text, numeric files */public static void readFileByChars (String fileName) {File file = new File (fileName); Reader reader = null; try {System. out. println ("Read file Content in characters, one byte at a time:"); // read one character at a time reader = new InputStreamReader (new FileInputStream (file); int tempchar; while (tempchar = reader. read ())! =-1) {// for windows, \ r \ n indicates a line break when the two characters are together. // If the two characters are displayed separately, the rows are changed twice. // Therefore, block \ r or \ n. Otherwise, there will be a lot more blank lines. If (char) tempchar )! = '\ R') {System. out. print (char) tempchar) ;}} reader. close ();} catch (Exception e) {e. printStackTrace ();} try {System. out. println ("Read File Content in characters, read multiple bytes at a time:"); // read multiple characters at a time char [] tempchars = new char [30]; int charread = 0; reader = new InputStreamReader (new FileInputStream (fileName); // read multiple characters into the character array, charread is the number of characters read at a time. while (charread = reader. read (tempchars ))! =-1) {// also blocked \ r does not show if (charread = tempchars. length) & (tempchars [tempchars. length-1]! = '\ R') {System. out. print (tempchars);} 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) {try {reader. close ();} catch (IOException e1) {}}}/*** reads the file in the unit of action, used to read row-oriented formatted files */public static void readFileByLines (String fileName) {File file = new File (fileName); BufferedReader reader = null; try {System. out. println ("reads the content of a file in the unit of action, and reads the entire row at a time:"); reader = new BufferedReader (new FileReader (file); String tempString = null; int line = 1; // read a row at a time until null is the end of the file while (tempString = Reader. readLine ())! = Null) {// display the row number System. out. println ("line" + line + ":" + tempString); line ++;} reader. close ();} catch (IOException e) {e. printStackTrace ();} finally {if (reader! = Null) {try {reader. close ();} catch (IOException e1) {}}}/*** Randomly Read File Content */public static void readFileByRandomAccess (String fileName) {RandomAccessFile randomFile = null; try {System. out. println ("randomly read a file:"); // open a random access file stream, in read-only mode randomFile = new RandomAccessFile (fileName, "r "); // file length, number of bytes long fileLength = randomFile. length (); // start position of the read object int beginIndex = (fileLength> 4 )? 4: 0; // move the start position of the read file to the beginIndex position. RandomFile. seek (beginIndex); byte [] bytes = new byte [10]; int byteread = 0; // read 10 bytes at a time. If the file content is less than 10 bytes, read the remaining bytes. // Assign the number of bytes read at a time to byteread while (byteread = randomFile. read (bytes ))! =-1) {System. out. write (bytes, 0, byteread) ;}} catch (IOException e) {e. printStackTrace () ;}finally {if (randomFile! = Null) {try {randomFile. close ();} catch (IOException e1) {}}}/** display the remaining bytes in the input stream */private static void showAvailableBytes (InputStream in) {try {System. out. println ("number of bytes in the Current byte input stream:" + 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 );}}