Read and Write files in java

Source: Internet
Author: User

Read and Write files in java

// Byte stream import java. io. *; public class testchaer {public static void main (String args []) {FileReader fr = null; FileWriter fw = null; try {fr = new FileReader ("C: /Users/Administrator/Desktop/xuexi/jiankongjiao. py "); fw = new FileWriter (" C:/Users/Administrator/Desktop/xuexi/xuexi11.txt "); char [] buffer = new char [100]; while (true) {int tem = 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 }}}}
// Upload stream import java. io. *; public class inpur {public static void main (String args []) {FileInputStream FD = null; FileOutputStream fos = null; try {FS = new FileInputStream ("C: /Users/Administrator/Desktop/xuexi/jiankongjiao. py "); fos = new FileOutputStream (" C:/Users/Administrator/Desktop/xuexi/xuexi2.txt "); byte [] buffer = new byte [100]; while (true) {int temp = FCM. read (buffer, 0, buffer. length); if (temp =-1) {break;} fos. write (buffer, 0, temp); String stri = new String (buffer); // call the tim method of the string object, the Ending Space of string and the empty string stri = stri. trim (); System. out. println (stri) ;}} catch (Exception e) {// TODO: handle exception System. out. println (e);} finally {try {FCM. close (); fos. close ();} catch (Exception e) {System. out. println (e );}}}}

 

One row is read,

package ifwh;import java.io.*;public class teatsjaa { public static void main(String agrs[]) {    FileReader fileReader=null;    BufferedReader  bufferedWriter=null;    try {        fileReader=new FileReader("C:/Users/Administrator/Desktop/xuexi/jiankongjiao.py");        bufferedWriter =new BufferedReader(fileReader);        String line=null;        while (true) {            line=bufferedWriter.readLine();            if (line==null){                break;            }            System.out.println(line);        }    } catch (Exception e) {        // TODO: handle exception        System.out.println(e);    }finally {        try {            fileReader.close();            bufferedWriter.close();        } catch (Exception e2) {            // TODO: handle exception            System.out.println(e2);        }    }}}

 

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.