How to write a file to ISO-8859-1 without losing accentuation

Asked

Viewed 1,549 times

3

I would like to know please how to save a file to ISO-8859-1 and not to miss the graphic accent if possible, my doubt comes from the following. When defining in Eclipse that the file encoding follows this charset I mentioned above, it correctly saves the files using accent but if I saw 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;
    }

}
  • To the read the file via Java code, are you using the correct charset? If you save in Eclipse (or any other way) a file in ISO-8859-1, then this file can be read perfectly by a Java program (which, if done correctly, will store its contents in a String UTF-16 lossless), manipulated, and saved again in the same encoding. If you are having trouble doing so, please post the code used (only the relevant section).

  • @mgibsonbr put the code.

1 answer

2


You should use the java.nio.charset.Charset class when building an object String. With this you are ensuring that you are building an object in the same encoding you specified in Eclipse. In your case use the name "ISO-8859-1" and the static method forName which provides you with a Charset object to use in the String.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.