1 Packagefile operation;2 3 ImportJava.io.File;4 ImportJava.io.FileOutputStream;5 Importjava.io.IOException;6 ImportJava.io.OutputStream;7 ImportJava.io.OutputStreamWriter;8 9 Public classTestoutputstreamwriter {Ten Public Static voidMain (string[] args)throwsIOException { OneFile file=NewFile ("D:" +file.separator+ "Test.txt"); AOutputStream out =NewFileOutputStream (file);//If the file does not exist, it will be created automatically - //the byte stream is converted to a character stream. The contents of the file are essentially in the form of a byte operation in the operating system, which requires implicit conversion in the buffer when processing Chinese -OutputStreamWriter outputstreamwriter=NewOutputStreamWriter (out); theString str= "welcom! China"; - outputstreamwriter.write (str); - //Outputstreamwriter.flush ();//content is written into the file after the flush () or close () operation - outputstreamwriter.close (); + } -}
OutputStreamWriter
1 Packagefile operation;2 3 ImportJava.io.File;4 ImportJava.io.FileInputStream;5 Importjava.io.IOException;6 ImportJava.io.InputStream;7 ImportJava.io.InputStreamReader;8 9 Public classTestinputstreamreader {Ten Public Static voidMain (string[] args)throwsIOException { OneFile file=NewFile ("D:" +file.separator+ "com" +file.separator+ "test.txt"); A Char[] chars=New Char[1024]; - if(File.exists ()) { - //file.getparentfile (). Mkdirs ();//It is recommended to use mkdirs instead of Mkdir,mkdirs to create a single-level catalog and to create multilevel catalogs, mkdir can only create single-level catalogs the //file.createnewfile (); -InputStream in=Newfileinputstream (file); -InputStreamReader inputstreamreader=NewInputStreamReader (in); - intlen=Inputstreamreader.read (chars); +System.out.println ("*" +NewString (Chars,0,len) + "*"); - inputstreamreader.close (); + } A } at}
InputStreamReader
The bytes of the "Java" IO stream are streamed to a character stream: Java.io.OutputStreamWriter and Java.io.InputStreamReader