Home > Back-end >  About IO flow for example, copy the code and repeated copying, met a difficult to solve the problem,
About IO flow for example, copy the code and repeated copying, met a difficult to solve the problem,

Time:09-26

Are all about copying files, one is the characters of the flow, is a byte stream
Characters flow  results and then copy the file after the code
Byte stream is an instance of the
Public static void copyDirectory () throws IOException {
Scanner sc=new Scanner(System.in);
System. The out. Println (" please enter the source directory location: ");
String srcPath=sc. NextLine ();//from the console access path
The File srcFile=new File (srcPath);
System. The out. Println (" please enter the target location: ");
String destPath=sc. NextLine ();//from the console access path
The File destFile=new File (destPath);
//copies the contents of the source path to the target directory
FileUtils. CopySrcPathTODestPath (srcFile destFile);
}
Public static void copySrcPathTODestPath (File srcFile, File destFile) throws IOException {
//create the destination folder
DestFile. The file.mkdirs ();/* create this abstract pathname specified directory, including all necessary but there is no parent directory, */
//get all the File object in the source path
The File files []=srcFile. ListFiles ();
For (the File File: files
) {
File dest=new File (File destFile, getName ());
//determine whether the current file object for the directory
If (the file. The isDirectory ()) {
//by recursion, complete copy directory
CopySrcPathTODestPath (file, dest);
} else {
//copy the file
FileInputStream in=new FileInputStream (file);
FileOutputStream out=new FileOutputStream (dest);
//read
Byte [] bys=new byte [1024].
Int len=0;
While ((len=in. Read (bys))!=1) {
//write
Out. Write (bys, 0, len);
}
//off flow
in.close();
out.close();

}

}
}

When I set up the source directory and the implementation of the target directory
Target directory if a source directory as part of the folder will be unlimited replication, the final blow up, I guess may be recursive problem, but I don't know how to solve his,
Normal: the source directory: F: \ API target directory: D: \ \ Program Files
Unlimited replication: source directory: D: \ \ Program Files \ Java directory: D: \ \ Program Files \ Java \ 123
Ask god help me, thank you very much

CodePudding user response:

Unlimited replication: source directory: D: \ \ Program Files \ Java directory: D: \ \ Program Files \ Java \ 123
Should be finished your source directory replication of the original but you have a directory below 123 to copy the directory and then copy it again some things not to
Can determine your copySrcPathTODestPath (File srcFile, File destFile) in the srcFile whether you create a new directory is a return to avoid useless copy directly

CodePudding user response:

Characters of the flow is coding, what is your default encoding system?
You can use the System. The out. Println (System. GetProperty (" file. The encoding ")); Encoding that is used by the generated file will default to use save as way to check your SRC. TXT coding, see whether consistent?
If the code is not consistent, you can use code flow characters, i.e., (assuming the coding for GBK)
BufferedReader reader=new BufferedReader (new InputStreamReader (new FileInputStream (" SRC. TXT "), "GBK"));
BufferedWriter writer=new BufferedWriter (new OutputStreamWriter (new FileOutputStream (" copy. TXT "), "GBK"));

As for byte stream example, if the destination folder is the source folder folder, you have to put it out of the object of the source file, or is equivalent to replicate themselves (and their because of coming from the source file copy content constantly refresh) again, this copy themselves each end, will eventually overflow,
Instead of

If (file. IsDirectory () & amp; & ! File. GetAbsolutePath () equals (dest. GetAbsolutePath ()) ) {//if not recursive subfolders yourself
//by recursion, complete copy directory
CopySrcPathTODestPath (file, dest);







  • Related