Java programming file Details – Build a class called FileDetails.java. When you
ID: 3793544 • Letter: J
Question
Java programming
file Details – Build a class called FileDetails.java. When you instantiate this class and give it a filename, it will report back the size of the file, whether the file is Readable and whether the file is Writeable; plus any other file information that you might deem important.
This cd goes in mainà
FileDetails fd=newFileDetails(“anyfile.doc”);
All other code goes in the constructor.
Writing to a File – This time build a FileWrite.java. This class, when instantiated, will create a new file and write a String to the file. This coed goes in mainà
FileWrite fw = new FileWrite(“myfile.txt”, “Go Braves”);
All other code goes in the constructor.
Reading from a File – This time build a class called FileRead.java. This class, when instantiated, will read the text from a file and print the data to the Console. This code goes in mainà FileRead fr = new FileRead(“myfile.txt”);
All other code goes in the constructor.
Explanation / Answer
Tested on eclipse , windows OS, JDK 1.8
/**********************FileDetails.java****************/
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
public class FileDetails {
public FileDetails() {
}
public FileDetails(String fileName) {
/**
* file object creation
*/
File file = new File(fileName);
/**
* converting into path
*/
Path path = file.toPath();
BasicFileAttributes attr;
try {
/**
* Getting attribute of file
*/
attr = Files.readAttributes(path, BasicFileAttributes.class);
System.out.println("creationTime: " + attr.creationTime());
System.out.println("lastAccessTime: " + attr.lastAccessTime());
System.out.println("lastModifiedTime: " + attr.lastModifiedTime());
System.out.println("isDirectory: " + attr.isDirectory());
System.out.println("isOther: " + attr.isOther());
System.out.println("isRegularFile: " + attr.isRegularFile());
System.out.println("isSymbolicLink: " + attr.isSymbolicLink());
System.out.println("size: " + attr.size());
System.out.println("Is readable: " + file.canRead());
System.out.println("Is Writable: " + file.canWrite());
System.out.println("Free Space: " + file.getFreeSpace());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
FileDetails fd = new FileDetails("myfile.txt");
}
}
/********************output*********************/
creationTime: 2017-02-16T17:33:11.768736Z
lastAccessTime: 2017-02-16T17:33:11.768736Z
lastModifiedTime: 2017-02-16T17:33:11.769237Z
isDirectory: false
isOther: false
isRegularFile: true
isSymbolicLink: false
size: 9
Is readable: true
Is Writable: true
Free Space: 238501961728
/***************************FileRead.java*******************************/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileRead {
public FileRead() {
}
/**
* In this constructor we are reading file
*
* @param fileName
*/
public FileRead(String fileName) {
/**
* Variable declaration of BufferedReader and fileReader
*/
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(fileName);
br = new BufferedReader(fr);
String sCurrentLine;
br = new BufferedReader(new FileReader(fileName));
/**
* Reading file line by line
*/
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
/**
* Printing exception
*/
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
new FileRead("myfile.txt");
}
}
/**************************output*********************/
Go Braves
/***********************************FileWrite.java***************************/
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class FileWrite {
public FileWrite() {
}
/**
* This constructor is used for writing data into file if file does not
* exist then it will create new file and write content into file
*
* @param fileName
* @param content
*/
public FileWrite(String fileName, String content) {
/**
* Object creation of BufferedReader and FileWrite
*/
BufferedWriter bw = null;
FileWriter fw = null;
try {
fw = new FileWriter(fileName);
bw = new BufferedWriter(fw);
/**
* Writing into file
*/
bw.write(content);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
/**
* closing all connections
*/
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
new FileWrite("myfile.txt", "Go Braves");
}
}
/******************************content of output file***************************/
Go Braves
Thanks a lot
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.