JAVA QUESTION: We’ll continue working with file IO and exception handling by bui
ID: 3715817 • Letter: J
Question
JAVA QUESTION: We’ll continue working with file IO and exception handling by building a simple text-based interface for browsing the file system. When your program starts it should ask the user if they would like to dump results to a log file IN ADDITION to standard out. If so, your program should prompt for the name of the log file. If the file already exists, the contents should be overwritten.
The program will then display (6) options to the user:
1) List the contents of a directory provided by the user
2) List the contents of a directory provided by the user as well as all of its sub directories. (hint: use recursion)
3) Locate a file with a given name.
4) Locate files with a given file extension
5) Concatenate the contents of 2 files whose names are provided by the user and output the result to a third file (name also provided by the user)
6) Exit
Explanation / Answer
//FileSystem.java
package filesystem;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class FileSystem {
/**
* Generates and returns a hyphen based prefix for file/directory
* as per the level
* @param level
* @return
*/
public static String getPrefix(int level) {
String prefix = "";
while (level > 0) {
prefix += "-";
level--;
}
return prefix;
}
/**
* List contents of a directory
* @param dirName
* @throws IOException
*/
public static void listDirContents(String dirName) throws IOException {
Files.list(new File(dirName).toPath()).forEach(file -> {
System.out.println(file.getFileName());
});
}
/**
* List contents of a directory, can be called recursively
* with a parameter level
* @param dirName
* @param level
* @throws IOException
*/
public static void listDirContentsRec(String dirName, int level) throws IOException {
Files.list(new File(dirName).toPath()).forEach(file -> {
System.out.println(getPrefix(level) + file.getFileName());
if (Files.isDirectory(file)) {
try {
listDirContentsRec(file.toString(), level + 1);
} catch (IOException ex) {
Logger.getLogger(FileSystem.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
/**
* Locates a file with given file name
* @param dirName
* @param fileName
* @throws IOException
*/
public static void locateFile(String dirName, String fileName) throws IOException{
Files.list(new File(dirName).toPath()).forEach(file -> {
if(file.getFileName().equals(fileName)){
System.out.println("File found: "+file.toAbsolutePath().toString());
return;
}
if (Files.isDirectory(file)) {
try {
locateFile(file.toString(), fileName);
} catch (IOException ex) {
Logger.getLogger(FileSystem.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
System.out.println("File not found!");
}
/**
* Locates a file with given file name
* @param dirName
* @param fileExt
* @throws IOException
*/
public static void locateFileWithExt(String dirName, String fileExt) throws IOException{
Files.list(new File(dirName).toPath()).forEach(file -> {
if(Files.isRegularFile(file) && file.getFileName().endsWith(fileExt) ){
System.out.println(file.toAbsolutePath().toString());
}
if (Files.isDirectory(file)) {
try {
locateFileWithExt(file.toString(), fileExt);
} catch (IOException ex) {
Logger.getLogger(FileSystem.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
/**
* Concatenates contents of file1 and file 1 into fileOut
* @param file1
* @param file2
* @param fileOut
* @throws FileNotFoundException
* @throws IOException
*/
public static void concatenateFiles(String file1, String file2, String fileOut) throws FileNotFoundException, IOException {
try (PrintWriter pw = new PrintWriter(fileOut)) {
BufferedReader br = new BufferedReader(new FileReader(file1));
String line = br.readLine();
while (line != null) {
pw.println(line);
line = br.readLine();
}
br = new BufferedReader(new FileReader(file2));
line = br.readLine();
while (line != null) {
pw.println(line);
line = br.readLine();
}
pw.flush();
br.close();
}
}
/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
int input;
while (true) {
System.out.println("1) List the contents of a directory provided by the user "
+ "2) List the contents of a directory provided by the user as well as all of its sub directories. (hint: use recursion) "
+ "3) Locate a file with a given name. "
+ "4) Locate files with a given file extension "
+ "5) Concatenate the contents of 2 files whose names are provided by the user and output the result to a third file (name also provided by the user) "
+ "6) Exit");
System.out.println("Enter your choice:");
input = sc.nextInt();
switch (input) {
case 1:
System.out.println("Enter directory name: ");
listDirContents(sc.next());
break;
case 2:
System.out.println("Enter directory name: ");
listDirContentsRec(sc.next(), 0);
break;
case 3:
System.out.println("Enter directory to search: ");
String dirName = sc.next();
System.out.println("Enter file name: ");
String fileName = sc.next();
locateFile(dirName, fileName);
break;
case 4:
System.out.println("Enter directory to search: ");
String dir = sc.next();
System.out.println("Enter file extention: ");
String fileExt = sc.next();
locateFileWithExt(dir, fileExt);
break;
case 5:
System.out.println("Enter first file name: ");
String file1 = sc.next();
System.out.println("Enter second file name: ");
String file2 = sc.next();
System.out.println("Enter output file name: ");
String fileOut = sc.next();
concatenateFiles(file1, file2, fileOut);
break;
case 6:
System.exit(0);
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.