0

Possible Duplicate:
How do I detect the encoding of some text?

How to distinguish the unicode text file and others of a text file?

I am doing a bulk upload file using java. first I write the inputs in excel file and then I do save as Unicode text(.txt) file. Then I will upload the Unicode text file, and read from my java class.

Here I have a problem. I can distinguish the .txt files and other than text files. But how can i find a file whether it is Unicode text file or other text file.

Community
  • 1
  • 1
Satya
  • 8,146
  • 9
  • 38
  • 43

1 Answers1

0

try this

import org.mozilla.universalchardet.UniversalDetector;

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

    // (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();
  }
}
Abhishek bhutra
  • 1,400
  • 1
  • 11
  • 29