-2

I am new to Java and I am developing a GUI. The problem which I am facing is that I am unable to use the function "readLine()" in file handling. The intellisense is not recognizing this particular method, but I have seen codes in which this method runs perfectly. I have pasted my code below.

try
{
    Index ind= new Index();
    File file1 = new File(ind.path);
    File file2 = new File(file1.getAbsolutePath() + ".tmp");
    FileWriter fw1 =new FileWriter(file1,true);
    BufferedWriter bf1  = new BufferedWriter(fw1);
    BufferedWriter bf2 = new BufferedWriter(new FileWriter(file2));
    String line= null;
    while((line = bf1.readLine()) != null)
    {
        String tline = line.trim();
        if(tline.contains(inputVal))
        {
            continue;
        }
        else
        {
            bf2.write(line);

        }

    }

Hoping for a better response.

4

4 回答 4

6

You cannot read from a BufferedWriter, you have to use a BufferedReader.

于 2013-11-04T14:31:44.087 回答
2

readLine is undefined for BufferedWriter, use a BufferedReader instead

BufferedReader reader = new BufferedReader(new FileReader(ind.path));
于 2013-11-04T14:31:56.940 回答
1

You're using the wrong object, you're bf1 should be a BufferedReader, not a BufferedWriter since there is no readLine in this Class.

于 2013-11-04T14:36:54.947 回答
0

You need to use a BufferedReader as opposed to a BufferedWriter (which is what you're using in the code you provided above).

Have a look through this documentation - it outlines how to use a BufferedReader

http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html

Hope this helps you out.

于 2013-11-04T15:20:21.020 回答