mardi 21 avril 2015

Detect Encoding with Java

Vote count: 0

i need r help pleas?

i have an example wich is working very fine. with this example (see code below) I can detect the encoding of file using universaldetector framework from mozilla.

but I want that this example detect the encoding of input and not of the file for Example using class SCANNER? how can i modify the code below to detect the encoding of input instead of file?

thanx in advance

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();
  }
}

asked 52 secs ago



Detect Encoding with Java

Aucun commentaire:

Enregistrer un commentaire