April 1, 2016 afternoon, "Java Primer 123" opened the first page, from the road to never return. Newcomers to the new gift

Source: Internet
Author: User
Tags file copy getmessage

Package copyfile;

Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;

public class CopyFile {
/**
* If you manipulate strings, you can also implement a Mac naming method
* This is a file copy under the same folder
* @param srcfilepath Source file path
* @param destfilepath destination file path
* @return Copy succeeded returns True
*/
public static Boolean Copyfilesamepath (String srcfilepath,string destfilepath) {
File Srcfile=new file (Srcfilepath);
File Destfile=new file (Destfilepath);
if (! ( Srcfile.exists ())) {
System.out.println ("Source file does not exist, cannot be copied");
return false;
}
if (! ( Srcfile.getpath (). Equals (Destfile.getpath ()))) {
System.out.println ("This method can only replicate files in the same directory");
return false;
}
if (! ( Srcfile.isfile ())) {
System.out.println ("This method only copies files");
return false;
}
File Newfile=null;
int increasing=2;
String folder=destfile.getparent ();
String filename= "Copy" +destfile.getname ();
String Newpath=folder+file.separator+filename;

Newfile=new File (NewPath);

while (Newfile.exists ()) {
Filename= "Copy" +increasing++ + "" +destfile.getname ();
Newpath=folder+file.separator+filename;

Newfile=new File (NewPath);

}
Byte[] Databytes=null;
try{
FileInputStream reader=new FileInputStream (srcfile);
Databytes=new byte[reader.available ()];
Reader.read (databytes);
Reader.close ();
}catch (FileNotFoundException e) {
System.out.println (E.getmessage ());
}catch (IOException e) {
System.out.println (E.getmessage ());
}
try{
Newfile.createnewfile ();
FileOutputStream writer=new FileOutputStream (newFile);
Writer.write (databytes);
Writer.close ();
}catch (FileNotFoundException e) {
System.out.println (E.getmessage ());
}catch (IOException e) {
System.out.println (E.getmessage ());
}
if (newfile.exists ()) {
return true;
}else{
return false;
}
}
/**
* This is a file copy under a different folder
* @param srcfilepath Source file path
* @param destfilepath Target path
* @param overlay is covered
* @return Successful return True
*/
public static Boolean Copyfiledifferentpath (String srcfilepath,string Destfilepath,boolean overlay) {
File Srcfile=new file (Srcfilepath);
File Destfile=new file (Destfilepath);
if (! ( Srcfile.exists ())) {
System.out.println ("Source file does not exist, cannot be copied");
return false;
}
if (Srcfile.getpath (). Equals (Destfile.getpath ())) {
System.out.println ("Directory of files must be different");
return false;
}
if (! ( Srcfile.isfile ())) {
System.out.println ("This method only copies files");
return false;
}
if (! ( Srcfile.getname (). Equals (Destfile.getname ()))) {
System.out.println ("File name must be the same");
return false;
}
if (destfile.exists ()) {
if (overlay) {
Destfile.delete ();
}else{
System.out.println ("Duplicate file name");
return false;
}
}
try{
Destfile.createnewfile ();
}catch (IOException e) {
E.printstacktrace ();
return false;
}
Byte[] Databytes=null;
try{
FileInputStream reader=new FileInputStream (srcfile);
Databytes=new byte[reader.available ()];
Reader.read (databytes);
Reader.close ();
}catch (FileNotFoundException e) {
E.printstacktrace ();
}catch (IOException e) {
E.printstacktrace ();
}
try{
FileOutputStream writer=new FileOutputStream (destfile);
Writer.write (databytes);
Writer.close ();
}catch (FileNotFoundException e) {
E.printstacktrace ();
}catch (IOException e) {
E.printstacktrace ();
}
if (destfile.exists ()) {
return true;
}else{
return false;
}
}
/**
* This is a folder copy method under different folders
* @param srcfolderpath source folder address
* @param destfolderpath destination file address
* @param overlay is overwritten, if overwrite all files and folders with the same name in the target folder will be replaced, the different names are not affected
* @return Successful return True
*/
public static Boolean Copyfolderdifferentpath (String srcfolderpath,string Destfolderpath,boolean overlay) {
File Srcfile=new file (Srcfolderpath);
File Destfile=new file (Destfolderpath);
if (! ( Srcfile.exists ())) {
System.out.println ("Source file does not exist, cannot be copied");
return false;
}
if (Srcfile.getpath (). Equals (Destfile.getpath ())) {
System.out.println ("Directory of files must be different");
return false;
}
if (! ( Srcfile.getname (). Equals (Destfile.getname ()))) {
System.out.println ("folder name must be the same");
return false;
}
if (!srcfile.isdirectory ()) {
System.out.println ("This method only copies folders");
return false;
}
if (destfile.exists ()) {
if (overlay) {
File[] Files=srcfile.listfiles ();
int size=files.length;
for (int i=0;i<size;i++) {
if (Files[i].isfile ()) {
Copyfile.copyfiledifferentpath (Files[i].getpath (), Destfile.getpath () +file.separator+files[i].getname (), true);
}else{
Copyfile.copyfolderdifferentpath (Files[i].getpath (), Destfile.getpath () +file.separator+files[i].getname (), True) ;
}
}
}else{
System.out.println ("Duplicate folder name");
return false;
}
}else{
Destfile.mkdirs ();
File[] Files=srcfile.listfiles ();
int size=files.length;
for (int i=0;i<size;i++) {
if (Files[i].isfile ()) {
Copyfile.copyfiledifferentpath (Files[i].getpath (), Destfile.getpath () +file.separator+files[i].getname (), true);
}else{
Copyfile.copyfolderdifferentpath (Files[i].getpath (), Destfile.getpath () +file.separator+files[i].getname (), True) ;
}
}
}
return true;
}
/**
* This is the same directory as the folder copy method, in order to create copies of the way
* @param srcfolderpath source folder
* @param destfolderpath target folder
* @return Successful return True
*/
public static Boolean Copyfoldersamepath (String srcfolderpath,string destfolderpath) {
File Srcfile=new file (Srcfolderpath);
File Destfile=new file (Destfolderpath);
if (! ( Srcfile.exists ())) {
System.out.println ("Source file does not exist, cannot be copied");
return false;
}
if (! ( Srcfile.isdirectory ())) {
System.out.println ("This method only copies folders");
return false;
}
File Newfile=null;
int increasing=2;
String folder=destfile.getparent ();
String filename= "Copy" +destfile.getname ();
String Newpath=folder+file.separator+filename;

Newfile=new File (NewPath);

while (Newfile.exists ()) {
Filename= "Copy" +increasing++ + "" +destfile.getname ();
Newpath=folder+file.separator+filename;

Newfile=new File (NewPath);
}
Newfile.mkdirs ();
File[] Files=srcfile.listfiles ();
int size=files.length;
for (int i=0;i<size;i++) {
if (Files[i].isfile ()) {
Copyfile.copyfiledifferentpath (Files[i].getpath (), Newfile.getpath () +file.separator+files[i].getname (), false);
}else{
Copyfile.copyfolderdifferentpath (Files[i].getpath (), Newfile.getpath () +file.separator+files[i].getname (), false) ;
}
}
return true;
}
public static void Main (string[] args) {
Copyfoldersamepath ("/users/dawn/documents/javapractice/datafile", "/users/dawn/documents/javapractice/datafile" );
}
}

This is a copy of the file class, there are 4 methods, the integration of all copies of the file, including copying folders and files in the same directory, copying files or folders to different directories, because the direct use of the output stream and input stream operations, so for all file types are supported.

Novice just learn Java, shortcomings also please correct me

April 1, 2016 afternoon, "Java Primer 123" opened the first page, from the road to never return. Newcomers to the new gift

Related Article

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.