Skip to content

Instantly share code, notes, and snippets.

@jacks205
Last active December 17, 2015 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacks205/5573847 to your computer and use it in GitHub Desktop.
Save jacks205/5573847 to your computer and use it in GitHub Desktop.
This is a File Browser program that can be used to locate, append, and display files, also has an option of creating a log file that updates and prints everything that is shown to the user. Option (4) is the one thing I can't get working at the moment but it doesn't crash the program.
import java.util.Scanner;
import java.io.*;
public class FileBrowser{
private static String logName;
public FileBrowser(){
logFile();
run();
}
public static void print(String s){
updateLog(s);
System.out.println(s);
}
// public void listOptions(){ wanted to make this a seperate method but had to make run() a static method and couldnt have this as a non-static
// print("File Browser 9000");
// print(" ");
// print("(1) List the contents of a directory.");
// print("(2) List the contents and sub directories of a directory.");
// print("(3) Locate a file with a given name.");
// print("(4) Locate files with a given file extension.");
// print("(5) Concatenate the contents of 2 files and output the result to a third file.");
// print("(6) Exit");
// }
public static void run(){
Scanner input = new Scanner(System.in);
print("File Browser 9000");
print(" ");
print("(1) List the contents of a directory.");
print("(2) List the contents and sub directories of a directory.");
print("(3) Locate a file with a given name.");
print("(4) Locate files with a given file extension.");
print("(5) Concatenate the contents of 2 files and output the result to a third file.");
print("(6) Exit");
String answer = input.nextLine();
updateLog(answer);
if(answer.equals("1")){
ListContents listContents = new ListContents(1);
}
else if (answer.equals("2")){
ListContents listContents = new ListContents(2);
}
else if(answer.equals("3")){
LocateFile locateFile = new LocateFile(3);
}
else if(answer.equals("4")){
LocateFile locateFile = new LocateFile(4);
}
else if(answer.equals("5")){
LocateFile locateFile = new LocateFile(5);
}
else if (answer.equals("6")){
System.exit(1);//quits program
}
}
public void logFile(){
Scanner input = new Scanner(System.in);
System.out.println("Would you like to make a log[Yes/No]: ");
if(input.nextLine().equalsIgnoreCase("yes")){
System.out.println("Name the log file(ex: log.txt): ");
logName = input.nextLine();
try{
PrintWriter pw = new PrintWriter(new FileOutputStream(logName));
pw.close();
}
catch(Exception ex){
System.out.println("Exception " + ex.getMessage());
}
}
}
public static void updateLog(String records){ //updates log, static to use in other classes to update and not create an object each time
if(logName == null){
return;
}
try{
PrintWriter pw = new PrintWriter(new FileOutputStream(logName, true));
pw.println(records);
pw.close();
}
catch(Exception ex){
System.out.println("Exception " + ex.getMessage());
}
}
public static void main(String[] args){
FileBrowser fileBrowser = new FileBrowser();
}
}
import java.util.Scanner;
import java.io.*;
public class ListContents{
public ListContents(int i){
Scanner input = new Scanner(System.in);
File directory;
while(true){//repeats incase user enters a directory that doesnt exist
FileBrowser.print("Enter Directory: ");
String answer = input.nextLine();
FileBrowser.updateLog(answer);
directory = new File(answer);
if(directory.listFiles() != null){
break;
}
}
if(i == 1){ //shows files only
try{
//FileBrowser.print("Before showDirectory"); used to check for errors
showDirectory(directory.listFiles());//array of Files sent to method
}
catch(Exception ex){
FileBrowser.print("Exception " + ex.getMessage());
}
}else if(i == 2){//shows files and subdirectories
try{
printTree(directory);//array of Files sent to method
}
catch(Exception ex){
FileBrowser.print("Exception " + ex.getMessage());
}
}
FileBrowser.run();
}
public void showDirectory(File[] files){ //prints directory
//FileBrowser.print("showDirectory");used to check for errors
try{
for(int i = 0; i < files.length; ++i){
FileBrowser.print(files[i].getName());
}
System.out.println();
}
// catch(FileNotFound fx){
// FileBrowser.print(fx.getMessage());
// }
catch(Exception ex){
FileBrowser.print("Exception " + ex.getMessage());
}
FileBrowser.print(" ");
}
public void printTree(File f){//using recursion for printing out list of files and the subdirectories
//FileBrowser.print("printTree");used to check for errors
File[] theFiles = f.listFiles();
for(int i = 0; i < theFiles.length; ++i){
if(!(theFiles[i].isDirectory())){
try{
FileBrowser.print(theFiles[i].getName());
}
catch(Exception ex){
FileBrowser.print("Exception " + ex.getMessage());
}
}
else{
try{
FileBrowser.print(theFiles[i].getName());
printTree(theFiles[i]);
}
catch(Exception ex){
FileBrowser.print("Exception " + ex.getMessage());
}
}
}
FileBrowser.print(" ");
}
}
import java.util.Scanner;
import java.io.*;
public class LocateFile{
private boolean fileFound;
private String fileExtension;
public LocateFile(int i){
this.fileFound = false;
if(i == 3){
FileBrowser.print("Enter file name: ");
searchComputer(false);
}
else if(i == 4){
FileBrowser.print("Enter file extention (ex: txt\\java\\jpg): ");
searchComputer(true);
}
else if(i == 5){
concatenateFiles();
}
FileBrowser.run();
}
// public void searchFile(){
// Scanner input = new Scanner(System.in);
// FileBrowser.print("Enter file name: ");
// String fileName = input.nextLine();
// try{
// }
// catch(Exception ex){
// FileBrowser.print(ex.getMessage());
// }
// }
public void searchComputer(boolean filter){
Scanner input = new Scanner(System.in);
this.fileExtension = input.nextLine();
FileBrowser.print("Searching...");
FileBrowser.print(" ");
searchDirectory(this.fileExtension, (new File("\\")), filter);
}
public void searchDirectory(String fileName, File f, boolean extention){
File[] theFiles = f.listFiles();
if(!this.fileFound){
for(int i = 0; i < theFiles.length; ++i){
if(!extention){
if(theFiles[i].getName().equals(fileName)){
try{
this.fileFound = true;
FileBrowser.print(theFiles[i].getPath());
}
catch(Exception ex){
// FileBrowser.print(("Exception: " + ex.getMessage()));
}
}
}else{
String chars = theFiles[i].getName();
if(theFiles[i].getName().substring(theFiles[i].getName().length() - chars.length(), theFiles[i].getName().length()).equals(fileName) && !theFiles[i].isDirectory()){
try{
FileBrowser.print(theFiles[i].getName());
FileBrowser.print(theFiles[i].getPath());
}
catch(Exception ex){
// FileBrowser.print("Exception: " + ex.getMessage());
}
}
}
}
for (int i = 0; i < theFiles.length; ++i) {
if(theFiles[i].isDirectory() && !this.fileFound || extention){ //goes to next directory to search for file
try{
searchDirectory(fileName, theFiles[i], extention);
}
catch(Exception ex){
// FileBrowser.print("Exception: " + ex.getMessage());
}
}
}
}
}
public void concatenateFiles(){
Scanner input = new Scanner(System.in);
FileBrowser.print("Enter Full Pathnames that are given in option (4)");
FileBrowser.print("Path of 1st File: ");
String firstFile = input.nextLine();
FileBrowser.print("Path of 2nd File: ");
String secondFile = input.nextLine();
String temp1 = " ";
String temp2 = " ";
try{
Scanner sc1 = new Scanner(new FileInputStream(firstFile));
while(sc1.hasNextLine()){
temp1 += sc1.nextLine();
}
sc1.close();
Scanner sc2 = new Scanner(new FileInputStream(secondFile));
while(sc2.hasNextLine()){
temp2 += sc2.nextLine();
}
sc2.close();
}
catch(Exception fx){
FileBrowser.print(fx.getMessage());
}
FileBrowser.print("Name of new file?(Add \".txt\" extension): ");
String newFile = input.nextLine();
try{
PrintWriter pw = new PrintWriter(new FileOutputStream(newFile));
pw.write(temp1 + " " + temp2);
pw.close();
}
catch(Exception fx){
FileBrowser.print(fx.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment