Java Learning notes I/O

Source: Internet
Author: User

Package Com.chinasofti.javase20160819;import Java.io.bufferedreader;import java.io.bufferedwriter;import Java.io.file;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.FileWriter;import Java.io.ioexception;import Java.io.inputstreamreader;import java.io.UnsupportedEncodingException; Public classIotest { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        int[] Arr=readfile ("D:\\myft\\demo\\javase20160819\\src\\com\\chinasofti\\javase20160819\\test.txt");  for(intV:arr) System. out. Print (Char) v); WriteFile ("D:\\myft\\demo\\javase20160819\\src\\com\\chinasofti\\javase20160819\\test1.txt", arr); String Str="AFFJAISFI and drops will be a"; WriteFile ("D:\\myft\\demo\\javase20160819\\src\\com\\chinasofti\\javase20160819\\test1.txt", str); }    /** * Read the file, return int[], encode the characters in the file * @param panth*/    Private Static int[] ReadFile (String panth) {File dir=NewFile (Panth); FileInputStream fil=NULL; InputStreamReader inStr=NULL; BufferedReader BUFFR=NULL; intLength = (int) dir.length (); int[] arr =New int[length]; Try{fil=NewFileInputStream (dir); InStr=NewInputStreamReader (fil,"GBK");//Transit stream, convert bytes into character stream BUFFR=NewBufferedReader (INSTR); Arr=ReadAll (buffr,length); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(unsupportedencodingexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{            if(BUFFR! =NULL){                Try{buffr.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }            if(InStr! =NULL){                Try{instr.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }            if(Fil! =NULL){                Try{fil.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }        }        returnarr; }        /** * Read all the characters in the file, return * @param BUFFR * @param length of file in int[] type, you can get * @return using the File object's length () method */    Private Static int[] ReadAll (BufferedReader BUFFR,intlength) {        intindex=0; int[] arr=New int[length];  while(index<length) {            Try{Arr[index]=Buffr.read (); Index++; } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }        returnarr; }        /** * Write to File * @param panth * @param arr: int type encoding to write characters*/    Private Static voidWriteFile (String Panth,int[] arr) {File dir=NewFile (Panth); /** Determine if the file exists*/        if(!dir.exists ()) {            Try{dir.createnewfile (); } Catch(IOException E1) {//TODO auto-generated Catch blockE1.printstacktrace (); }} FileWriter fil=NULL; BufferedWriter BUFFW=NULL; Try{fil=NewFileWriter (dir); BUFFW=NewBufferedWriter (fil);            Writeall (Buffw,arr); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{                if(BUFFW! =NULL){                    Try{buffw.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }                }                if(Fil! =NULL){                    Try{fil.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }                }            }                }        /** * Write to File * @param panth * @param string: Strings that need to be written to the file*/    Private Static voidWriteFile (String panth,stringstring) {File dir=NewFile (Panth); /** Determine if the file exists*/        if(!dir.exists ()) {            Try{dir.createnewfile (); } Catch(IOException E1) {//TODO auto-generated Catch blockE1.printstacktrace (); }} FileWriter fil=NULL; BufferedWriter BUFFW=NULL; Try{fil=NewFileWriter (dir); BUFFW=NewBufferedWriter (fil); Buffw.write (string); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{            if(BUFFW! =NULL){                Try{buffw.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }            if(Fil! =NULL){                Try{fil.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }        }                /*char[] Arr=string.tochararray (); for (char V:arr) {}*/    }        /** * Write all Files * @param buffw * @param arr*/    Private Static voidWriteall (BufferedWriter BUFFW,int[] arr) {         for(intI=0; i<arr.length;i++){            Try{buffw.write (arr[i]); if(Arr[i] = = -) Buffw.write (Ten); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }    }}

Java Learning notes I/O

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.