English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Java code
public class ReadFromFile { /** * Reading files byte by byte, commonly used for reading binary files such as images, sounds, videos, and other files. */ public static void readFileByBytes(String fileName) { File file = new File(fileName); InputStream in = null; Open a FileWriter, the second parameter true in the constructor indicates that the file is written in append mode System.out.println("Read file content byte by byte, 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(); } e.printStackTrace(); return; } Open a FileWriter, the second parameter true in the constructor indicates that the file is written in append mode System.out.println("Read file content by byte, read multiple bytes at a time:"); // Read multiple bytes at once byte[] tempbytes = new byte[100]; int byteread = 0; in = new FileInputStream(fileName); ReadFromFile.showAvailableBytes(in); // Read multiple bytes into a byte array, where byteread represents 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(); } if (in != null) { Open a FileWriter, the second parameter true in the constructor indicates that the file is written in append mode in.close(); } catch (IOException e1) { } } } } /** * Read files by character, 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; Open a FileWriter, the second parameter true in the constructor indicates that the file is written in append mode System.out.println("Read file content by character, 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, the \r\n characters together represent a line break. // But if these two characters are displayed separately, they will cause a line break twice. // Therefore, the \r character should be masked, or the \n character should be masked. Otherwise, many blank lines will appear. if (((char) tempchar) != '\r') { System.out.print((char) tempchar); } } reader.close(); } catch (Exception e) { e.printStackTrace(); } Open a FileWriter, the second parameter true in the constructor indicates that the file is written in append mode System.out.println("Read file content by character, 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)); // Read multiple characters into a character array, where charread represents the number of characters read at a time while ((charread = reader.read(tempchars)) != -1) { // Similarly, mask and do not display \r 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(); } if (reader != null) { Open a FileWriter, the second parameter true in the constructor indicates that the file is written in append mode reader.close(); } catch (IOException e1) { } } } } /** * Read file by lines, commonly used for reading formatted files oriented to lines */ public static void readFileByLines(String fileName) { File file = new File(fileName); BufferedReader reader = null; Open a FileWriter, the second parameter true in the constructor indicates that the file is written in append mode System.out.println("Read file content by lines, read one entire 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 file end while ((tempString = reader.readLine()) != null) { // Display line number System.out.println("line "); + line + : " + tempString); line++; } reader.close(); } e.printStackTrace(); } if (reader != null) { Open a FileWriter, the second parameter true in the constructor indicates that the file is written in append mode reader.close(); } catch (IOException e1) { } } } } /** * Randomly read file content */ public static void readFileByRandomAccess(String fileName) { RandomAccessFile randomFile = null; Open a FileWriter, the second parameter true in the constructor indicates that the file is written in append mode System.out.println("Randomly read a segment of 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(); // The starting position of reading the file int beginIndex = (fileLength > 4) &63; 4 : 0; // Move the read file start position to the beginIndex position. randomFile.seek(beginIndex); byte[] bytes = new byte[10]; int byteread = 0; // Read once10bytes, if the file content is not enough10bytes, then read the remaining bytes. // Assign the number of bytes read in one read to byteread while ((byteread = randomFile.read(bytes)) != -1) { System.out.write(bytes, 0, byteread); } } e.printStackTrace(); } if (randomFile != null) { Open a FileWriter, the second parameter true in the constructor indicates that the file is written in append mode Append file using method B: using FileWriter } catch (IOException e1) { } } } } /** * Display the number of bytes remaining in the input stream */ private static void showAvailableBytes(InputStream in) { Open a FileWriter, the second parameter true in the constructor indicates that the file is written in append mode System.out.println("The number of bytes available in the current byte input stream is: "); + in.available()); } 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); } }
Java code
public class AppendToFile { /** * A method to append to a file: using RandomAccessFile */ public static void appendMethodA(String fileName, String content) { Open a FileWriter, the second parameter true in the constructor indicates that the file is written in append mode // Open a random access file stream in read-write mode 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); Append file using method B: using FileWriter } e.printStackTrace(); } } /** * public static void appendMethodB(String fileName, String content) { */ try { Open a FileWriter, the second parameter true in the constructor indicates that the file is written in append mode //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 file using method A AppendToFile.appendMethodA(fileName, content); AppendToFile.appendMethodA(fileName, "append end. \n"); //Display file content ReadFromFile.readFileByLines(fileName); //Append file using method B AppendToFile.appendMethodB(fileName, content); AppendToFile.appendMethodB(fileName, "append end. \n"); //Display file content ReadFromFile.readFileByLines(fileName); } }
That's all for the ways of reading and writing files in Java brought to you by the editor (simple example). Hope everyone will support and cheer for the tutorial~