Question

In: Computer Science

in java please!!! suppose there is a text file named TextFile.txt with some data in the...

in java please!!!

suppose there is a text file named TextFile.txt with some data in the root directory (e.g. C:\ or \) of your computer. In JAVA, write code that (i) opens the file in read mode; (ii) displays the file content on the monitor screen/console; (iii) uses exception handling mechanism with try, catch, and finally blocks; and (iv) safely closes the file. Writing the main() method is optional.

Solutions

Expert Solution

Java Code:-

import java.io.*;
public static void readFile {

    String fileName ="C:\\tesstfile.doc";
  
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new FileReader(new File(fileName)));
        String line =null;
        while ((line = reader.readLine())!=null){
            System.out.println(line);
        }
    } catch (FileNotFoundException e) {

    } catch (IOException e) {

    } finally {
        if(reader!=null) {
            try {
                reader.close(); // close the file surrounded by try catch block
            } catch (IOException e) {

            }
        }
    }

}

=========================================================================

Note: Could you please consider my effort on this work and give me a UPVOTE. Thank you :)


Related Solutions

Given some data in a text file, the task is to scramble the text and output...
Given some data in a text file, the task is to scramble the text and output in a separate text file. So, we need to write a Python program that reads a text file, scrambles the words in the file and writes the output to a new text file. Rules to be followed: Words less than or equal to 3 characters need not be scrambled. Don’t scramble first and last char, so Scrambling can become Srbmnacilg or Srbmnailcg or Snmbracilg,...
Write a program in Java that reads an input text file (named: input.txt) that has several...
Write a program in Java that reads an input text file (named: input.txt) that has several lines of text and put those line in a data structure, make all lowercase letters to uppercase and all uppercase letters to lowercase and writes the new lines to a file (named: output.txt).
Consider a text file that you will create named “employees.txt”. The file contains data organized according...
Consider a text file that you will create named “employees.txt”. The file contains data organized according to the following format:John Smith 10 15Sarah Johnson 40 12Mary Taylor 27 13Jim Stewart 25 8For instance, “John” is the first name, “Smith” is the last name, “10” is the number of hours per week, and “15” is the hourly rate.Write a program that computes the weekly salary of each employee. The program prints the first name, last name, and weekly salary of each...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
in java: Write a class responsible for storing data in a text file. The class should...
in java: Write a class responsible for storing data in a text file. The class should include a method that accepts a string and appends it to a file.
In this PYTHON 3 program assignment, you will find a text file named WorldSeries.txt. This file...
In this PYTHON 3 program assignment, you will find a text file named WorldSeries.txt. This file contains a chronological list of the World Series' winning teams from 1903 through 2018. The first line in the file is the name of the team that won in 1903, and the last line is the name of the team that won in 2018. (Note the World Series was not played in 1904 and 1994. There are entries in the file indicating this.) Write...
Q4. Assume you have been given a scrambled text file with some hidden text data similar...
Q4. Assume you have been given a scrambled text file with some hidden text data similar to the one in your assessment. What will be the best method that you will use to unscramble the file and why would you choose this method? Justify your answer. [5 marks] You have collected a digital evidence from a crime scene and calculated its hash value using WinHex editor with MD5 algorithm. You have stored the evidence in a forensics lab. After a...
Write a java program: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program read an "input.txt" file (like below) and store the information in an respective arrays. This input.txt file will look like below and will be stored in the same directory as the java file. The top line of the text represents the number of people in the group for example. the lines below it each represent the respective persons preferences in regards to the other...
Write all your answers for this problem in a text file named aa.txt – for each...
Write all your answers for this problem in a text file named aa.txt – for each problem write the problem number and your answer. 1.1 What type of object can be contained in a list (write the letter for the best answer)? a. String b. Integer c. List d. String and Integer only e. String, Integer, and List can all be contained in a list 1.2 Which statement about the debugger is not correct? a. It is a powerful tool...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT