繁体   English   中英

使用Java检测编码

[英]Detect Encoding with Java

我有一个正在工作的例子。 通过这个例子(下面提供),我可以使用mozilla的universaldetector框架检测文件的编码。

但我希望这个例子使用类Scanner检测输入的编码而不是Example的文件的编码? 如何修改下面的代码来检测输入而不是文件的编码?

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.mozilla.universalchardet.UniversalDetector;

public class TestDetector {
  public static void main(String[] args) throws java.io.IOException {
    byte[] buf = new byte[4096];


    java.io.FileInputStream fis = new java.io.FileInputStream("C:\\Users\\khalat\\Desktop\\Java\\toti.txt");


    // (1)
    UniversalDetector detector = new UniversalDetector(null);

    // (2)
    int nread;
    while ((nread = fis.read(buf)) > 0 && !detector.isDone()) {
      detector.handleData(buf, 0, nread);
    }

    // (3)
    detector.dataEnd();

    // (4)
    String encoding = detector.getDetectedCharset();
    if (encoding != null) {
      System.out.println("Detected encoding = " + encoding);
    } else {
      System.out.println("No encoding detected.");
    }

    // (5)
    detector.reset();
  }
}

我发现了一个至少可以测试的优雅例子,而charatcht是ISO-8859-1,见下面的代码。

public class TestIso88591 {
    public static void main(String[] args){
        if(TestIso88591.testISO("ü")){
            System.out.println("True");
        }
        else{
            System.out.println("False");
        }

    }
    public static boolean testISO(String text){
        return  Charset.forName(CharEncoding.ISO_8859_1).newEncoder().canEncode(text);
    }
}

现在我对专家Java提出质疑。有一个测试charachter的可能性是ISO-8859-5还是ISO-8859-7? 是的我知道有utf-8但是我的确切问题是我如何测试iso-8859-5 charachter。 因为输入数据应存储在SAP中,而SAP只能存储在ISO-8859-1 CHarachter中。 我需要尽快。

好的,我研究了一下。 结果是。 从stdin读取字节来猜测编码是没用的,因为java API允许你直接读取输入为已经编码的字符串;)这个dector的唯一用例是从文件中获取未知字节流或插座等猜测如何在java字符串中解码它。

下一个伪代码,它只是理论上的方法。 但是,正如我们发现它没有任何意义;)

它非常简单。

byte[] buf = new byte[4096];
java.io.FileInputStream fis = new java.io.FileInputStream("C:\\Users\\khalat\\Desktop\\Java\\toti.txt");

UniversalDetector detector = new UniversalDetector(null);

int nread;
while ((nread = fis.read(buf)) > 0 && !detector.isDone()) {
    detector.handleData(buf, 0, nread);
}

你在这里做的是从文件读取一个字节数组,然后传递给检测器。

将FileInputStream替换为其他阅读器。

例如,从Standard In读取所有内容:

byte[] buf = new byte[4096];
InputStreamReader isr = new InputStreamReader(System.in);

UniversalDetector detector = new UniversalDetector(null);

int nread = 0;
while ((nread = isr.read(buf, nread, buf.length)) > 0 && !detector.isDone()) {
    detector.handleData(buf, 0, nread);
}

注意!! 此代码未经我测试。 它仅基于Java API Docs。 我还会在输入流和读取之间放置一个BufferedReader来缓冲。 由于4096字节的缓冲区大小,它也无法工作。 当我看到我的示例时,它会工作,当你在一个块中直接输入Stdandard IN中的最小4096个字节时,否则while循环将永远不会启动。

关于Reader API,基类java.io.Reader( http://docs.oracle.com/javase/7/docs/api/java/io/Reader.html#read ( char[],% 20int,% 20int) ) )定义读取为抽象的方法,以及任何基于Reader的impl。 必须推动这种方法。 所以它就在那里!

关于你无法弄清楚一大块未知字节的编码。 是的,这是正确的。 但你可以猜测,就像来自mozilla尝试的探测器一样。 因为你有一些线索:1。我们希望字节是文本2.我们知道任何指定编码中的任何字节3.我们可以尝试解码猜测编码中的几个字节并比较结果字符串

关于我们是专家:是的,大多数使用的是;)但我们不喜欢为别人做作业。 我们喜欢修复错误或提供建议。 因此,提供一个完整的示例,提供我们可以解决的错误。 或者它发生在这里:我们给你一些伪代码的建议。 (我没有时间设置项目并为您写一个工作示例)

好评论线程;)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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