PDA

View Full Version : Call external file (URGENT)


Bobo
02-28-04, 09:20 PM
I want to call a menu from external file. Then count number of record and print the menu by a for loop. But the following code doesn't work.

As I will use menu.length in remaining program, I cannot set it as a constant length. So I use an integer 'count' to count it's length. Is it correct?


import java.text.*;
import java.io.*;

public class Test
{

public static void main(String[] args) throws IOException
{

File inFile = new File("menu.dat");
FileReader fileReader = new FileReader(inFile);
BufferedReader bufReader = new BufferedReader(fileReader);
String line;
int count = 0;

line = bufReader.readLine();


while (line != null)
{
count++;
line = bufReader.readLine();

String[]menu = new String[count];
for (int i=0; i<count; i++)
{
menu[i]= bufReader.readLine();
System.out.println (menu);
}
}
}
}

stdunbar
03-01-04, 11:00 AM
Don't bother with an array - use the collections packages for what they are there for:

import java.text.*;
import java.io.*;
import java.util.Vector;

public class Test
{

public static void main(String[] args) throws IOException
{

File inFile = new File("menu.dat");
FileReader fileReader = new FileReader(inFile);
BufferedReader bufReader = new BufferedReader(fileReader);
String line;

line = bufReader.readLine();

Vector menu = new Vector();

while (line != null)
{
menu.add( line );
line = bufReader.readLine();
}

for (int i=0; i<menu.size(); i++)
{
System.out.println (menu.elementAt(i));
}
}
}



I want to call a menu from external file. Then count number of record and print the menu by a for loop. But the following code doesn't work.