Home Answers Viewqa Java-Beginners Java Objectives Question

 
 


bob
Java Objectives Question
3 Answer(s)      11 months ago
Posted in : Java Beginners

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 that displays the sizes of the two files, as well as the ratio of the two file sizes. Save the file as FileStatistics2.java.

Exercise 3: 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

June 18, 2012 at 3:49 PM


Exercise 1:

Here we have created a text file and display its name, size and last modification date & time.

import java.io.*;
import java.util.*;
    import java.text.*;

class FileStatistics{
    public static void main(String[] args){
        try{
        File f=new File("c:/data.txt");
        BufferedWriter bw=new BufferedWriter(new FileWriter(f));
        bw.write("Hello World!");
        bw.close();
    String name=f.getName();
        long len=f.length();
        long lastModified=f.lastModified();
        System.out.println("File Name: "+name);
        System.out.println("File Sie: "+len);
        Date d=new Date(lastModified);
        SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy hh:mm");
        System.out.println("File Last Modified: "+sdf.format(d));
        }
        catch(Exception e){
          }
    }
    }

June 19, 2012 at 11:57 AM


Exercise 2

Here, we have created a text editor such as Notepad and save the file as Quote.txt and ask the user to enter the favorite movie quote. Then write it into the text file and Copy the file contents and save them into a word file 'Quote.doc'. After that, we have displayed the sizes of the two files.

import java.io.*;
import java.util.*;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.poifs.filesystem.*;
import org.apache.poi.hwpf.extractor.WordExtractor;

class FileStatistics2
{
    static void writeToFile(String content, String path){
        try{
        POIFSFileSystem fs = new POIFSFileSystem();
        DirectoryEntry directory = fs.getRoot();
        directory.createDocument("WordDocument", new ByteArrayInputStream(content.getBytes()));
        FileOutputStream out = new FileOutputStream(path);
        fs.writeFilesystem(out);
        out.close();
        }
        catch(Exception ex) {
        System.out.println(ex.getMessage());
       }
    }
    public static void main(String[] args) 
    {
        File f=new File("c:/Quote.txt");

        try{
        Scanner input=new Scanner(System.in);
        System.out.println("Enter fav movie quote: ");
        String str=input.nextLine();
        BufferedWriter bw=new BufferedWriter(new FileWriter(f,true));

            bw.write(str);
            bw.newLine();
            bw.close();

        BufferedReader br=new BufferedReader(new FileReader(f));
        String s="",st="";
        while((s=br.readLine())!=null){
            st+=s+" ";
        }
        File file=new File("c:/Quote.doc");
             writeToFile(str,file.getPath());
             System.out.println("Text File Size: "+f.length());
             System.out.println("Word File Size: "+file.length());

        }
        catch(Exception e){}
     }
    }

June 20, 2012 at 12:43 AM


On number 2 I'm getting this errors

FileStatistics2.java:3: error: package org.apache.poi.hwpf does not exist import org.apache.poi.hwpf.HWPFDocument; ^ FileStatistics2.java:4: error: package org.apache.poi.hwpf.usermodel does not exist import org.apache.poi.hwpf.usermodel.*; ^ FileStatistics2.java:5: error: package org.apache.poi.hwpf.usermodel does not exist import org.apache.poi.hwpf.usermodel.Range; ^ FileStatistics2.java:6: error: package org.apache.poi.poifs.filesystem does not exist import org.apache.poi.poifs.filesystem.*; ^ FileStatistics2.java:7: error: package org.apache.poi.hwpf.extractor does not exist import org.apache.poi.hwpf.extractor.WordExtractor; ^ FileStatistics2.java:13: error: cannot find symbol POIFSFileSystem fs = new POIFSFileSystem(); ^ symbol: class POIFSFileSystem location: class FileStatistics2 FileStatistics2.java:13: error: cannot find symbol POIFSFileSystem fs = new POIFSFileSystem(); ^ symbol: class POIFSFileSystem location: class FileStatistics2 FileStatistics2.java:14: error: cannot find symbol DirectoryEntry directory = fs.getRoot(); ^ symbol: class DirectoryEntry location: class FileStatistics2 8 errors









