Question

Java Write an application using the FileInputStream that OPENS a file which contains the name of...

Java

Write an application using the FileInputStream that OPENS a file which contains the name of the user's favorite book and then DISPLAYS it to the user. IF the file does not exist, PROMPT the user for the book's title, and then WRITE it to the file by using a FileOutputStream. Save the file as BookApplication.java.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;

public class BookApplication {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Scanner sc=new Scanner(System.in);
       try{
               File f = new File("book.txt");//to check if file exists or not
               if(!f.exists()){//if file doesn't exist create a new one and ask user for book name and write to it
                   f.createNewFile();
       OutputStream outputStream = new FileOutputStream("book.txt");
               System.out.println("Enter favorite book");
               String bookname=sc.nextLine();
               outputStream.write(bookname.getBytes());
               }else{//if file exists read book name from the file and display to user
           InputStream inputStream = new FileInputStream("book.txt");
           int ch;
           while((ch=inputStream.read())!=-1)
           System.out.print((char)ch);
               }
      
      
       } catch (IOException ex) {
       ex.printStackTrace();
       }

   }

}

Expected output:

when file doesn't exist the output will be

After writing data to file when file exists output will be

Add a comment
Know the answer?
Add Answer to:
Java Write an application using the FileInputStream that OPENS a file which contains the name of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Exercise 1: Create a file by using any word processing program or text editor. Write an...

    Exercise 1: Create a file by using any word processing program or text editor. Write an application that displays the file's name, size, and time of last modification. Save the file as FileStatistics.java. Exercise 2: Create a file that contains your favorite movie quote. Use a text editor, such as Notepad, and save the file as Quote.txt. Copy the file contents and paste them into a word processing program, such as Word. Save the file as Quote.doc. Write an application...

  • -Write a JavaFX application that displays a Label containing the opening sentence or two from your...

    -Write a JavaFX application that displays a Label containing the opening sentence or two from your favorite book. Save the project as FXBookQuote1a. -Add a button to the frame in the FXBookQuote program. When the user clicks the button, display the title of the book that contains the quote in a second label. Save the project as FXBookQuote1b.

  • 1.A. Write a JavaFX application that displays a Label containing the opening sentence or two from...

    1.A. Write a JavaFX application that displays a Label containing the opening sentence or two from your favorite book. Save the project as FXBookQuote1a. B. Add a button to the frame in the FXBookQuote program. When the user clicks the button, display the title of the book that contains the quote in a second label. Save the project as FXBookQuote1b.

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • write a python program that prompts the user for a name of a text file, opens...

    write a python program that prompts the user for a name of a text file, opens that file for reading , and tracks the unique words in a file and counts how many times they occur in the file. Your program should output the unique words and how often they occur in alphabetical order. Negative testing for a file that does not exist and an empty file should be implemented.

  • Write a program that performs the following: - Reads from the file "myfile.txt" using readlines() -...

    Write a program that performs the following: - Reads from the file "myfile.txt" using readlines() - Prints out the contents of the file "myfile.txt", that was read into a list by readlines(). Note that this should not look like a list, so you will need to loop through the list created by readlines and print the text. - Use the try/except method to create the file if it does not exist - If the file does not exist, prompt the...

  • Write a program in Java (using swing) that opens a window that contains a button. The...

    Write a program in Java (using swing) that opens a window that contains a button. The button is labled with "0" (zero). With every click on the button, the number in its label increments by 1.

  • USING JAVA and NOTTT using BufferedReader or BufferedWriter Write an application that implements ...

    USING JAVA and NOTTT using BufferedReader or BufferedWriter Write an application that implements a simple text editor. Use a text field and a button to get the file. Read the entire file as characters and display it in a TextArea. The user will then be able to make changes in the text area. Use a Save button to get the contents of the text area and write that over the text in the original file. Hint: Read each line from...

  • JAVA Write an application to test the HuffmanTree class. Your application will need to read a...

    JAVA Write an application to test the HuffmanTree class. Your application will need to read a text file and build a frequency table for the characters occurring in that file. Once that table is built create a Huffman code tree and then a string consisting of 0 and 1 characters that represents the code string for that file. Read that string back in and re-create the contents of the original file. Prompt the user for file name. Read the file,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT