Io filewriter filereader Learning

Source: Internet
Author: User
import java.awt.Frame;import java.io.*;public class filewriter {/** * @param args */public static void main(String[] args) throws IOException{// TODO Auto-generated method stub//test1();test2();}static void test1()throws IOException{FileWriter fw = new FileWriter("F:\\fd.txt");fw.write("sfasf");fw.flush();fw.write("zzzzz");fw.flush();fw.close();fw = new FileWriter("F:\\fd.txt", true);fw.write("cccc");fw.flush();fw.close();}static void test2(){FileWriter fw = null;try {fw = new FileWriter("F:\\fd2.txt");fw.write("wzz");} catch (IOException e) {// TODO: handle exceptionSystem.out.println(e.toString());}finally{try {if(fw!=null)fw.close();} catch (IOException e2) {// TODO: handle exceptionSystem.out.println(e2.toString());}}}}
import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class filereader {/** * @param args */public static void main(String[] args)throws IOException {// TODO Auto-generated method stub//test1()//test2();//test3();//test4();test5();}static void test1()throws IOException{FileReader fr = new FileReader("F:\\fd.txt");int c;while((c = fr.read()) != -1){System.out.println((char)c);}fr.close();}static void test2()throws IOException{FileReader fr = new FileReader("F:\\fd.txt");char[] buf = new char[3];int x;while((x = fr.read(buf)) != -1){System.out.println(buf);}fr.close();}static void test3()throws IOException{FileReader fReader = new FileReader("F:\\fd.txt");char[] buf = new char[2];int x;while((x = fReader.read(buf))!= -1){System.out.print(new String(buf, 0, x));}}static void test4()throws IOException{FileWriter fw = new FileWriter("F:\\fd3.txt");FileReader fr = new FileReader("F:\\fd2.txt");int x;while((x = fr.read())!=-1){fw.write(x);}fw.close();fr.close();}static void test5(){FileWriter fw = null;FileReader fr = null;try {fw = new FileWriter("F:\\fd3.txt");fr = new FileReader("F:\\fd2.txt");int len = 0;char[] buf = new char[1024];while((len = fr.read(buf)) != -1){fw.write(buf, 0, len);}} catch (Exception e) {// TODO: handle exceptionSystem.out.println(e.toString());}finally{try {if(fw != null)fw.close();} catch (IOException e2) {// TODO: handle exceptionSystem.out.println(e2.toString());}try {if(fr!=null)fr.close();} catch (IOException e2) {// TODO: handle exceptionSystem.out.println(e2.toString());}}}}

Copy of the continued text read from filewriter filereader



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.