Learning Java in the middle of the beginning is mainly through the API to learn, do not rush slowly.
Sub-demand, we need to copy files from one file to another folder, which we can use a lot of methods, but in Java I used to be the IO (nonsense), the main contact Javaio in a variety of ways to see what the relationship between them. Take a look at the following Java packages to use:
First we will consider how to read the file into the program, in Java IO (io is a Java software package) all the implementation classes inherit from the writer and reader two classes (base class), in this case to describe, First we read the file into the program read this action using the object of the FileInputStream class to read:
We see that the construction method in FileInputStream needs to pass a file object, so let's look at how the file object indicates a path to the files:
Yes, in the file's construction, the path and filename of the files can be found in the file needs to be developed, here, if there is no such file under the folder, the program will appear the system cannot find the specified file exception. After the file has been found, it can be read into the file, which is the read data or the byte stream, using InputStreamReader to convert the stream of bytes into character streams.
Public StaticArraylist<string> Read ()throwsioexception{String Path= "D:\\sendtest"; String filename= "Requst.properties"; FileInputStream FIS=NULL; InputStreamReader in=NULL ; BufferedReader buf=NULL; String Data=NULL; ArrayList<String> arr=NewArraylist<string>(); Try{FIS=NewFileInputStream (NewFile (path,filename)); Inch=NewInputStreamReader (FIS); BUF=NewBufferedReader (in); Try { while(Data =buf.readline ())! =NULL) {System.out.println ("Data+" +data); Arr.add (data); } } Catch(IOException e) {e.printstacktrace (); } } Catch(FileNotFoundException e) {e.printstacktrace (); } if(fis!=NULL) {fis.close (); } if(in!=NULL) {in.close (); } if(buf!=NULL) {buf.close (); } returnarr; }
The purpose of converting bytes into a character stream is that when we read a file, the program converts it directly to characters and then outputs it. Also for the sake of efficiency, we will be in the packaging layer, from the code to see, outside the inputstreamreader we wrap the BufferedReader this class used after we read the data will be a line of reading that is Buf.readline ().
After reading the data I use a string array to save him, and then we use the writer class to write the data to the file, directly on the code :
Public Static voidWrite (arraylist<string> arr)throwsioexception{FileOutputStream Fos=NULL; OutputStreamWriter OSW=NULL; BufferedWriter BR=NULL; Try{fos=NewFileOutputStream (NewFile ("D:\\sendtest", "Respone.properties")); } Catch(FileNotFoundException e) {e.printstacktrace (); } OSW=Newoutputstreamwriter (FOS); BR=NewBufferedWriter (OSW); for(String str:arr) {Try{br.write (str); } Catch(IOException e) {e.printstacktrace (); } } Try{Br.flush (); } Catch(IOException e) {e.printstacktrace (); } System.out.println (Success "); if(fos!=NULL) {fos.close (); } if(osw!=NULL) {osw.close (); } if(br!=NULL) {br.close (); } }
You can see that this part of the code also encapsulates the OutputStreamWriter byte character conversion bridge, BufferedWriter and high-efficiency read stream on the basis of the word stream. It is important to note that the meaning of Br.flush () reads the data into the buffer in the required method of writing to the file.
Here is the code for the entire class I wrote for reference only:
Packagecom.qunchen.www;ImportJava.io.BufferedInputStream;ImportJava.io.BufferedOutputStream;ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.io.OutputStreamWriter;Importjava.util.ArrayList; Public classTransferofdata {/*** Read Data * and write to another file *@throwsIOException*/ Public StaticArraylist<string> Read ()throwsioexception{String Path= "D:\\sendtest"; String filename= "Requst.properties"; FileInputStream FIS=NULL; InputStreamReader in=NULL ; BufferedReader buf=NULL; String Data=NULL; ArrayList<String> arr=NewArraylist<string>(); Try{FIS=NewFileInputStream (NewFile (path,filename)); Inch=NewInputStreamReader (FIS); BUF=NewBufferedReader (in); Try { while(Data =buf.readline ())! =NULL) {System.out.println ("Data+" +data); Arr.add (data); } } Catch(IOException e) {e.printstacktrace (); } } Catch(FileNotFoundException e) {e.printstacktrace (); } if(fis!=NULL) {fis.close (); } if(in!=NULL) {in.close (); } if(buf!=NULL) {buf.close (); } returnarr; } Public Static voidWrite (arraylist<string> arr)throwsioexception{FileOutputStream Fos=NULL; OutputStreamWriter OSW=NULL; BufferedWriter BR=NULL; Try{fos=NewFileOutputStream (NewFile ("D:\\sendtest", "Respone.properties")); } Catch(FileNotFoundException e) {e.printstacktrace (); } OSW=Newoutputstreamwriter (FOS); BR=NewBufferedWriter (OSW); for(String str:arr) {Try{br.write (str); } Catch(IOException e) {e.printstacktrace (); } } Try{Br.flush (); } Catch(IOException e) {e.printstacktrace (); } System.out.println (Success "); if(fos!=NULL) {fos.close (); } if(osw!=NULL) {osw.close (); } if(br!=NULL) {br.close (); } } Public Static voidMain (string[] args)throwsIOException {ArrayList<String> arr =read (); Write (arr); } }
This is my first blog, to tell the truth to learn a summary of the harvest, I hope you and I can achieve their ideals.
Java File Replicator (your own name, used primarily for data relocation, file copying, etc.)