Java IO operations

Source: Internet
Author: User

Tag:java    program    io stream    

IO: input/output stream      (read in write out)/* *  use file byte stream to read data to program  */public class  Testfileinputstream {public static void main (String[] args)  throws  ioexception {//prepare the File object     f is the source file that will be read File f = new file ("file/ Abc.txt ");//Prepare the Byte input stream object fileinputstream fis = null;try {//convert the source file f into a byte input stream fis = new  fileinputstream (f);//Get data from the stream       read a byte//int read = fis.read (); /read one byte array at a time//first prepare an empty cup byte[]bs = new byte[(int) f.length ()];//store the data in the stream into BS Fis.read (BS);// Presents the data in a byte array     (converts a byte array to a string) string str = new string (BS); System.out.println (str);}  catch  (filenotfoundexception e)  {e.printstacktrace ();} Finally{fis.close ();}}} --------------------------------------------------------------------------/* *  writing data from a program to a file   */public class testfileoutputstream&nbsP {Public static void main (String[] args)  {//creates a target file.     writes data to F in File f = new file ("File/def.txt"); fileoutputstream fos = null;//prepare the file output stream try {//Create a file output stream object and specify the file to which the data will be written//fileoutputstream ( F,true)//f represents the target file, true indicates whether to append Fos = new fileoutputstream (f,true);//writes data to a file string str =   "Xiao Ming, what's the weather like today?" ";//first string to byte array byte[] bs = str.getbytes ();//write data from byte array to file Fos.write (BS);//Brush Cache Fos.flush ();}  catch  (exception e)  {e.printstacktrace ();} Finally{try {if (Null!=fos) {fos.close ();}}  catch  (ioexception e)  {e.printstacktrace ();}}} -------------------------------------------------------------------------/** *  Use buffered streams to read data  */ Public static void main (String[] args)  {bufferedreader br = null;try  {//creates a buffered stream object that specifies the file to read Br = new bufferedreader (New filereader (New file ("FIle/abc.txt "));//operation of the file String str =  br.readline ();//read a row of data directly while (Str!=null) {// The loop reads System.out.println (str) without being empty; Str = br.readline ();}}  catch  (exception e)  {e.printstacktrace ();} Finally{if (NULL!=BR) {try {//only need to close the buffered stream object, FileReader object is closed together, no need to manually close Br.close ();}  catch  (ioexception e)  {e.printstacktrace ();}}}} ----------------------------------------------------------------------------------/** *  uses the character buffer stream, Read/write (copy) the file  */public static void main (String[] args)  {//first file/the file The data in the Def.txt is read out bufferedreader br = null;//buffered output stream object Bufferedwriter bw = null;try  {/*file file = new file ("File/def.txt");//To have a FileReader object, you need to have a file object first FILEREADER FR  = new filereader (file);//To create a buffered stream object, you must first have the Reader object Br = new bufferedreader (FR); */// Build file input Stream object Br = new bufferedreader (New filereader (New file ("File/def. txt "));//build file output stream object, specify to write data to D-disk in Aaa.txt Bw = new bufferedwriter (New filewriter (new  File ("D:/aaa.txt"));//Use buffered stream to read a row of data string str = br.readline (); while (str!=null) {// Write the string read above directly to the file Bw.write (str); Bw.newline ();//newline Bw.flush ();//Brush stream//Continue to read the data Str = br.readline ();}}  catch  (exception e)  {e.printstacktrace ();} finally{try {//Advanced Post-out if (bw!=null) {bw.close ();} if (br!=null) {br.close ();}}  catch  (ioexception e)  {e.printstacktrace ();}}} -------------------------------------------------------------------------------/** *  *  @author  Administrator  character input stream:         the class of the stream ending in reader must be a character input stream         filereader: File character stream     bufferedreader: buffered character stream  */public class testreader {public static void main (String[] args)  {//Create a character input stream object Filereader fr = null;try {//creates a character input stream object and specifies which file object the operation is Fr = new filereader (New file ("File/abc.txt"));// Reads data using the character input stream//int read = fr.read ();//reads a character//prepares a character array char[]chs = new char[10];// Reads the data into a character array, and Len represents the number of characters read into the array int len = fr.read (CHS);//loop reads the data out while (len!=-1) {//len=- 1 The delegate reads the end of the file//converts the data from each read to the character array into a string//     starts from the first element of the array, and Len represents the number of characters in the array string str =  new string (Chs,0,len); System.out.println (str);//continue to loop read Len = fr.read (CHS);}}  catch  (exception e)  {e.printstacktrace ();} Finally{if (NULL!=FR) {try {fr.close ();}  catch  (ioexception e)  {e.printstacktrace ();}}}} ----------------------------------------------------------------------------------/* *  configuration file reads and uses The key and value in the  * properties file are string types  *  * */public class testproper  {public static void main (String[] args)  {fileinputstream fis = null; properties pp = null;try {//constructs an input stream object Fis = new fileinputstream ("file/ Abc.properties ");//Create Properties Object Pp = new properties ();//Use profile object to stream load to pp.load (FIS);// Obtain Data String name = pp.getproperty ("name") from PP; String age = pp.getproperty ("Age"); String hobbys = pp.getproperty ("hobby");//Will love Hobbys into a few separate parts    "dance, eat, sing" string[]  hobs = hobbys.split (","); System.out.println (Name+age+hobbys);}  catch  (exception e)  {e.printstacktrace ();} Finally{try {fis.close ();}  catch  (ioexception e)  {e.printstacktrace ();}}} Review previous       (keep blogging every day)


Java IO operations

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.