代码之家  ›  专栏  ›  技术社区  ›  plaidshirt

无法在java中设置字符编码。util。扫描仪

  •  4
  • plaidshirt  · 技术社区  · 7 年前

    我用 Apache Tika 获取文件的编码。

                FileInputStream fis = new FileInputStream(my_file);
                final AutoDetectReader detector = new AutoDetectReader(fis);
                fis.close();
                System.out.println("Encoding:" + detector.getCharset().toString());
    

    我用 Scanner 从文件中读取值。

                    Scanner scanner = new Scanner(my_file, detector.getCharset().toString());
                    Map<String, String> values = new HashMap<>();
                    String line, key = null, value = null;
                    while (scanner.hasNextLine()) {
                        line = scanner.nextLine();
                        if (line.contains(":")) {
                            if (key != null) {
                                values.put(key, value.trim());
                                key = null;
                                value = null;
                            }
                            int indexOfColon = line.indexOf(":");
                            key = line.substring(0, indexOfColon);
                            value = line.substring(indexOfColon + 1);
                        } else {
                            value += " " + line;
                        }
                    }
    

    扫描仪 无法从带有编码的文件中读取文本 windows-1252 ,我得到空字符串。

    更新2018.11.07。 对于BufferedReader,我也有同样的问题。

                        Map<String, String> values = new HashMap<>();
                        String line, key = null, value = null;
                        FileInputStream is = new FileInputStream(my_file);
                        InputStreamReader isr = new InputStreamReader(is, getEncoding(my_file));
                        BufferedReader buffReader = new BufferedReader(isr);
    
                        while (buffReader.readLine() != null) {
                            line = buffReader.readLine();
                            if (line.contains(":")) {
                                if (key != null) {
                                    values.put(key, value.trim());
                                    key = null;
                                    value = null;
                                }
                                int indexOfColon = line.indexOf(":");
                                key = line.substring(0, indexOfColon);
                                value = line.substring(indexOfColon + 1);
                            } else {
                                value += " " + line;
                            }
                        }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   gi097 Sildoreth    7 年前

    我会尝试使用以下方法来阅读字符,而不是阅读线条:

    ByteArrayOutputStream line = new ByteArrayOutputStream();
    Scanner scanner = new Scanner(my_file);
    
    while (scanner.hasNextInt()) {
        int c = 0;
        // read every line
        while (c != newline) { // TODO: Check for a newline char
            c = scanner.nextInt();
            line.write((byte) c);
        }
        byte[] array = line.toByteArray();
        String output = new String(array, "Windows-1252"); // This should do the trick
    
        // We have a string here, do your logic
    
        line.reset();
    }
    

    这种方法很难看,但使用 new String 它能够指定特定的编码。我根本没有测试或运行这段代码,但至少它会显示您是否正确阅读了任何内容。