Java file IO Exercise title: Copy the. java file under one folder to a. txt file under another folder

Source: Internet
Author: User

 PackageCom.swift;ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.OutputStreamWriter;Importjava.io.UnsupportedEncodingException; Public classCopy_java_to_txt { Public Static voidMain (string[] args) {/** Copy the. java file under one folder to a. txt file under another folder*/        Try{stringbuffer SB=NewStringBuffer (); BufferedReader BR=NewBufferedReader (NewInputStreamReader (NewFileInputStream ("E:\\neck\\test.java"), "Utf-8"));            String str;  while(str = br.readline ())! =NULL) {System.out.println ("Read a line from the file ...");                Sb.append (str); Sb.append ("\ r \ n"); } System.out.println ("File read to container succeeded" + "\ r \ n" +sb.tostring ()); String dir= "E:\\apple"; String FileName= "Test.txt"; File File=NewFile (dir,filename); if(!File.getparentfile (). exists ()) {System.out.println (File.getparentfile ()+ "Directory does not exist, will be created ...");            File.getparentfile (). Mkdirs (); }Else{System.out.println ("Directory exists without creating ..."); } bufferedwriter BW=NewBufferedWriter (NewOutputStreamWriter (NewFileOutputStream (file), "Utf-8"));            Bw.write (Sb.tostring ());        Bw.flush (); } Catch(unsupportedencodingexception e) {e.printstacktrace (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }    }}

Above is specifying which file to copy

Below is the search for all files ending in. Java copied to another folder and renamed

You need to use the two types of lists below

The list () method returns the file names of all files and directories under a directory, and returns a string array

The Listfiles () method returns the absolute path to all files and directories in a directory, and returns a file array

Java file IO exercise: Copy the. java file under one folder to a. txt file under another folder

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.