Last active
October 25, 2018 15:30
-
-
Save HugoSilvaF/4dc8e70d6013d1b4ce25 to your computer and use it in GitHub Desktop.
Adicionado licença
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2018 Hugo Silva <[email protected]> | |
* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
/** | |
* Créditos github/CrazyDev. | |
* Créditos github/JonathanxD. | |
*/ | |
public class FileCopy { | |
private final File toDir; | |
private final String localFile; | |
private final String fileName; | |
private final Boolean[] logs; | |
/** | |
* | |
* @param localFile Local do arquivo a ser copiado. | |
* @param toDir Local para onde o arquivo vai ser copiado. | |
* @param fileName Nome do arquivo que será copiado. | |
* @return FileCopy | |
*/ | |
public FileCopy(String localFile, String toDir, String fileName) { | |
this.localFile = localFile; | |
this.fileName = fileName; | |
this.toDir = new File(toDir); | |
this.logs = new Boolean[]{true, false, true, true, true, true, true, true, true, true, true, true}; | |
} | |
public void copy() throws IOException { | |
File file; | |
InputStream inputStream; | |
FileOutputStream fileOutputStream; | |
try { | |
inputStream = getClass().getResourceAsStream(localFile); | |
if(inputStream == null) { | |
if(logs[0]) | |
System.out.println("Failed to read the file " + localFile); | |
return; | |
} | |
if(!toDir.exists()) { | |
try { | |
if(toDir.mkdirs()) { | |
if(logs[1]) | |
System.out.println("Success to create the directory " + toDir.toURI()); | |
} else { | |
if(logs[2]) | |
System.out.println("Failed to create the directory " + toDir.toURI()); | |
} | |
} catch (NullPointerException ex) { | |
if(logs[3]) | |
System.out.println("The destination is null " + toDir.toURI()); | |
ex.printStackTrace(); | |
} catch (SecurityException ex) { | |
if(logs[4]) | |
System.out.println("This directory is protected " + toDir.toURI()); | |
ex.printStackTrace(); | |
} | |
} else { | |
if(logs[5]) | |
System.out.println("The directory defined , is not directory. Or it has already been created " + toDir.toURI()); | |
} | |
file = new File(toDir, fileName); | |
if(!file.exists()) { | |
try { | |
file.createNewFile(); | |
if(logs[6]) | |
System.out.println("Successfully created file " + toDir.toURI()); | |
} catch (IOException ex) { | |
if(logs[7]) | |
System.out.println("An error occurred while creating the file" + toDir.toURI()); | |
ex.printStackTrace(); | |
} catch (SecurityException ex) { | |
if(logs[8]) | |
System.out.println("This file is protected " + toDir.toURI()); | |
ex.printStackTrace(); | |
} | |
} | |
fileOutputStream = new FileOutputStream(file); | |
int byteNumber; | |
while((byteNumber = inputStream.read()) != -1) { | |
try { | |
fileOutputStream.write(byteNumber); | |
} catch (IOException ex) { | |
if(logs[9]) | |
System.out.println("Error writing file " + toDir.toURI()); | |
ex.printStackTrace(); | |
} | |
} | |
fileOutputStream.close(); | |
if(logs[10]) | |
System.out.println("Successfully copied file " + toDir.toURI()); | |
} catch (NullPointerException ex) { | |
if(logs[11]) | |
ex.printStackTrace(); | |
} | |
} | |
public FileCopy setLogFailedToReadFile(Boolean bool) { | |
this.logs[0] = bool; | |
return this; | |
} | |
public FileCopy setLogSucessToCreateDirectory(Boolean bool) { | |
this.logs[1] = bool; | |
return this; | |
} | |
public FileCopy setLogFailedToCreateDirectory(Boolean bool) { | |
this.logs[2] = bool; | |
return this; | |
} | |
public FileCopy setLogTheDestinationIsNull(Boolean bool) { | |
this.logs[3] = bool; | |
return this; | |
} | |
public FileCopy setLogThisDirectoryIsProtected(Boolean bool) { | |
this.logs[4] = bool; | |
return this; | |
} | |
public FileCopy setLogThisDirectoryHasAlreadyCreated(Boolean bool) { | |
this.logs[5] = bool; | |
return this; | |
} | |
public FileCopy setLogSucessfullyCreatedFile(Boolean bool) { | |
this.logs[6] = bool; | |
return this; | |
} | |
public FileCopy setLogAnErrorOcurredWhile(Boolean bool) { | |
this.logs[7] = bool; | |
return this; | |
} | |
public FileCopy setLogThisFileIsProtected(Boolean bool) { | |
this.logs[8] = bool; | |
return this; | |
} | |
public FileCopy setLogErrorWritingFile(Boolean bool) { | |
this.logs[9] = bool; | |
return this; | |
} | |
public FileCopy setLogSucessfullyCopiedFile(Boolean bool) { | |
this.logs[10] = bool; | |
return this; | |
} | |
public FileCopy setLogNullPointerEx(Boolean bool) { | |
this.logs[11] = bool; | |
return this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Como usar: