Read of file: public static void Duqu (String path) throws ioexception{file file=new file (path); FileInputStream fis=new fileinputstream (file); InputStreamReader isr=new inputstreamreader (FIS, "gb2312"); BufferedReader br=new BufferedReader (ISR); StringBuffer buf=new StringBuffer (); String Str=null;while ((Str=br.readline ())!=null) {buf.append (str); Buf.append ("\ r \ n");} System.out.println (Buf.tostring ()); Br.close ();} Write to file: public static void Write (string path, string content) {string s = new String (); string S1 = new String (); try {file F = new File (path); if (f.exists ()) {System.out.println ("file exists"); } else {System.out.println ("file does not exist, creating ..."); if (F.createnewfile ()) {System.out.println ("file created successfully! "); } else {System.out.println ("File creation failed! "); }} BuffeRedreader input = new BufferedReader (new FileReader (f)); while ((s = input.readline ()) = null) {S1 + = s + "/n"; } input.close (); S1 + = content; BufferedWriter output = new BufferedWriter (new FileWriter (f)); System.out.println ("File contents:" + S1); Output.write (S1); Output.close (); } catch (Exception e) {e.printstacktrace (); } }
Java IO Read and write operations