00
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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;

/******************************************************************************
 * A classe fornece metodos utilitarios para ler ou gravar arquivos.
 * 
 * @since 27 de novembro de 2020 v1.0
 * @version 1.0
 * @author "Pedro Reis"
 *****************************************************************************/
public final class FileTools
{
    /*[01]---------------------------------------------------------------------
    
    -------------------------------------------------------------------------*/
    public static String readTextFile(final File file) throws IOException
    {
        return 
            new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
       
    }//readTextFile()
    
    /*[02]---------------------------------------------------------------------
    
    -------------------------------------------------------------------------*/
    public static String readTextFile(final String filename) throws IOException
    {
        return readTextFile(new File(filename));
    }//readTextFile()
    
    /*[03]---------------------------------------------------------------------
    
    -------------------------------------------------------------------------*/
    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();
  
    }//writeTextFile()
    
    /*[04]---------------------------------------------------------------------
    
    -------------------------------------------------------------------------*/
    public static void writeTextFile(final String filename, final String content)
        throws IOException
    {
        writeTextFile(new File(filename), content);
    }//writeTextFile()
     
    /*[05]---------------------------------------------------------------------
    
    -------------------------------------------------------------------------*/
    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();
        
    }//downloadUrl()
    
}//classe FileTools