2

我正在执行java代码。readLine()即使文件中有文本,该方法也会从文本文件中返回一个空字符串。

BufferedReader csv =  new BufferedReader(new FileReader("D:/SentiWordNet_3.0.0/home/swn/www/admin/dump/senti.txt"));
String line = "";      
while((line = csv.readLine()) != null){ 
    String[] data = line.split("\t");
    Double score = Double.parseDouble(data[2])-Double.parseDouble(data[3]);
}

调用后split(),抛出异常Arrayindexoutofboundsexception
下面是文本文件。每行以"a"数字开头。该代码能够检索到带有单词 apocrine 的行,但不能检索带有单词 eccrine 的行。当我在调试模式下运行时,行变量返回为空字符串。

a 00098529 0 0 大汗腺#1(外分泌腺)产生分泌物,其中部分分泌细胞随分泌物一起释放;“母乳是一种大汗腺分泌物”

a 00098736 0.25 0 eccrine#1(外分泌腺)产生清澈的水性分泌物,但不释放部分分泌细胞;对调节体温很重要

a 00098933 0 0 自流#1(水)在内部静水压力下上升到地表;“自流井”;“自流压力”

我是否应该使用其他构造来读取文本文件中的行

4

6 回答 6

1

您可以在下面的 readline() 的 javadoc中看到BufferedReader..

读取一行文本。一行被认为是由换行符 ('\n')、回车符 ('\r') 或紧跟换行符的回车符中的任何一个终止的。

因此,如果您的文本包含换行符('\n')和回车符,BufferedReader则将返回一个空字符串。考虑跟随字符串。

abc\n\rdef

如果您调用3 次,这将返回"abc", 。不仅是上面的String,下面的String也可能会导致同样的结果。"""def"readLine()

abc\n\n定义

abc\r\rdef

在您的文本文件中,它必须包含这些组合中的一种或多种。或者它可能包含whitespases在那些特殊字符之间。例如:

abc\n\t\n定义

abc\n \rdef

等等...

这就是为什么你得到一个空行。

要克服这个问题,您可以检查while-loop.

while ((line = csv.readLine()) != null) {
    if(line.trim().isEmpty()){
        continue;
    }
    //Your code
}
于 2015-12-25T03:01:48.980 回答
0

阅读每一行:

while ((thisLine = br.readLine()) != null) {
     System.out.println(thisLine);
   }

如果这不起作用,那么我认为您的文本文件有问题。

于 2013-08-30T12:39:55.080 回答
0
        //Get scanner instance
        Scanner scanner = new Scanner(new File("SampleCSVFile.csv"));

        //Set the delimiter used in file
        scanner.useDelimiter(",");

        //Get all tokens and store them in some data structure
        //I am just printing them
        while (scanner.hasNext()) 
        {
            System.out.print(scanner.next() + "|");
        }

        //Do not forget to close the scanner  
        scanner.close();
于 2013-08-30T12:51:11.110 回答
0

以下是从文件中读取数据的示例方法。
在这里读取每一行后,将其添加到 arraylist 并返回 arraylist。

public ArrayList<String> fileRead(String fileName){
        File           f;
        String         s;
        FileReader     fr = null;
        BufferedReader br = null;
        ArrayList<String>   sl = new ArrayList<String>();
        try {
            f  = new File(fileName); 
            fr = new FileReader(f);
            br = new BufferedReader(fr);
            while((s=br.readLine())!=null){
                sl.add(s);
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
                try {
                    if(br!=null)
                        br.close();
                    if(fr!=null)
                        fr.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
        return sl;
    }
于 2013-08-30T12:24:00.130 回答
0

尝试使用扫描仪:

Scanner in = new Scanner(new FileReader("filename.txt"));
while (in.hasNext()){
   String str = in.next());
   // Use it
}
于 2013-08-30T12:35:34.420 回答
-1

关键是你错误地使用了 BufferedReader,如果你像使用 FileReader

new FileReader( filename ) 

这里的文件名是文件路径,如“./data/myfile.txt”。ecplise 或编译器不会给出编译错误或警告,但是,这是一个致命错误,如果您随后使用 readLine(),将导致从文件中读取任何内容。像这样的正确方法:

BufferedReader csv =  new BufferedReader(new FileReader( new File("filename") ) )
csv.readLine()

我试了你的文件,发现你的文件格式不对。您的文件格式如下:a 00098529 0 0 每个字符串都由空格分隔,但不是制表符,因此当您使用 split("\t") 时,您什么也得不到。给定您的文件格式,您应该使用 split(" ") 或者您应该通过用制表符划分每个字符串来更改文件格式

于 2013-08-30T12:50:27.737 回答