简体   繁体   中英

How to remove ESC characters from string in JAVA

I want to remove ESC character from string in file. I'm reading data from file line by line and i want to remove ESC chars. Here is sample

public static void main(String[] args) {
    String rootFolder = "D:\\sample-files";
    File folder = new File(rootFolder);
    List<File> files = new ArrayList<File>();

    if (folder.exists()) {
        files = (List<File>) FileUtils.listFiles(folder, null, true);
        for (File file : files) {
            Path path = Paths.get(file.getPath());
            try {
                List<String> lines = Files.readAllLines(path);
                for (String line : lines) { 
                    line = line.replaceAll("[\\u0000-\\u001f]", "");
                    line = line.replaceAll("\\r\\n", "\n");
                    line = line.replaceAll("\\r", "\n");
                }

                Path outputPath = Paths.get(file.getParent() + "\\" + file.getName() + "_result.txt");
                Files.write(outputPath, lines);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

在此处输入图片说明

Here is how I've done it:

import java.io.BufferedReader;;
import java.io.FileReader;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {

    /**
     * Use {@link StringBuilder} to convert a `Character` list to {@code String}.
     */
    public static String charactersToString(java.util.List<Character> chars)
    {
        StringBuilder sb = new StringBuilder(chars.size());
        for (Character c : chars) {
            sb.append(c.charValue());
        }
        return sb.toString();
    }

    /**
     * Use {@link Stream} to convert {@code String} to a list of {@code chars}.
     */
    public static java.util.List<Character> stringToCharacters(String text) {
        return text.chars().mapToObj(c -> (char) c).collect(Collectors.toList());
    }

    public static void main(String[] args)
    {
        java.io.File file = new java.io.File("file.txt");
        java.util.List<String> content = new java.util.ArrayList<>();

        try (BufferedReader br = new BufferedReader(new FileReader(file)))
        {
            String line;
            while ((line = br.readLine()) != null)
            {
                /* Convert String to Character array so that
                 * it can be easily processed each char at a time
                 */
                java.util.List<Character> chars = stringToCharacters(line);
                /*
                 * Check if ascii value of char matches ESC
                 */
                chars.removeIf(c -> (int)c == 27);
                /*
                 * Add the processed line to a list that will later
                 * be used to write back to the same file
                 */
                content.add(charactersToString(chars));
            }
            /* 
             * Write all processed lines back to the same file
             */
            java.nio.file.Files.write(file.toPath(), content);
        }
        catch (java.io.IOException e) {
            e.printStackTrace();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM