package test;import java.io.bufferedreader;import java.io.bufferedwriter;import java.io.filereader;import java.io.filewriter;import java.io.ioexception;import Java.io.randomaccessfile;public class file { public static void main (String [] args] { try{ //system.out.println (Bufferedreadertest ());// Bufferedwritertest ("Hello,world"); //appendmethoda ("1.txt", "Hello"); //appendmethodb ("1. TXT "," hello "); }catch (exception e) { e.printstacktrace (); } } public static string bufferedreadertest () throws ioexception{ filereader fr=new filereader ("1.txt"); bufferedreader br=new BufferedReader (FR); string s= ""; string a=br.readline (); while (a!= NULL) { s=s+a; a=br.reAdline (); } br.close (); fr.close (); return s; } public static void bufferedwritertest (string s) throws Ioexception{ filewriter fw=new filewriter ("1.txt"); bufferedwriter bw=new bufferedwriter (FW); bw.write (s); bw.close (); fw.close (); } public static void appendmethodb (string filename, string content) { try { // Open a write file, the second parameter in the constructor true indicates that the file is written in append form &nbsP; filewriter writer = new filewriter (filename, true); writer.write (content); Writer.close (); } catch (IOException e) { e.printstacktrace (); } } public Static void appendmethoda (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 (); //moves the write file pointer to the end of the file. randomfile.seek (filelength); randomfile.writebytes (content); randomfile.clOSE (); } catch (ioexception e) { e.printstacktrace (); } }}
Java read-write file sample