this is what I have so far, to store what it reads into a array, now I need a way to pick 1 random line and output it. There needs to be an option but I have no idea how to make one, and have tried .getRandom(); for a name but have no idea for that either.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
public class TextFile
{
public static void main(String[] args)
{
File file = new File("/Users/Period_3/Desktop/test.txt");
StringBuffer contents = new StringBuffer();
BufferedReader reader = null;
try
{
reader = new BufferedReader(new FileReader(file));
String text = null;
String strLine;
String[] myarray;
myarray = new String[10];
// all lines are read
while ((text = reader.readLine()) != null)
{
contents.append(text)
.append(System.getProperty(
"line.separator"));
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} finally
{
try
{
if (reader != null)
{
reader.close();
}
} catch (IOException e)
{
e.printStackTrace();
}
}
// show file contents here
System.out.println(contents.toString());
}
}

New Topic/Question
Reply




MultiQuote





|