How to record a file in ISO-8859-1 without losing accent

3

I would like to know how to write a file in ISO-8859-1 and not lose the graphical accent if possible, my doubts come from the following. When defining in Eclipse that the file encoding follows this charset I quoted above, it correctly saves the files using accent but if I were using Java code try to perform a file edit and save in this charset, I will lose the accents. What to do?

public class Diretorio {

    public static void main(String[] args) {

        Properties properties = new Properties();
        InputStream inputStream;

        try {

            List<File> listFile = getFileList("C:\Users\Diego Macario\Documents\Eclipse\ecred_manat\WebContent");
            inputStream = new FileInputStream("c:\teste.properties");
            properties.load(inputStream);


            for (File file : listFile) {

                List<String> lines = Files.readAllLines(file.toPath(), Charset.forName("ISO-8859-1"));

                for (Entry<Object, Object> label : properties.entrySet()) {

                    String key = "#{lbl['" + (String) label.getKey() + "']}";

                    for (String string : lines) {

                        if (string.contains(key)) {

                            int index = lines.indexOf(string);

                            String value = (String) label.getValue();
                            value = new String(value.getBytes("ISO-8859-1"), "UTF-8");
                            string = string.replace(key, value);
                            lines.set(index, string);

                        }
                    }

                }

                Files.write(file.toPath(), lines, Charset.forName("ISO-8859-1"), StandardOpenOption.TRUNCATE_EXISTING);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static List<File> getFileList(String path) {
        String[] extension = { "xhtml" };
        List<File> listFile = new ArrayList<>(FileUtils.listFiles(
                new File(path), extension, true));
        return listFile;
    }

}
    
asked by anonymous 14.03.2014 / 01:32

1 answer

2

You should use the java.nio.charset.Charset class when constructing an object String. This ensures that you are building an object in the same encoding that you specified in Eclipse. In your case, use the name "ISO-8859-1" and the static method forName that gives you a Charset object to use in the String.

    
14.03.2014 / 03:54