Java IO stream

Source: Internet
Author: User

Character Stream
FileReader
FileWriter

BufferedReader
BufferedWriter

BYTE stream

InputStream OutputStream

FileInputStream FileOutputStream
Bufferedinputstream Bufferedoutputstream

Import java.io.*;p ublic class copydemo{public static void Main (String args[]) throws Ioexception{copy_2 ();} public static void Copy_1 () throws Ioexception{filewriter fw = new FileWriter ("Copy.txt"); FileReader FR = new FileReader ("Litiepeng.txt"); int ch = 0;while ((Ch=fr.read ())!=-1) fw.write (CH); Fw.close (); Fr.close ( );} public static void Copy_2 () {FileWriter FW = null; FileReader FR = NULL;TRY{FW = new FileWriter ("Copy2.txt"), FR = new FileReader ("Litiepeng.txt"); char[] buf = new char[1024] ;//This may be too big, read the data did not load here int len = 0;while ((Len=fr.read (BUF))!=-1) {fw.write (Buf,0,len);}} catch (IOException e) {throw new RuntimeException ("Read and Write Failed");} Finally{if (fr!=null) {try{fr.close ();} catch (IOException e) {}}if (fw!=null) {try{fw.close ();} catch (IOException e) {}}}}public static void sop (Object obj) {System.out.println (obj);}}

  

Package Pack;import java.io.*;p ublic class Demotwo {public static void main (String args[]) throws IOException {FileReader FR = new FileReader ("Litiepeng.txt");/* defines a character array to store the read characters that read (char []) returns a read to the number of characters char[] buf = new Char[3]; * int num1 = Fr.read (BUF); SOP ("NUM1 =" +new String (BUF)); * If NUM1 returns 1, think about it?  The data in *BUF was originally ABC  last read 1 characters D  then the last buf is DBC. You cannot output the BUF directly at this time. Only the new String (BUF,0,NUM1) can be output;  *  * int num2 = Fr.read (BUF); SOP ("num2 =" +new * String (BUF,0,NUM2));//Why do this, compare it with the above to know */char[] buf = new char[1024 ];int num = 0;while ((num = Fr.read (BUF))! =-1) {SOP (New String (buf, 0, num));} Fr.close ();} public static void Sop (Object obj) {System.out.print (obj);//If the data is printed, it is generally used in print, not println because it can be read one time, but the file is not wrapped in a line,}}

  

Import java.io.*;p ublic class filewriterdemo{public static void Main (String args[]) {FileWriter FW = null;//Why is this on the outside? TRY{FW = new FileWriter ("K:\\demo2.txt"); Fw.write ("Abac");} catch (IOException e) {sop ("catch:" +e.tostring ());} Finally{try{if (Fw!=null)//Why would he want this  ? The FW is not created if the previous exception was thrown, but finally is still executed. There will be an exception. Fw.close (); catch (IOException e) {sop (e.tostring ());}}} public static void Sop (Object obj) {System.out.println (obj);}}

  

/* line break in Windows is represented by 2 symbols, \ r \ n   linux is \n*/------------------------import java.io.*;p ublic class filewriter2{ public static void Main (String args[]) throws Ioexception{filewriter fw = new FileWriter ("Litiepeng.txt", true);// This is if the file exists, append to this file. Fw.write ("nihao\r\n good"); Fw.close ();} public static void Sop (Object obj) {System.out.println (obj);}}

  

Abstract base class for character streams: Reader,writer*/import java.io.*;p ublic class iodemo{public static void Main (String args[]) throws ioexception{ Creates an FileWriter object that must be explicitly manipulated when initialized to a file//file that is created into the specified directory and overwritten if the file exists.//FileWriter FW = new FileWriter ("Demo.txt", True );   FileWriter FW = new FileWriter ("Demo.txt");   Invokes the writer method, writes the string to the stream, but does not yet save the file to Fw.write ("Litiepeng"), flushes the buffer in the Stream object, writes the data in the stream to the file Fw.flush ();//Closes the stream resource, However, the data in the internal buffer is refreshed once before it is closed.//and flush differs after flush refreshes, the stream can continue to be used, and when close refreshes, the stream is closed. Fw.close (); public static void Sop (Object obj) {System.out.println (obj);}}

  

Printstreamsystem.setin (); System.setout ();p ackage pack;import java.io.ioexception;import java.io.printstream;import Java.text.simpledateformat;import Java.util.date;public class Demo{public static void Main (String args[]) throws ioexception{try{int[] arr = new int[2]; System.out.println (Arr[3]);} catch (Exception e) {try{date d = new Date (); SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); String s = sdf.format (d); PrintStream PS = new PrintStream ("Exception.log");p s.println (s);//First print time system.setout (PS);//There will be abnormal generation of}catch ( IOException ee) {throw new RuntimeException ("Log file creation failed");} E.printstacktrace (System.out); SOP ("ddd");}} public static void Sop (Object obj) {System.out.println (obj);}}

  


Java IO stream

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.