In: Computer Science
package filesystem;
// Defines an interface Path
public interface Path
{
// Constants stores the current path
public static final String FILEPATH =
"E:/Workspace/Student/";
// Method to return file path
public String getPath();
}// End of interface
-------------------------------------------------------------------------------------------------
package filesystem;
// Defines class FileSystem implements interface Path
public class FileSystem implements Path
{
// Overrides method of Path interface to return file
path
public String getPath()
{
return FILEPATH;
}// End of method
}// End of FileSystem
-------------------------------------------------------------------------------------------------
package filesystem;
import java.io.*;
// Defines driver class FileSystemTest
public class FileSystemTest
{
// main method definition
public static void main(String []ss)throws
IOException
{
// Calls the method to get file
path
String path = new
FileSystem().getPath();
// Displays the path
System.out.println("\n Current
Path: " + path);
// To store file name
String fileName;
// Declares an object of class
BufferedReader for file
BufferedReader fileBr = null;
// Declares an object of class
BufferedReader for console
BufferedReader input = null;
// Declares an object class File
for file operation
File file = null;
// Declares an object of FileReader
to read file
FileReader fileReader = null;
try
{
// Creates the
object to accept data from console
input = new
BufferedReader(new InputStreamReader(System.in));
// Accepts the
file name
System.out.print("\n Enter the file name: ");
fileName =
input.readLine();
// Generates
file path
file = new
File(path + fileName);
// Opens the
file for reading
fileReader = new
FileReader(file);
// Attach
buffered reader to file reader
fileBr = new
BufferedReader(fileReader);
String
line;
// Loops till
end of the file and reads a line
while ((line = fileBr.readLine()) != null)
// Displays the line
System.out.println(line);
}// End of try block
// Closing operation
finally
{
fileBr.close();
input.close();
fileReader.close();
}// End of finally
}// End of main method
}// End of driver class
Sample Output:
Current Path: E:/Workspace/Student/
Enter the file name: employeeData.txt
Pyari Sahu P111 89.99 80
Sasmita Sahu S121 78.12 60
Ram Panda R122 32.12 12
Anil Sharma A211 45.7 30
Rohit Panda R133 16.36 19
Tanvi Sahu 2T21 79.91 65
Manvi Sahu M331 92.99 70
Binod Sharma B129 61.33 22