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