Related Pages:
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
Java Objectives Question
Java Objectives Question  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
Sun Certified Java Professional (SCJP)
be correct in 210 minutes). The pattern of the question is multiple choice and drag & drop in both version. Exam Objectives
question
question   sir plz tell me what should i give in title box. just i want java program for the question typed in this area
question
question   sir plz tell me what should i give in title box. just i want java program for the question typed in this area
Question
Question   When there is an exception in my program how java runtime system handles
question
question  dear sir/madam my question is how to compare two text format in java..we are java beginners..so we need the complete source code for above mentioned question...we have to compare each and every word
question
question  how to capture web snippets using java or java script
question
question  why java is platform independent in nature
question
question  how to solve the problem of The requested resource is not available+java+eclipse
question
question  select from table using jsp , java bean and mysql
QUESTION
QUESTION  please atach java tutorial to my email   Hello Friend, Please visit the following link: Java Tutorial Thanks
question
question  which language is leading the world java or .net and which language followed by all the Oops principles either java or .net
question
question  do you have any idea about the following concept,give me Java code for A COMPARATIVE STUDY ON PERSONALIZATION IN INFORMATION RETRIEVAL WITH SHARING OF THE CONCEPT BASED USER PROFILES using Java
question
question  Dear Sir, could you please send me a simple example of java and database connectivity with java and sql   Please visit the following link: JDBC Tutorials
question
question  Good Afternoon, code to display the employees on leave,and approve that leave for manager.using JPanel+mysql+Java
question
question  i need a code for time comparison in the basis of hour in java, based on grigorian calendar
question
question  i need a sample java code for concept based user profile, and search engine personalization
question
question  Please give me a code for search engine using java and mysql
question
question  Good afternoon Sir, please send me the Java source code for Search engine
question
question  Sir, Please help me to develop a simple search engine model in java , send me some codes
question
question  Good Morning Sir, Please give me Java source code for Peer-to-Peer Streaming
question
question  Sir, Please give me a source code for client server socket programing in java
question
question  Sir, Please send me Java Swing source code for video streaming , it's very urgent
Question
Question  Sir, give me a java program to count the number of tokens , given a string and a seperator
question
question  how to unzip afile using huffman algorithm in java   Hi, Please see at Huffman example. Thanks
question
question  Good Morning Sir, submit attendance to database using system IP address with current system date and time.using JPanel+mysql+Java
question
question  Good afternoon Sir, how to solve the problem of The requested resource is not available+java+eclipse if you can play a demo
question
question  i need to select data from database using mysql+java script+html   Please specify some more details
question
question  Good Afternoon Sir, Please give me a client Server Socket programming using jsp and java with mysql connection
Question
Question  Sir, give me a Java program to read data from keyboard up to names , sort in alphabetical order using bubble sort
question
question  Good Afternoon Sir, give me the source code to submit attendance to data base using System IP Address with system date and time.using JPanel+mysql+java
question
question  good afternoon sir, please send me a simple Jsp or Java code to display a calendar with Indian public holidays,current date and time.It is very urgent,please consider and me as soon as possible
question
question  please give me the relevant modules and details to start java coding for "Deriving Concept-based User Profiles from Search Engine Logs" it's very urgent
question
question  Sir , i have a starting trouble to start my search engine project , please help me to start my project , please send me some relevant java codes
question
question  Sir, how to display web snippets for a perticular query for eg:web snippets for apple how to implement this concept in my java project please help me
question
question  Sir, How to stream video on one computer which is playing on another PC in LAN using java + socket / RMI . if you have any idea about that please help me and give the source code
question
Action in JSP  more than one actions done in a jsp in struts2 and controll go in different java class   Have a look at the following link: Sruts Tutorials
Question
Question  Sir, please help me to write a JAVA program to read data from keyboard up to names and sort in alphabetical order using bubble sort
question
question  Sir, If i have a frame with two button sign in and cancell using java swing .If i am clicking a sign in button using user name... the source code using Java Swing
question
question  Dear Sir, Could you please send me a simple and standard example including Java bean class , use of that bean class in JSP   ...://www.roseindia.net/jsp/loginbean.shtml http://www.roseindia.net/jsp/java-beans.shtml
Question
java interview questions and answers for one year experianced candidates  Good afternoon sir could you please send me program style java interview questions and answers for one year experianced candidates.ie ,the questions like
Question
program style java interview questions and answers  Good afternoon sir could you please send me program style java interview questions and answers for one year experianced candidates.ie ,the questions like find out the result
question
question  Hi good morning Sir, Use grid -That displays employee name,employee_id,status(P/A),check box or Approve button. (Button in each row of the grid and below a button for approving all attendances marked )-using java/jsp
question
question  Hi good morning Sir, Use grid -That displays employee name,employee_id,status(P/A),check box or Approve button. (Button in each row of the grid and below a button for approving all attendances marked )-using java/jsp
question
,jsp and simple java script.i don't know ajax
question
all attendances marked )-using JPannel+java + mysql
question
question  Good after noon Sir, Please send me the code of the problem given below.it's urgent code to display employees on absent and manager can approve,and send a notification message to an employee.using JPanel+mysql+Java
question
question  i need to select some details form database and that will displays on the same jsp page which is already contains some other details ,mustn't go to next page while i am clicking a link. have any option in jsp or java
question
to develop my java project

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.