Java IO stream One

Source: Internet
Author: User
Tags readfile

/* for IO input and output streams there are two major categories: 1, Byte stream (more commonly used) 2, character stream (refers to the text (including Chinese and English, etc.), the character stream and the word stream is the difference between the character stream object is the fusion of encoding, that is, the text in which way encoding, to avoid garbled. Commonly used four abstract base classes: Byte stream: InputStream, outputstream character stream: Reader, writer the subclass of four base class is the name of the four base class as the suffix, so it is easier to identify the child class. Since IO streams are used to manipulate data, the most common form of data is as follows: The file is illustrated in this example by manipulating the file. Requirement: On the hard disk, create the file and write the text data. */import java.io.*;class Filewriterdemo {public static void main (string[] args) throws ioexception{/* creates a FileWriter object, Once the object is initialized, the file must be explicitly manipulated. And the file is created in the specified directory. If a file with the same name already exists in the directory, the original file will be overwritten. In fact, the step is to clear the destination of data to be stored. */filewriter fw=new FileWriter ("D:\\myfile\\111.txt");/* Call the Write method to write the text into the stream */fw.write ("Hello world\n");/* Refreshes the data in the buffer in the Stream object. Brushes the data to the destination. */fw.flush (); Fw.write ("HDGBJSHFJSDV,CHVJSDHC");/* Closes the stream resource, but refreshes the data in the internal buffer one time before closing. Brushes the data to the destination. The difference between close () and flush (): When close refreshes, the stream is closed, and after flush refreshes, the stream can continue to be used. */fw.close ();}}
Import Java.io.*;class filewriterdemo1{public static void Main (string[] args) {WriteFile (); ReadFile ();} public static void WriteFile () {FileWriter fw=null;try{fw=new FileWriter ("D:\\myfile\\mycode\\1.txt", true);// True indicates that if the file already exists, the file is subsequently written without overwriting the original file. Fw.write ("\r\ndemetria");} catch (IOException e) {System.out.println (e.tostring ());} Finally{try{if (Fw!=null)//This must be judged whether it is empty, and then close the file Fw.close ();} catch (IOException e) {System.out.println (e.tostring ());}}} public static void ReadFile () {FileReader fr=null;int ch=0;try{fr=new filereader ("D:/myfile/mycode/1.txt"), while (ch= Fr.read ())!=-1) {SOP ("ch=" + (char) ch);} /*while (True) {ch=fr.read (); if (Ch==-1) Break;sop ((char) ch);} */}catch (IOException e) {sop (e.tostring ());} Finally{try{if (Fr!=null) Fr.close ();} catch (IOException e) {sop (e.tostring ());}}} public static void Sop (Object obj) {System.out.println (obj);}}

/* The second method uses a character array to get each word designators to be read in a defined character array. */import java.io.*;class FileWriterDemo2 {public static void main (string[] args) {WriteFile (); ReadFile ();} public static void WriteFile () {FileWriter fw=null;try{fw=new FileWriter ("D:/myfile/mycode/2.txt", true); Fw.write (" Helloworld! ");} catch (IOException e) {sop (e.tostring ());} Finally{try{if (Fw!=null) Fw.close ();} catch (IOException e) {sop (e.tostring ());}}} public static void ReadFile () {FileReader fr=null;try{fr=new filereader ("D:\\myfile\\mycode\\2.txt"); char[] Ch=new Char[1024];int Num=0;while ((Num=fr.read (CH))!=-1) {SOP (New String (Ch,0,num));}} catch (IOException e) {sop (e.tostring ());} Finally{try{if (Fr!=null) Fr.close ();} catch (IOException e) {sop (e.tostring ());}}} public static void Sop (Object obj) {System.out.println (obj);}}

/* Copy a text file from the C drive to the D drive. The principle of copying: In fact, the C disk under the file data is stored in the D-disk file. Step: 1, create a new file in the D drive. Used to store data in C-disk files, 2, define read stream and C-Drive file association, 3, through continuous reading and writing to complete the data storage. */import java.io.*;class FileWriterDemo4 {public static void main (string[] args) throws Ioexception{filewriter Fw=null; FileReader fr=null;try{fw=new FileWriter ("D:/myfile/mycode/newdemo.txt");//define the Write character stream object Fr=new FileReader ("c:/test/ Old.txt "); int num=0;char[] ch=new char[1024];int count=0;while ((Num=fr.read (CH))!=-1) {Fw.write (new String (Ch,0,num) ); count++;} System.out.println ("Count:" +count),//count A value of 1 indicates that the loop only walked once. }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) {}}}}



Java IO stream One

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.