java - 在打开文件之前如何知道编码方案?

标签 java encoding

JAVA, 当我尝试使用 BufferedReader 打开并读取文件时,我收到一条错误消息,指出我使用了错误的编码。因此系统调用了一个异常,我的编码器无法读取文件。 在这种情况下,我如何知道文件使用哪种编码。 当然,如果文件是用“utf-8”编写的,那么就不可能读取“euc-kr”编码的文件。我的问题是我想在打开文件之前获取字符集信息,以便我可以为该文件选择正确的编码方案。有人帮帮我吗?

这是我的代码

package lecture06;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;

public class FindExample01 {

    /**
     * Initialized in : getInput()
     * Used at : findPattern()
     */
    private static String pattern;

    /**
     * Initialized in initApplication
     * @param args
     */
    private static BufferedWriter wBuffer;

    public static void main(String[] args) {
        initApplication();
        Path dir = Paths.get(getInput());
        System.out.println("root = " + dir.toString());
        System.out.println("pattern = " + pattern);
        searchDirectory(dir.toString());
        try {
            wBuffer.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            wBuffer.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static void initApplication()
    {
        try {
            wBuffer = Files.newBufferedWriter(Paths.get("Index.txt"), StandardCharsets.UTF_8);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static String getInput()
    {
        Scanner sc = new Scanner(System.in);
        String dir = null;
        for(;;)
        {
            System.out.println("Root Directory: ");
            dir = sc.next();
            if (Files.exists(Paths.get(dir), LinkOption.NOFOLLOW_LINKS)) break;
        }
        for(;;)
        {
            System.out.println("Find what ?");
            pattern = sc.next();
            if (pattern.length() > 2) 
            {
                sc.close();
                return dir;
            }
        }
    }

    private static void searchDirectory(String root)
    {
        File fiRoot = new File(root);
        File[] files = fiRoot.listFiles();
        for (File file : files)
        {
            if (file.isDirectory()) searchDirectory(file.getAbsolutePath());
            else findPattern(file.toPath());
        }
    }

    private static void findPattern(Path path)
    {
        try {
            BufferedReader rBuffer = Files.newBufferedReader(path, StandardCharsets.UTF_8 );
            int count = 1;
            String line;
            while ((line = rBuffer.readLine()) != null)
            {
                int idx;
                while ((idx = line.indexOf(pattern)) != -1)
                    writeIndex(path.toString(), count, idx);
                count++;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static void writeIndex(String path, int count, int idx)
    {
        try {
            wBuffer.write(path + " : " + count + " : " + idx + " : " + pattern);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            wBuffer.newLine();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

最佳答案

尝试juniversalchardet ,它是一个编码检测器库。它有一个可以检测到的流行编码的列表。为此,您不需要读取整个文件,只需读取第一个字节

    byte[] buf = new byte[4096];
    UniversalDetector detector = new UniversalDetector(null);
    int nread;
    while ((nread = fileInputStream.read(buf)) > 0 && !detector.isDone()) {
        detector.handleData(buf, 0, nread);
    }
    detector.dataEnd();
    String encoding = detector.getDetectedCharset();

关于java - 在打开文件之前如何知道编码方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52095591/

相关文章:

c# - Azure WebJobs 上的编码问题

java - 您将如何在 java 中扫描文件并计算该文件的字母数字字符和元音的数量?

java - RESTEasy/Jackson 未将分层 POJO 正确序列化为 JSON

java - 线程运行无法正常工作

java - 为什么 byteArray 的长度是 22 而不是 20?

ruby - 类别特殊字符的 Jekyll 编码名称

java - 如何让 java getRuntime().exec() 运行带参数的命令行程序?

java - 为特定条件维护 hashCode 契约(Contract),equals() 取决于两个整数

c++ - 从 MySQL 中检索 xml

java - 再次出现Tomcat编码问题