JAVA Objectives Question?

JAVA Objectives Question?

Write an application using the FileInputStream that opens a file that 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 DisplayBook.java

View Answers

July 3, 2012 at 3:33 PM

Here is a code that checks whether the given file exists or not. If file exists,then the code will display the data. Otherwise, it will allow the user to enter title's name and write it to the file. We have used FileInputStream and FileOutputStream for I/O operations.

import java.io.*;

class DisplayBook {
    public static void main(String[] args) 
    {
        int ch;
        try{
        File f=new File("c:/b.txt");
        if(f.exists()){
        StringBuffer buffer=new StringBuffer("");
        FileInputStream fis=new FileInputStream(f);
        while ((ch = fis.read()) != -1){
         buffer.append((char) ch);
        }
        System.out.println(buffer.toString());
        fis.close();
        }
        else{
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter Title: ");
            String str=br.readLine();
            FileOutputStream fos=new FileOutputStream(f);
            byte buf[] = str.getBytes();
            for (int i=0; i < buf.length; i += 2) {
             fos.write(buf[i]);
            } 
            fos.close();
        }
       }
        catch(Exception e){
        System.out.println(e);
        }
    }
}









Related Tutorials/Questions & Answers:
question1 - Java Beginners
JAVA Objectives Question?
Advertisements
Java Objectives Question
Core Java Interview Questions Page 2, Core Java QuestionQ
Java - Java Interview Questions
java questions - Java Interview Questions
java - Java Interview Questions
JAVA QUESTIONS - Java Beginners
interview questions - Java Interview Questions
interview - Java Interview Questions
Part I. Exam Objectives
Java Interview Questions
jvm - Java Interview Questions
Struts - Java Interview Questions
help me - Java Interview Questions
Questions about Java's String pool
java questions
java questions
java questions
java questions
Java Questions
Java Questions
java questions
java questions
java Questions
Java Questions
java certification questions - Java Beginners
java - Java Interview Questions
Interview Questions - What is Java?
java - Java Interview Questions
Java - Java Interview Questions
JAVA - Java Interview Questions
hint - Java Interview Questions
java - Servlet Interview Questions
java interview - JSP-Interview Questions
java - Java Interview Questions
Java Questions & Java FAQ
Interview Questions - Where Java is used?
java - Servlet Interview Questions
java - Java Interview Questions
Interview Questions - How Java is Used?
Java interview questions and answers
java - JSP-Interview Questions
Questions in Swing - Java Beginners
Core Java Interview questions and answers
Java interview questions
java servlets - Servlet Interview Questions
Need of Management by Objectives
Objectives of Fleet Management
java - Java Interview Questions

Ads