代码之家  ›  专栏  ›  技术社区  ›  Raheel Mushtaq

如何在Android中从.txt文件中读取阿拉伯语文本[duplicate]

  •  1
  • Raheel Mushtaq  · 技术社区  · 10 年前

    我正在尝试读取包含阿拉伯文本的文件,然后需要将读取的文本放置在文本视图中。。

    以下是我尝试的代码:

    public static String readRawFile(Context ctx, int resId) throws UnsupportedEncodingException {
    
    
        InputStream inputStream = ctx.getResources().openRawResource(resId);
        InputStreamReader isr = new InputStreamReader(inputStream, "UTF-8");
        BufferedReader reader = new BufferedReader(isr);
        String line;
        StringBuilder text = new StringBuilder();
    
        try {
            while ((line = reader.readLine()) != null) {
                text.append(line);
                Log.e("Line Read", line);
                text.append('\n');
            }
        } catch (IOException e) {
            return null;
        }
        return text.toString();
    }
    

    但我读取的文件如下

    (P3EP qDDGPqD1QN-EN@pFPqD1QN项目

    我应该如何读取文件,使读取的文本是阿拉伯语

    1 回复  |  直到 10 年前
        1
  •  0
  •   Seek Addo    10 年前

    试着只用Bufferreader,用它我可以同时读阿拉伯语和英语单词。而且您不需要UT-8编码。 我希望这能帮助您:

    public static void readArabic(String path) throws FileNotFoundException {
    
    
            try(BufferedReader red = new BufferedReader(new FileReader(path));) {
                String out;
                while ((out = red.readLine()) != null){
                    System.out.println(out);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    

    这是主要方法:

    public static void main(String[] args) throws FileNotFoundException {
    
            String path1 = "/Users/addodennis/Desktop/Projects/HotelReservation/src/Data/testAra";
            readArabic(path1);
    
    
        }
    

    这是输出: 我刚从维基百科页面上复制了一些阿拉伯语文本。

    §¨§±al-abjadyah-al-arabyah或§±§±¨ (§)或alifb(§) § §§± §± ° ¨ § ¨ § (¨§ § § § §± (¨§) § ± §§ §§ §¨ §.

    如果您仍然想使用StringBuilder,那么您也可以这样做:

        public static String readArabic(String path) throws FileNotFoundException {
    
        StringBuilder add = new StringBuilder();
        try(BufferedReader red = new BufferedReader(new FileReader(path));) {
            String out;
            while ((out = red.readLine()) != null){
                //System.out.println(out);
                add.append(out);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return String.valueOf(add);
    }