Last active
July 27, 2019 12:52
-
-
Save gbzarelli/163defc6c6796b114e6817d2bdbdabf6 to your computer and use it in GitHub Desktop.
Clase utils de arquivos. Carrega e grava arquivos. Também converte arquivos em Base64
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
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.Base64; | |
/** | |
* | |
* @author Guilherme Biff Zarelli (helpdev.com.br) | |
*/ | |
public class FileUtils { | |
private static final Base64.Encoder ENCODER = Base64.getEncoder(); | |
public static String encodeToString(byte[] bytes) { | |
return ENCODER.encodeToString(bytes); | |
} | |
public static String encodeToString(File file) | |
throws IOException { | |
byte[] bytes = readAllBytes(file); | |
return encodeToString(bytes); | |
} | |
public static void writeFile(byte[] data, File pathFile, String nameFile) throws IOException { | |
File fileWrite = new File(pathFile, nameFile); | |
if (!pathFile.exists() && !pathFile.mkdirs()) { | |
throw new IOException("Failed create directory"); | |
} | |
if (!fileWrite.exists() && !fileWrite.createNewFile()) { | |
throw new IOException("Failed to create file"); | |
} | |
Files.write(fileWrite.toPath(), data); | |
} | |
public static byte[] readAllBytes(File file) throws IOException { | |
return Files.readAllBytes(file.toPath()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment