-1

In my java program I need to read the contents of file line by line using java.I tried the following code but I am getting this error

import acm.program.*;  

import acm.util.*;
import java.io.*;
import java.util.*;

public class ReadFile  extends ConsoleProgram{

    private BufferedReader openFileReader(String Prompt){
        BufferedReader rd =null;
        while(rd==null){
            String name=readLine(Prompt);
            try{
            rd=new BufferedReader(new FileReader(name));

        }catch(IOException ex){
            println("cannot open");


        }

    }
        return rd;

    }

public void run(){
    BufferedReader rd = OpenFileReader('File');
    try{
        while(true){
            String line=rd.readLine();
            if(line==null){
                break;
            }
            rd.close();
            catch(IOException ex){
                throw new ErrorException(ex);

            }
        }
    }
}

}

I am getting error in this line

BufferedReader rd = OpenFileReader('File');

The error I am getting is "Invalid character constant". How should I resolve this?

user2492854
  • 291
  • 2
  • 9
  • 30

4 Answers4

2

You're treating the File phrase as if it was a character and not a string:

BufferedReader rd = OpenFileReader('File');

Change it to:

BufferedReader rd = OpenFileReader("File");
Yair Nevet
  • 12,046
  • 12
  • 61
  • 101
1

Short answer: Single quotes are for chars, not Strings. Changing the single quotes to double will solve your problem, as mentioned in Yair Nevet's answer.

The somewhat longer answer:

A String in Java has double quotes, such as ". This can have any number of characters between the opening and closing ".

A char in Java has single quotes, such as '. This can only have one character between the opening and closing '.

The Invalid character constant message comes up when you try to assign a char value with more than one character between the opening and closing '.

Community
  • 1
  • 1
Xynariz
  • 1,211
  • 1
  • 10
  • 27
0

Ya corrected it .That was the error. I am new to java programming..

user2492854
  • 291
  • 2
  • 9
  • 30
-2

The simple way, is to use org.apache.commons.io.FileUtils

FileUtils.readLines(new File("path"));
Nassim MOUALEK
  • 4,180
  • 3
  • 20
  • 38
  • 2
    The question is asking what caused the error, not how to read from a file. – Xynariz May 07 '14 at 16:11
  • writing a lot of codes to read a File is a loose of time, the is more cleaner using common library – Nassim MOUALEK May 07 '14 at 16:19
  • Yes, it can be complicated to read from files manually, but it's a good way to practice using Java, and a very common exercise in introductory Java courses. – Xynariz May 07 '14 at 16:21
  • So my response has a relationship with the title Question – Nassim MOUALEK May 07 '14 at 16:21
  • i dont read his code, i just read "Reading file contents line by line using Java" – Nassim MOUALEK May 07 '14 at 16:22
  • "I didn't read his code" - That's the problem. When you answer a question on StackOverflow, you should be answering what the question actually asks. The titles are very often misleading and/or inaccurate. – Xynariz May 07 '14 at 16:22
  • But when some one search in google he will find this page and its good to find the more short answer, not the 60 lines of codes – Nassim MOUALEK May 07 '14 at 16:26
  • Googling gives us [this question](http://stackoverflow.com/q/4716503/2030691), with [this accepted answer](http://stackoverflow.com/a/4716521/2030691), which are both sufficient. There are hundreds of duplicate-type questions (such as this one) on StackOverflow. These people are asking for how to fix their problem, not how to re-design everything they're doing. – Xynariz May 07 '14 at 16:30