I have a problem outputting the contents of a text file. Whenever I try to display the contents of a file a "null" word is printed at the end of the output.
Any help would be deeply appreciated.
import java.io.*;
import java.lang.*;
public class prog1
{
public static void main(String[] args)
{
readFile();
} //end of main
public static boolean isDoubleString(String s)
{
try
{
Double.parseDouble(s);
return true;
}
catch(NumberFormatException nx)
{
return false;
}
} // end of isDoubleString()
public static String kbdInput(String prompt)
{
String s = "";
System.out.print(prompt);
try
{
BufferedReader kbd = new BufferedReader(new InputStreamReader(System.in));
s = kbd.readLine();
}
catch(IOException iox)
{
System.out.println("No keyboard detected...");
System.exit(0);
}
return s;
} //end of kbdInput
public static void readFile()
{
String fname = kbdInput("Enter file name: ");
BufferedReader infile = null;
try
{
infile = new BufferedReader(new FileReader(fname));
}
catch(IOException iox)
{
System.out.println("File does not exist...");
System.exit(0);
}
String aLine = "";
try
{
while(aLine != null)
{
aLine = infile.readLine();
System.out.println(aLine);
}
infile.close();
}
catch(IOException iox)
{
System.out.println("File is empty...");
System.exit(0);
}
} //end of readFile
}