Java Read and write files

Source: Internet
Author: User

//Byte streamImportJava.io.*; Public classTestchaer { Public Static voidMain (String args[]) {FileReader fr=NULL; FileWriter FW=NULL; Try{FR=NewFileReader ("c:/users/administrator/desktop/xuexi/jiankongjiao.py"); FW=NewFileWriter ("C:/users/administrator/desktop/xuexi/xuexi11.txt"); Char[] buffer=New Char[100];  while(true) {            intTem=fr.read (buffer, 0, buffer.length); if(Tem==-1){                 Break; } fw.write (Buffer,0, TEM); }    } Catch(Exception e) {System.out.println (e); }    finally {        Try{fr.close ();        Fw.close (); } Catch(Exception E2) {//Todo:handle Exception        }    }}}
//character StreamImportJava.io.*; Public classInpur { Public Static voidMain (String args[]) {FileInputStream fis=NULL; FileOutputStream Fos=NULL; Try{FIS=NewFileInputStream ("c:/users/administrator/desktop/xuexi/jiankongjiao.py"); FOS=NewFileOutputStream ("C:/users/administrator/desktop/xuexi/xuexi2.txt"); byte[] buffer=New byte[100];  while(true){               intTemp=fis.read (buffer,0, buffer.length); if(Temp==-1){                    Break; } fos.write (Buffer,0, temp); String Stri=NewString (buffer); //calling the String object's Tim method will wrap the string's closing space and empty stringStri=Stri.trim ();           System.out.println (Stri); }         }       Catch(Exception e) {//Todo:handle ExceptionSystem.out.println (e); }       finally{            Try{fis.close ();            Fos.close (); } Catch(Exception e) {System.out.println (e); }            }}}

A line of reading,

 PackageIFWH;ImportJava.io.*; Public classTeatsjaa { Public Static voidMain (String agrs[]) {FileReader FileReader=NULL; BufferedReader BufferedWriter=NULL; Try{FileReader=NewFileReader ("c:/users/administrator/desktop/xuexi/jiankongjiao.py"); BufferedWriter=NewBufferedReader (FileReader); String Line=NULL;  while(true) { line=Bufferedwriter.readline (); if(line==NULL){                 Break;        } System.out.println (line); }    } Catch(Exception e) {//Todo:handle ExceptionSystem.out.println (e); }finally {        Try{filereader.close ();        Bufferedwriter.close (); } Catch(Exception E2) {//Todo:handle ExceptionSystem.out.println (E2); }    }}}

Java Read and write files

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.