1, read the contents of the file by byte 2, read the contents of the file by character 3, read the contents of the file by line 4, Random Read the contents of the file public class ReadFromFile { /** * Read files in bytes, often used to read binary files, such as pictures, sounds, images, and other files. */ 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, 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 the contents of the file 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); Reads multiple bytes into a byte array, byteread 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) { } } } } /** * Read files in characters, often used to read 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, 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. However, if the two characters are displayed separately, the lines will be changed two times. So, block \ r, or block \ n. Otherwise, there will be a lot more empty lines. if ((char) tempchar)!= ' \ R ') { System.out.print ((char) tempchar); } } Reader.close (); catch (Exception e) { E.printstacktrace (); } try { System.out.println ("read the contents of the file 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 (fileName) (new FileInputStream); Reads multiple characters into a character array, charread the number of characters read at a time while ((Charread = Reader.read (tempchars))!=-1) { Same shielding. \ r does not display 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) { } } } } /** * Read files in behavior 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 a behavioral unit, read one whole line at a time:"); reader = new BufferedReader (new FileReader (file)); String tempstring = null; int line = 1; Read one line at a time until you read NULL to end the file while ((tempstring = Reader.readline ())!= null) { Show line Numbers 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 the contents of the file */ public static void Readfilebyrandomaccess (String fileName) { Randomaccessfile randomfile = null; try { System.out.println ("read the contents of a file randomly:"); Open a random Access file stream, read-only Randomfile = new Randomaccessfile (FileName, "R"); File length, number of bytes Long filelength = Randomfile.length (); Read the starting position of the file int beginindex = (Filelength > 4)? 4:0; Moves 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 file content is less than 10 bytes. Assigns a read number of bytes 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) { } } } } /** * Show 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); } } 5, append content to file tail public class Appendtofile { /** * A method Append file: Use Randomaccessfile / public static void app Endmethoda (string fileName, string content) { try { ///Open a random Access file stream, read-write Randomaccessfile randomfile = new Randomaccessfile (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 (); } } /** * B method Append file: Use FileWriter / public static void Appendmethodb (String fileName, String cont ENT) { try { //Open a write-file, the second argument in the constructor is true to write the file in append form FileWriter writer = new FileWriter (FileName, true); Writer.write (content); Writer.close (); } catch (IOException e) { E.printstacktrace (); } } Pub |