我正在尝试使用 Java 读取阿拉伯文本,但扫描仪看不到任何元素,因此尽管 LineNumberReader 可以识别文本文件中的行,但读取不成功。
我在英文文本上尝试了相同的代码,效果很好。
我使用的是netbeans 7.0.1
这是我的代码:
public class ReadFile {
private int number_of_words;
private File f1;
private String array[][],lines[];
private Scanner scan1;
public ReadFile(String sf1) throws FileNotFoundException
{
f1=new File(sf1);
scan1=new Scanner(f1);
}
public String[][] getA()
{
return array;
}
public void read() throws IOException
{
int counter=0,i=0;
LineNumberReader lnr = new LineNumberReader(new FileReader(f1));
lnr.skip(Long.MAX_VALUE);
number_of_words=lnr.getLineNumber();
array = new String[2][number_of_words];
lines = new String[number_of_words];
while(scan1.hasNext())
{
String temp;
temp=scan1.nextLine();
lines[counter++] = temp;
System.out.println(lines[counter-1]+"\t"+lines.length);
}
Arrays.sort(lines);
counter=0;
while(i<lines.length)
{
String temp = lines[i++];
StringTokenizer tk=new StringTokenizer(temp,"\t");
array[0][counter] = tk.nextToken();
array[1][counter++] = tk.nextToken();
}
}
}
最佳答案
默认情况下,扫描仪使用系统编码。读取数据特殊字符时需要使用正确的字符编码。
scan1=new Scanner(f1, "UTF-8");
如果 UTF-8 不起作用,您需要尝试使用阿拉伯语特定编码。
这里有几个可能有用的链接 File reading practices和 Java supported encodings
关于java - 无法在Java中读取阿拉伯文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9215104/