Java code
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("Read file content 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 { 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 in 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 files of text, numbers, etc.*/ public static void readFileByChars(String fileName) { File file = new File(fileName); Reader reader = null; try { System.out.println("Read file content 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, 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. // Therefore, block /r, or block /n. Otherwise, there will be many more blank signs. 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) { // Also blocked /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 file in behavioral units, often used to read formatted files with row-oriented */ public static void readFileByLines(String fileName) { File file = new File(fileName); BufferedReader reader = null; try { System.out.println("Read file content in behavioral units, read one whole line at a time:"); reader = new BufferedReader(new FileReader(file)); String tempString = null; int line = 1; // Read in one line at a time until null is the end of the file while ((tempString = reader.readLine()) != null) { // Show 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) { } } } } /** * Random read file content*/ public static void readFileByRandomAccess(String fileName) { RandomAccessFile randomFile = null; try { System.out.println("Random read a piece of file content:"); // Open a random access file stream, read-only randomFile = new RandomAccessFile(fileName, "r"); // File length, number of bytes long fileLength = randomFile.length(); // Start position of reading file int beginIndex = (fileLength > 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 one 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) { } } } } /** * 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); } }Java code
public class AppendToFile { /** * Method A append file: Use RandomAccessFile */ public static void appendMethodA(String fileName, String content) { try { // Open a random access file stream, and read and 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(); } } /** * Method B appends file: use FileWriter */ public static void appendMethodB(String fileName, String content) { try { //Open a filewriter, the second parameter in the constructor is true to write the file in an appended 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 file AppendToFile.appendMethodA(fileName, content); AppendToFile.appendMethodA(fileName, "append end. /n"); //Show file content ReadFromFile.readFileByLines(fileName); //Append file AppendToFile.appendMethodB(fileName, content); AppendToFile.appendMethodB(fileName, "append end. /n"); //Show file content ReadFromFile.readFileByLines(fileName); } }The above is the full content of Java reading and writing files (simple examples) brought to you by the editor. I hope everyone will support Wulin.com~