1. Read by byte
2. Read by character
3, read the text by line
4. Random Reading
import java.io.bufferedreader;import java.io.file;import java.io.fileinputstream;import java.io.filereader;import java.io.ioexception;import java.io.inputstream;import Java.io.inputstreamreader;import java.io.randomaccessfile;import java.io.reader;public class ReadFromFile {/** * reads files in bytes, commonly used for reading binary files, slices, sounds, images, etc. */public static void readfilebybytes (String filename) {File file = new file (filename); Inputstream in = null;try {system.out.println ("reads the contents of the file in bytes, Read 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&NBSP;{SYSTEM.OUT.PRINTLN ("reads the contents of the file in bytes, reads multiple bytes at a time:");// reads multiple bytes at a time byte[] tempbytes = new byte[100];int Byteread = 0;in = new fileinputstream (FileName); Readfromfile.showavailablebytes (in);// reads into multiple bytes into a byte array, Byteread is the number of bytes read in at one time while (byteread = In.read (tempbytes)) != -1) {system.out.write (Tempbytes, 0, byteread);}} catch (EXCEPTION&NBSP;E1) {e1.printstacktrace ();} finally {if (In != null) {try {in.close ();} catch (ioexception e1) {}}}}/** * read files in characters, commonly used for reading text, numbers and other types of files */public static void readfilebychars (String filename) {File file = new File (FileName); Reader reader = null;try {system.out.println ("read the contents of the file in characters, read 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 These two characters together, represent a newline. But if the two characters are displayed separately, the lines are changed two times. becauseThis, shield off \ r, or block \ n. Otherwise, there will be a lot more empty rows. if ((char) tempchar) != ' \ R ') {system.out.print ((char) tempchar);}} Reader.close ();} catch (exception e) {e.printstacktrace ();} TRY&NBSP;{SYSTEM.OUT.PRINTLN ("read file contents in characters, read multiple bytes at a time:");// read multiple characters at once Char[] tempchars = new char[30];int charread = 0;reader = new inputstreamreader (new FileInputStream (fileName);// reads multiple characters into a character array, charread the number of characters to read while (Charread = reader.read ( Tempchars) != -1) {// also shield off \ 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&NBSP;E1) {e1.printStackTrace ();} finally {if (Reader != null) {try {reader.close ();} catch (ioexception e1) {}}}}/** * read files in behavioral units, often used to read line-oriented format files */public Static void readfilebylines (string filename) {file file = new file ( FileName); Bufferedreader reader = null;try {system.out.println ("read the contents of the file in the Act unit, read a whole line at a time:"); reader = new bufferedreader (New filereader (file)); string tempstring = null;int line = 1;// reads one line at a time until NULL is read into the end of the file while ( Tempstring = reader.readline ()) != null) {// Display line number System.out.println ("line " + line + ": " + tempstring); line++;} Reader.close ();} catch (ioexception e) {e.printstacktrace ();} finally {if (Reader != null) {try {reader.close ();} catch (IOEXCEPTION&NBSP;E1) {}}}/** * Random Read file contents */public static void readfilebyrandomaccess (String fileName) {randomaccessfile randomfile = null;try {system.out.println ("Random reading of a file content:");// Open a random Access file stream, read-only Randomfile = new randomaccessfile (filename, "R");// file length, number of bytes long filelength = randomfile.length ();// The starting position of the read file int beginindex = (fileLength &NBSP;>&NBSP;4) ? 4 : 0;// Move the start position of the read file to the Beginindex location. Randomfile.seek (beginindex); byte[] bytes = new byte[10];int byteread = 0;// Read 10 bytes at a time and read the remaining bytes if the contents of the file are less than 10 bytes. Assigns the number of bytes read to bytereadwhile ((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&NBSP;E1) {}}}}/** * shows the number of bytes left in the input stream */private static void Showavailablebytes (Inputstream in) {try {system.out.println (the number of bytes in the current byte input stream is: " + 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);}}
String strpath = "D:/java/apache-tomcat-7.0.40/running.txt";
Readfromfile.readfilebybytes (strpath);
From a good-hearted person on the Internet.
This article is from the "climb over the mountains to see the Sea" blog, please be sure to keep this source http://670176656.blog.51cto.com/4500575/1699232
Java file read