Question

In: Computer Science

In the root of the project create a folder called res. In that folder create a...

  1. In the root of the project create a folder called res. In that folder create a file called DemoFile.txt
  2. Create a class with a static main that tests the ability to resolve and print a Path:
    • Create an instance of a FileSystem class.
    • Create an instance of the Path interface for the DemoFile.txt file. Print the constructed Path with System.out.println() method.
  1. Create a class that does the following:
    • Using a pre-Java 7 solution, create a class that tests streams in the static main.
    • The class should instantiate a new File class, a new FileReader class, and new BufferedReader class.
    • Read lines by using the readLine() method call.
    • The file path used should be: <home directory of the current project>/res/employees.txt
    • The file should handle errors when the file is not found as well as reading the contents of the file when it is found

Solutions

Expert Solution

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


Related Solutions

Create a new folder called CSCI130_A3_yourname. Create all of the files indicated below within this folder....
Create a new folder called CSCI130_A3_yourname. Create all of the files indicated below within this folder. For this assignment you will be creating a total of 2 classes that meet the definitions below. This assignment will allow the user to enter in the coefficients for a polynomial function of degree 2. The user will then be asked to enter a number. The program will use that number as an argument to the function, and will print the corresponding function value....
Inside “Lab1” folder, create a project named “Lab1Ex3”. Use this project to develop a C++ program...
Inside “Lab1” folder, create a project named “Lab1Ex3”. Use this project to develop a C++ program that performs the following:  Define a function called “Find_Min” that takes an array, and array size and return the minimum value on the array.  Define a function called “Find_Max” that takes an array, and array size and return the maximum value on the array.  Define a function called “Count_Mark” that takes an array, array size, and an integer value (mark) and...
Inside “Lab1”folder, create a project named “Lab1Ex1”. Use this project to write and run a C++...
Inside “Lab1”folder, create a project named “Lab1Ex1”. Use this project to write and run a C++ program that produces:  Define a constant value called MAX_SIZE with value of 10.  Define an array of integers called Class_Marks with MAX_SIZE which contains students Mark. Define a function called “Fill_Array” that takes an array, and array size as parameters. The function fills the odd index of the array randomly in the range of [50- 100] and fills the even index of...
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama...
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama Write a static method in that class called makeLists that takes no parameters and returns no value In makeLists, create an ArrayList object called avengers that can hold String objects. Problem 2 Add the following names to the avengers list, one at a time: Chris, Robert, Scarlett, Clark, Jeremy, Gwyneth, Mark Print the avengers object. You will notice that the contents are displayed in...
Assume that there is an image “fewa.jpg” in the root of the HTML folder. Write HTML...
Assume that there is an image “fewa.jpg” in the root of the HTML folder. Write HTML code to insert that image with appropriate alternative text. Also, apply an appropriate title to the image.?
I. At the beginning, create a folder named "test" and add the folder into the version...
I. At the beginning, create a folder named "test" and add the folder into the version control tool tracking system. Then create one file named "001" under this folder. Commit any change if necessary. II. Next, add another file named "002" in the same folder and commit the change ("adding") in the version control tool. III. Followed by adding that file, create a branch (namely, "branch-A") in the version control tool and make some changes to the contents in file...
Using NetBeans, create a Java project named FruitBasket. Set the project location to your own folder....
Using NetBeans, create a Java project named FruitBasket. Set the project location to your own folder. 3. Import Scanner and Stacks from the java.util package. 4. Create a Stack object named basket. 5. The output shall: 5.1.Ask the user to input the number of fruits s/he would like to catch. 5.2.Ask the user to choose a fruit to catch by pressing A for apple, O for orange, M for mango, or G for guava. 5.3.Display all the fruits that the...
In BlueJ, create a project called Lab6 Create a class called LineSegment –Note: test changes as...
In BlueJ, create a project called Lab6 Create a class called LineSegment –Note: test changes as you go Copy the code for LineSegment given below into the class. Take a few minutes to understand what the class does. Besides the mutators and accessors, it has a method called print, that prints the end points of the line segment in the form: (x, y) to (x, y) You will have to write two methods in the LineSegment class. There is information...
language: python Create a text file in your project folder with at least 20 "quirky sayings"/fortunes...
language: python Create a text file in your project folder with at least 20 "quirky sayings"/fortunes (the only requirement is that they be appropriate for display in class), If I use my own file though, you should handle as many fortunes as I put in. Make each fortune its own line, •in your main function ask the user for the name of the fortunes file.•Create a function which takes the name of the fortunes file as a parameter, open that...
In STS, create Java Project, called java_generics_yourNameLastname that; (Under source directory called, src, create a package...
In STS, create Java Project, called java_generics_yourNameLastname that; (Under source directory called, src, create a package csci3444.generics and put all code in it) Create a generic interface called “MyGenInterface” that takes 2 generic types K,V with below methods; public K getKey(); public V getValue(); Create a generic class called “MyGenClass” that implements “MyGenInterface” interface. has attributes “K key” and “V value” that can be inherited has a constructor that takes “K _key”, “V _value” inputs and initializes “key”, “value” attributes...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT