0

Possible Duplicate:
Best way to read a text file

In Java I can open a text file like this:

BufferedReader reader = new BufferedReader(new FileReader("file.txt"));

My question is, how do you read from the following file? The first line is a number (830) representing number of words, and the following lines contain the words.

830  
cooking   
English  
weather  
.  
.  

I want to read the words into a string array. But how do I read the data first?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
user793384
  • 63
  • 1
  • 1
  • 3

5 Answers5

3

You're on the right track; I would treat the first line as a special case by parsing it as an integer (see Integer#parseInt(String)) then reading the words as individual lines:

BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
String numLinesStr = reader.readLine();
if (numLinesStr == null) throw new Exception("invalid file format");
List<String> lines = new ArrayList<String>();
int numLines = Integer.parseInt(numLinesStr);
for (int i=0; i<numLines; i++) {
  lines.add(reader.readLine());
}
maerics
  • 133,300
  • 39
  • 246
  • 273
1

Unless you have some special reason, it's not necessary to keep track of how many lines the file contain. Just use something like this:

BufferedReader reader = new BufferedReader(new FileReader("file.txt"));

String line;
while ((line = reader.readLine()) != null) {
    // ...
}
Snps
  • 13,516
  • 6
  • 49
  • 72
1

If you're working with Java version greater than 1.5, you can also use the Scanner class:

Scanner sc = new Scanner(new File("someTextFile.txt"));
List<String> words = new ArrayList<String>();
int lines = sc.nextInt();

for(int i = 1; i <= lines; i++) {
    words.add(sc.nextLine());
}

String[] w = words.toArray(new String[]{});
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Dragon8
  • 1,607
  • 12
  • 8
0

Try the class java.io.BufferedReader, created on a java.io.FileReader. This object has the method readLine, which will read the next line from the file:

try
{
    java.io.BufferedReader in = 
        new java.io.BufferedReader(new java.io.FileReader("filename.txt"));

    String str;
    while((str = in.readLine()) != null)
    {
        ...
    }
 }
 catch(java.io.IOException ex)
 {
 }
Jim
  • 631
  • 3
  • 9
0

You could use reflection and do this dynamically:

 public static void read() {

            try {
                BufferedReader reader = new BufferedReader(new FileReader(
                        "filename.txt"));

                String line = reader.readLine();

                while (line != null) {
                    if (Integer.class.isAssignableFrom(line.getClass())) {
                        int number = Integer.parseInt(line);
                        System.out.println(number);
                    } else {
                        String word = line;
                        System.out.println(word);
                    }

                    line = reader.readLine();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
Shawn
  • 6,855
  • 6
  • 29
  • 44