
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.

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){}
}
}
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.