Java file read/write

Source: Internet
Author: User
Tags stringbuffer

Import Java.io.bufferedreader;import Java.io.bufferedwriter;import Java.io.datainputstream;import Java.io.dataoutputstream;import Java.io.file;import Java.io.fileinputstream;import java.io.FileNotFoundException; Import Java.io.fileoutputstream;import java.io.filereader;import java.io.filewriter;import java.io.IOException; Import Java.io.outputstream;import java.nio.charbuffer;import Java.util.arraylist;import java.util.List;public Class Filedemo {public static void main (string[] args) {file File=new file ("C:\\22ssssssssss.txt"); try {if (!file.exists ( )) {file.createnewfile (); FileOutputStream fileoutputstream=new fileoutputstream (file);D Ataoutputstream fdataoutputstream=new DataOutputStream (FileOutputStream)//fdataoutputstream.write ("SFS aha D". GetBytes ()); Fdataoutputstream.writeutf ("\ NX haha "); Fdataoutputstream.close (); Fileoutputstream.close ();} else {//NOTE 1: Generally read as far as possible in the format of writing when read,//Otherwise there may be garbled display or program exception. If the first write to the file with writeUTF (), when reading if not with readUTF () will appear garbled,//if READUTF () read the content is not UTF-8 format, the program will throw an exception. FileinpuTStream fileinputstream=new fileinputstream (file);D Atainputstream datainputstream=new DataInputStream (  FileInputStream);  byte [] b2=new byte[1024*120]; int Len=datainputstream.read (B2); Reads the remaining content by bytes//System.out.println (new String (B2,0,len));               System.out.println (Datainputstream.readutf ());               Datainputstream.close (); Fileinputstream.close ();}} catch (Exception e) {//todo:handle exception}}/** * Cache stream * @param args */public static void Main3 (string[] args) {File fi Le=new File ("C:\\22ss.txt"); try {if (!file.exists ()) {file.createnewfile (); FileWriter fwriter=new FileWriter (file); BufferedWriter bwriter=new BufferedWriter (fwriter); String string= "Sfjsafjas wedding dress fujian \ n"; bwriter.write (string); Bwriter.write (string); Bwriter.close (); Fwriter.close ();} else {filereader freader=new filereader (file); BufferedReader bufferedreader=new BufferedReader (Freader); while (Bufferedreader.ready ()) {String string= Bufferedreader.readline (); System.err.println (string);} Bufferedreader.close (); fReAder.close ();}} catch (Exception e) {//todo:handle exception}}/** * Character stream * @param args */public static void Main2 (string[] args) {File fi Le=new File ("C:\\demo1fsdfsd.txt"); try {if (!file.exists ()) {file.createnewfile (); FileWriter fwriter=new FileWriter (file); String string= "Sfjsafjas wedding in Fujian"; Fwriter.write (string); Fwriter.close (); else {filereader freader=new filereader (file); char[] C=new char[1024];  StringBuffer sbuilder=new stringbuffer (); int len=freader.read (c); while (Len!=-1) {freader.read (c, 0, Len); Sbuilder.append (c); Len=freader.read (); System.out.println (New String (c));}}} catch (Exception e) {//todo:handle exception}}/** * byte stream * @param args */public static void Main1 (string[] args) {File fi Le=new File ("C:\\demo1.txt"); try {if (!file.exists ()) {file.createnewfile (); FileOutputStream fostream=new fileoutputstream (file); byte[] b= "SJD horizontal lance poem defensive play Try It". GetBytes (); Fostream.write (b); Fostream.close ();} else {fileinputstream fisfileinputstream=new fileinputstream (file); byte[] B=new byte[fisfileinputStream.available ()];fisfileinputstream.read (b); System.out.println (New String (b)); Fisfileinputstream.close ();}} catch (IOException e) {//Todo:handle exceptionSystem.err.println (E.getmessage ());}}}

  

Java file read/write

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.