Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Java Objectives Question
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


bob
Java Objectives Question
3 Answer(s)      2 years and 4 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.


Advertisement
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 Tutorials/Questions & Answers:
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
Advertisements
Part I. Exam Objectives
Part I. Exam ObjectivesPrev   Next    Exam Objectives Learn how to develop and deploy enterprise application
Question in Java ??
Question in Java ??   Welcome every One ,I have Q in Java : Write aprogram that print the falewing table using SQRT method in the Math Class? Number squrfoot
Question in Java ??
Question in Java ??  Welcome every One ,I have Q in Java : Write aprogram that print the falewing table using SQRT method in the Math Class? Number 0 ,2, . . . ,18,20 squrfoot 00000 1.4142 5.2426 5.4721
Question in Java ??
Question in Java ??  Welcome every One ,I have Q in Java : Write aprogram that print the falewing table using SQRT method in the Math Class? Number 0 ,2, . . . ,18,20 squrfoot 00000 1.4142 5.2426 5.4721
Question on java
Question on java  why java is not purely oop?   java is not purely OOP because 1.) Multiple Inheritance is not supported. 2.) It uses primitive data type. 3.) static method can be called without creating instances
Question Java>?
Question Java>?  **ï??· Create a new class called Bank and in the main method use class Account (lab 4) to create an array of 10 accounts. ï??· Enter data for all accounts (account number,name, balance). ï??· Add
java question
java question   Create a class called Complex to perform arithmetic operations with complex numbers. 1- Use double variables to represent the fields of the class. 2- Provide a no-argument constructor with default values in case
java question :)
java question :)  write java program to use vector in ArrayList with add, remove,sort   import java.util.Vector; public class... Element MUAYAD"); vc.add("Vector Element JAVA"); vc.add("Vector Element
JAVA QUESTION
JAVA QUESTION  How to view image on Frame in swing(or)awt in Java
JAVA QUESTION
JAVA QUESTION  How to view image on Frame in swing(or)awt in Java
JAVA QUESTION
JAVA QUESTION  How to view image on Frame in swing(or)awt in Java
java question
java question  find the 127 twin pair in java
java question
java question  the purpose of defining multiple constructor in java
java question
java question  what is the difference between path and classpath in java
java question
java question  How to find the longest and shortest line from the file in case of java and java program should read the filename from the keyboard
java question
java question  wats dynamic dispatching
java question
java question  how to write a java program that, given a friend name , display the phone number and the email address for that friend
java question
java question  Please, give me sample program for insert Audio(sound) on Frame in swing in java
java question
java question  anyone provide the code to call c++ function from java   Here you will find details how to implement the c++ into the java. http://www.javaworld.com/javatips/jw-javatip23.html
Java Question
Java Question  can u give me one expample in which conditions we... in java includes only abstract methods i.e. methods have signatures only (or we... in java defines that it must be implemented by any class to make use of it. One
java question
java question  what are the exactly J2EE concepts.. struts and hybernates coming under which catagiry
java question
java question  input any number and check if sum of alternate digit is palindrome
java question
java question  how to create date and time based pgm in java... in db table if date and time crossed automatically update in the db and inform to login user as pop ups   http://www.roseindia.net/jsp/loginstatus.shtml
java question
java question  i wrote to java code to get data populated in the Revision field of table InventDim 2.Failing to successfully do the above, i tried to use the ConfigId field of table InventDim for the same purpose, by labeling
java question
java question  sir, can you give me the code for online banking using netbeans and mysql in j2ee with its database connectivity
java question
java question  sir, can you give me the code for online banking using netbeans and mysql in j2ee with its database connectivity
java question
java question  when we use the Abstract class and interface?   Please visit the following link: Abstract class and Interface
java question
java question  Can any one send me the source code to get confirmation after the successful registration to our mobile
java question
java question  I am converting a .net website into java one. Can you help me how to code things step by step. project name - src -java -main -webapp - common - jsp - css - images - javascripts I
 

 

 

DMCA.com