000102030405060708091011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
package br.com.hkp.whatsappwebfix.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
public final class FileTools
{
public static String readTextFile(final File file) throws IOException
{
return
new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
}
public static String readTextFile(final String filename) throws IOException
{
return readTextFile(new File(filename));
}
public static void writeTextFile(final File file, final String content)
throws IOException
{
FileWriter fw = new FileWriter(file, StandardCharsets.UTF_8);
fw.write(content);
fw.close();
}
public static void writeTextFile(final String filename, final String content)
throws IOException
{
writeTextFile(new File(filename), content);
}
public static void downloadUrl(final String url, final String path)
throws IOException
{
String filename = url.substring(url.lastIndexOf('/'), url.length());
URL download = new URL(url);
ReadableByteChannel rbc = Channels.newChannel(download.openStream());
FileOutputStream fos = new FileOutputStream(path + filename);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
}
}