0

我尝试使用以下两种方式在 Java 中读取 ANSI 编码的阿拉伯语文件

    Scanner scanner = null;
    try {
           scanner = new Scanner(new File("test/input.txt"), "ISO-8859-6");

    while (scanner.hasNextLine()) {
        String input =scanner.nextLine();
        processString(input);   
    }

我也尝试使用默认编码读取(即我省略了“ISO-8859-6”)

有什么建议么?

4

2 回答 2

0

试试这个代码:

 public static void transform(File source, String srcEncoding, File target,       String tgtEncoding) throws IOException {
    BufferedReader br = null;
    BufferedWriter bw = null;
    try {
        br = new BufferedReader(new InputStreamReader(new FileInputStream(source), Charset.forName(srcEncoding)));
        bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(target), tgtEncoding));
        char[] buffer = new char[16384];
        int read;
        while ((read = br.read(buffer)) != -1) {
            bw.write(buffer, 0, read);
        }
    } finally {
        try {
            if (br != null) {
                br.close();
            }
        } finally {
            if (bw != null) {
                bw.close();
            }`enter code here`
        }
    }
}
于 2017-03-12T01:28:39.543 回答
0

看这个:

       private static final String FILENAME = "/Users/jucepho/Desktop/ansi.txt";

   public static void main(String[] args) {
            BufferedReader br = null;
            FileReader fr = null;
            try {
                fr = new FileReader(FILENAME);
                br = new BufferedReader(fr);
                String sCurrentLine;
                br = new BufferedReader(new FileReader(FILENAME));
                while ((sCurrentLine = br.readLine()) != null) {
                    System.out.println(sCurrentLine);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (br != null)
                        br.close();
                    if (fr != null)
                        fr.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }

这个文件有这个字符http://www.alanwood.net/demos/ansi.html

于 2017-03-12T01:48:13.250 回答