Java learning [file IO], java learning io

Source: Internet
Author: User

Java learning [file IO], java learning io

Blog: http://blog.csdn.net/muyang_ren

1. Read a text file quickly and output the file content to a new file;

package lhy.java_day3.oop;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class Test_BuffeRead {    private static BufferedReader bfReader;    private static BufferedWriter bfWriter;    private static String str;    /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub        try {            bfReader = new BufferedReader(new FileReader("f:/java_day3.txt"));            bfWriter = new BufferedWriter(new FileWriter("f:/write.txt"));            while ((str=bfReader.readLine() )!= null) {                System.out.println(str);                bfWriter.write(str+"\r\n");                bfWriter.flush();            }        } catch (FileNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}

2. List the files suffixed with ". java" in a directory.

Package lhy. java_day3.oop; import java. io. file; public class Test_File {private static File fs;/*** @ param args */public static void main (String [] args) {String pathname = "e: \ test "; try {fs = new File (pathname); String [] str = fs. list (); for (String string: str) {if (string. endsWith (". java ") System. out. println (string) ;}} catch (NullPointerException e) {// TODO: handle exception System. out. Println ("failed to open the directory! ");}}}

3. Copy image files

package lhy.java_day3.oop;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class Test_picture {    private static FileInputStream fis;    private static FileOutputStream fos;    private static int ret;    private static byte[] b = new byte[2048];     /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub              try {            fis = new FileInputStream("f:\\tu1.jpg");            fos = new FileOutputStream("f:\\tu2.jpg");            while (true) {                ret = fis.read(b, 0, b.length);                if(ret!=-1){                    fos.write(b, 0, ret);                }else {                    break;                }                       }        } catch (FileNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}

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.