I’m studying for finals and I’m trying to do the following:
Open the file nFile, and print the string that corresponds to line number start to line number finish, in the file nameFile.
Must handle error message when the line number start or finish do not exist, should print the string “Line not found” if an IOException occurs, the method should print “IO Error”.
I’m using a text file with the following:
11
1c20
203
G2
If 2,4 is entered, “1c20 203 G2”. will be printed
I have researched a lot and I’m stuck on printing the strings when a line number is entered.
import java.io.*;
public class files {
public void dR(String nFile, int start, int finish)
{
try{
FileInputStream fstream = new FileInputStream("C://rest//dat.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
nameFile=null;
start=0;
finish=0;
// if start is greater or = to 1 and finish less than 4
while((nFile=br.readLine())!=null && start>= 1 && finish <= 4)
{
for(int i=0; i<=start; i++)
{
System.out.println(nFile);
br.close();
}
System.out.println("that doesnt exsist");
}
}
catch(IOException e)
{
System.out.println("there is an IO error");
}
}
}
you are throwing away all input you get from the parameter
also add a finally block that will do the close