View Javadoc
1   package fr.inrae.agroclim.indicators.util;
2   
3   import java.io.BufferedWriter;
4   import java.io.File;
5   import java.io.FileNotFoundException;
6   import java.io.FileOutputStream;
7   import java.io.OutputStreamWriter;
8   import java.nio.charset.StandardCharsets;
9   import java.nio.file.Path;
10  
11  /**
12   * Shortcut class to write UTF-8 files.
13   *
14   * @author Olivier Maury
15   */
16  public class Utf8BufferedWriter extends BufferedWriter {
17  
18      /**
19       * Constructor.
20       *
21       * @param file file to write
22       * @throws FileNotFoundException if file does not exist.
23       */
24      public Utf8BufferedWriter(final File file) throws FileNotFoundException {
25          super(new OutputStreamWriter(new FileOutputStream(file, true), StandardCharsets.UTF_8));
26      }
27  
28      /**
29       * Constructor.
30       *
31       * @param path path of file to write
32       * @throws FileNotFoundException if file does not exist.
33       */
34      public Utf8BufferedWriter(final Path path) throws FileNotFoundException {
35          super(new OutputStreamWriter(new FileOutputStream(path.toFile(), false), StandardCharsets.UTF_8));
36      }
37  
38      /**
39       * Constructor.
40       *
41       * @param fileName system-dependent name of file to write
42       * @throws FileNotFoundException if file does not exist.
43       */
44      public Utf8BufferedWriter(final String fileName) throws FileNotFoundException {
45          super(new OutputStreamWriter(new FileOutputStream(new File(fileName), true), StandardCharsets.UTF_8));
46      }
47  }