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

Posted on

Question :

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:UsersDiego MacarioDocumentsEclipseecred_manatWebContent");
            inputStream = new FileInputStream("");

            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) {


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



Answer :

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.


Leave a Reply

Your email address will not be published. Required fields are marked *