1. Read File Content in multiple ways.
1. Read File Content by byte
2. Read File Content by character
3. Read File Content by row
4. Randomly Read File Content
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. It is often used to read binary files, files, sounds, images, and other files. * @ Param filename file name */public static void readfilebybytes (string filename) {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, file * @ Param filename file name */public static void readfilebychars (string filename) {file = new file (filename); 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, when the two characters rn are together, it indicates a line break. // If the two characters are displayed separately, the rows are changed twice. // Therefore, R is blocked or N is blocked. 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) {// If (charread = tempchars. Length) is not displayed when R is blocked. & (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, commonly used to read row-oriented formatted files * @ Param filename file name */public static void readfilebylines (string filename) {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 wh Ile (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 * @ Param filename file name */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 (); // the start position of the read object. Int beginindex = (filele Ngth> 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 ){}}}}
2. append the content to the end of the file
Import java. io. fileWriter; import java. io. IOException; import java. io. randomAccessFile;/*** display the remaining bytes in the input stream * @ param in */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) ;}}/*** append the content to the end of the file */public class AppendToFile {/*** A method to append the file: use RandomAccessFile * @ param fileName file name * @ param content to Append content */public static void appendMethodA (String fileName, String content) {try {// to open a random file stream, read/write RandomAccessFile randomFile = new R AndomAccessFile (fileName, "rw"); // file length, number of bytes long fileLength = randomFile. length (); // move the Write File pointer to the end of the file. RandomFile. seek (fileLength); randomFile. writeBytes (content); randomFile. close ();} catch (IOException e) {e. printStackTrace () ;}}/*** Method B: Use FileWriter * @ param fileName * @ param content */public static void appendMethodB (String fileName, String content) {try {// open a file writer. The second parameter in the constructor, true, indicates that the file is written as an append. FileWriter writer = new FileWriter (fileName, true); writer. write (content); writer. close ();} c Atch (IOException e) {e. printStackTrace () ;}} public static void main (String [] args) {String fileName = "C:/temp/newTemp.txt"; String content = "new append! "; // Append the file AppendToFile by method. appendMethodA (fileName, content); AppendToFile. appendMethodA (fileName, "append end. n "); // display the file content ReadFromFile. readFileByLines (fileName); // append the file AppendToFile by method B. appendMethodB (fileName, content); AppendToFile. appendMethodB (fileName, "append end. n "); // display the file content ReadFromFile. readFileByLines (fileName );}}
Operation file instance:
Import java. io. bufferedReader; import java. io. file; import java. io. fileOutputStream; import java. io. fileReader; import java. io. fileWriter; import java. io. IOException; import java. io. outputStreamWriter; import java. io. printWriter; public class operFile {/*** @ param args * @ throws IOException */class data {int id; double x; double y; public data (int id, double x, double y) {super (); this. id = id; this. x = x; this. Y = y;} public int getId () {return id;} public void setId (int id) {this. id = id;} public double getX () {return x;} public void setX (double x) {this. x = x;} public double getY () {return y;} public void setY (double y) {this. y = y ;}} public static data [] readFileByLines (String fileName) {data [] o = new data [52]; File file = new File (fileName ); bufferedReader reader = null; operFile f = new operFile (); try {effece M. 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); if (line> 1) {String [] c = tempString. split (""); int id = Integer. parseInt (c [0]); double x = Double. parseDouble (c [1]); double y = Double. parseDouble (c [2]); data d = f. new data (id, x, y); d. setId (id); d. setX (x); d. setY (y); o [line-1] = d;} line ++;} reader. close ();} catch (IOException e) {e. printStackTrace ();} finally {if (reader! = Null) {try {reader. close () ;}catch (IOException e1) {}} return o;} public static void main (String [] args) {String file = new String ("SAN.txt "); data [] o = readFileByLines (file); for (int I = 1; I <o. length; I ++) {System. out. println (o [I]. getId () + "" + o [I]. getX () + "" + o [I]. getY ());}}}
File Format:
Output: