Output streams in Java

Source: Internet
Author: User

1, the output stream of three ways

1) output by character, overwrite the original content

2) output by Byte, overwrite the original content

3) do not overwrite the original content

Package Output;import Java.io.bufferedwriter;import Java.io.file;import java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.filewriter;import Java.io.ioexception;import Java.io.OutputStream;import Java.io.printwriter;public class Outputtest {public static void main (string[] args) {File file = new file ("C:\\users\\admi Nistrator\\desktop\\zzz.txt ");//Characterwriter (file," abc ");//bytewriter (file," abc "); CharacterWriter2 (file, "123");}  /* * 1, output by character, the contents of the output directly overwrite the previous text */public static void Characterwriter (file file, String count) {BufferedWriter BufferedWriter = null;try {bufferedwriter = new BufferedWriter (new PrintWriter (file)); Bufferedwriter.write (count); Bufferedwriter.flush ();} catch (IOException e) {e.printstacktrace ();} finally {if (bufferedwriter! = null) {try {bufferedwriter.close ();} catch (IO Exception e) {e.printstacktrace ();}}}} /* * 2, output by byte, overwrite original content */public static void Bytewriter (file file, String count) {OutputStream OutputStream = null;try {output Stream =New FileOutputStream (file); Outputstream.write (Count.getbytes ()); Outputstream.flush ();} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} finally {if ( OutputStream = null) {try {outputstream.close ();} catch (IOException e) {e.printstacktrace ();}}}} /* * 3, the content of the output does not overwrite the previous content */public static void CharacterWriter2 (file file, String count) {BufferedWriter bufferedwriter = null; try {bufferedwriter = new BufferedWriter (new FileWriter (file, True)); Bufferedwriter.write (count); Bufferedwriter.flush ();} catch (IOException e) {e.printstacktrace ();} finally {if (bufferedwriter! = null) {try {bufferedwriter.close ();} catch (IO Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}}


Output streams in Java

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.