The program \"Sample class to read from a file\", it reads information from the
ID: 3615490 • Letter: T
Question
The program "Sample class to read from a file", it reads information from
the file, can I save that information into a variable?
Let's say the .txt file has a '5' in it, can I add that 5 to another
variable in? If so how would I go about it? Thank you.
import java.io.*;
public class SimpleReader
{
public void readAndPrintFile(String fileName)
{
String line = null;
// try to do the following
try
{
// create the buffered reader
BufferedReader reader =
new BufferedReader(new FileReader(fileName));
// Loop while there is more data
while((line = reader.readLine()) !=null)
{
// print the current line
System.out.println(line);
}
// close the reader
reader.close();
}
catch(FileNotFoundException ex)
{
SimpleOutput.showError("Couldn't find " + fileName + " please pick it.");
fileName = FileChooser.pickAFile();
readAndPrintFile(fileName);
}
catch(Exception ex)
{
SimpleOutput.showError("Error reading file " + fileName);
ex.printStackTrace();
}
}
public static void main(String[] args)
{
SimpleReader reader = new SimpleReader();
reader.readAndPrintFile("SHIPS.txt");
}
}
Explanation / Answer
x.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.