1. Read file contents by bytes
public class ReadFromFile {public static void readFileByBytes(String fileName) {File file = new File(fileName);InputStream in = null;try {System.out.println("Read the file content in bytes, one word at a time Section: ");//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 in bytes Content, 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 one 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) {}}}}
2. Read file content by character
public static void readFileByChars(String fileName) {File file = new File(fileName);Reader reader = null;try {System.out.println("Read the file content in character units, 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 /r/n are together, they represent a newline. // But if these two characters are displayed separately, the lines will be changed twice. // So, block /r, or block /n. Otherwise, there will be a lot of blank lines. if (((char) tempchar) != '/r') {System.out.print((char) tempchar);}}reader.close();} catch (Exception e) {e.printStackTrace();} try {System.out.println("Read the file content in character units, 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 shield /r and do 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) {}}}}
3. Read file contents line by line
public static void readFileByLines(String fileName) {File file = new File(fileName);BufferedReader reader = null;try {System.out.println("Read the file content in line units, read a whole line at a time:");reader = new BufferedReader(new FileReader(file));String tempString = null;int line = 1;//Read one line at a time until null is read as 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 e1) {}}}}
4. Randomly read file contents
public static void readFileByRandomAccess(String fileName) {RandomAccessFile randomFile = null;try {System.out.println("Randomly read a file content:");//Open a random access file stream in read-only mode 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;//Move the starting position of reading the 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 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 e1) {}}}}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 the end of the file
public class AppendToFile {public static void appendMethodA(String fileName, String content) {try {//Open a random access file stream, read and write RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");//File length, words Number of sections 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();}}public static void appendMethodB(String fileName, String content) {try {/ /Open a file writer. The second parameter true in the constructor means writing the file in append form. FileWriter writer = new FileWriter(fileName, true);writer.write(content);writer.close();} catch (IOException e) {e.printStackTrace();}} public static void main(String[] args) {String fileName = "C:/temp /newTemp.txt";String content = "new append!";//Append the file according to method AAppendToFile.appendMethodA(fileName, content);AppendToFile.appendMethodA(fileName, "append end. /n");//Display the file content ReadFromFile.readFileByLines(fileName);//Append the file according to method BAppendToFile.appendMethodB(fileName, content);AppendToFile.appendMethodB( fileName, "append end. /n");//Display file content ReadFromFile.readFileByLines(fileName);}}