URGENT HELP PLEASE. I keep getting errors and I really need the help. I have che
ID: 662503 • Letter: U
Question
URGENT HELP PLEASE. I keep getting errors and I really need the help. I have checked answers on Chegg, but none of the answers seem to work either.
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.
Explanation / Answer
Exercise-1:(please create file with given names)
import java.io.File;
import java.util.Date;
public class FileStatistics {
public static void main(String[] args) {
File file = new File("Quote.txt");
if (!file.exists()) {
System.out.println("File doesnot exist");
return;
}
System.out.println("File name:"+file.getName());
System.out.println("File size:"+file.length()+" bytes");
System.out.println("Last Modification:"+new Date(file.lastModified()));
}
}
Exercise-2
import java.io.File;
public class FileStatistics2 {
public static void main(String[] args) {
File file1 = new File("Quote.txt");
if (!file1.exists()) {
System.out.println("File doesnot exist");
return;
}
System.out.println("File "+file1.getName()+" file size:"+file1.length()+" bytes");
File file2 = new File("Quote.doc");
if (!file2.exists()) {
System.out.println("File doesnot exist");
return;
}
System.out.println("File "+file2.getName()+" file size:"+file2.length()+" bytes");
}
}
Exercise-3
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class DisplayBook {
public static void main(String[] args) throws IOException {
File file = new File("input.txt");
if (!file.exists()) {
file.createNewFile();
System.out.println("Enter favourate book name:");
Scanner keyboard = new Scanner(System.in);
String name = keyboard.nextLine();
FileOutputStream fos = new FileOutputStream(file);
fos.write(name.getBytes());
fos.flush();
fos.close();
keyboard.close();
}
System.out.println("Reading from file");
System.out.print("favourite book name:");
FileInputStream fis = new FileInputStream(file);
int content;
while ((content = fis.read()) != -1) {
System.out.print((char) content);
}
fis.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.