Using IO streams in Java to copy files from a folder

Source: Internet
Author: User

Package copy file;


Import Java.io.File;
Import Java.io.FileFilter;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import Java.io.FilenameFilter;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;




public class CopyFile {
public static void Main (string[] args) {
String Path = "D:/file";
Execute e = new execute ();
E.exe (path);
}
}
Executive Section
Class execute{
public void exe (String s) {
File F = new file (s);
Find a destination file
F.list (New FilenameFilter () {Filter files

Public Boolean Accept (File dir, String name) {
Find the file you are looking for//determine if it is a. txt end file
if (Name.endswith (". txt")) {
Gets the path that will be \ converted to/
String path = Dir.getpath (). replace ("\ \", "/");
SYSTEM.OUT.PRINTLN (path);
Copy section
File F1 = new file (path+ "/" +name);
InputStream is = null;
OutputStream OS = null;
try {
is = new FileInputStream (F1);
OS = new FileOutputStream ("f:/mm/" +name);//Writes the read-in file to this file
byte [] b = new byte[1024];
int len = 0;
while ((Len=is.read (b))!=-1) {
Os.write (b);
}


} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
try {
Close the stream
Is.close (); Os.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
return true;
}
return false;
}
});
Determine if it is a folder and if it is a file enter the file
F.listfiles (New FileFilter () {
Public Boolean accept (File pathname) {
if (Pathname.isdirectory ()) {
EXE (Pathname.getpath (). replace ("\ \", "/"));
return true;
}
return false;
}
});
}
}

Using IO streams in Java to copy files from a 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.