Java读取ANSI文件错误

发布于 2024-11-05 05:07:34 字数 352 浏览 1 评论 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”)

有什么建议吗?

I tried to read an ANSI encoded Arabic file in Java using the following two way

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

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

I tried also to read with default encoding (i.e. I omitted the "ISO-8859-6")

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白况 2024-11-12 05:07:34

试试这个代码:

 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`
        }
    }
}

Try this code:

 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`
        }
    }
}
雪化雨蝶 2024-11-12 05:07:34

看看这个:

       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

Look at this:

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

This file has this characters http://www.alanwood.net/demos/ansi.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文