Skip to content
Permalink
Browse files
Add files via upload
  • Loading branch information
silves10 committed May 20, 2020
0 parents commit 83e004f2e94679716bc8add2d4a088b86643cd28
Show file tree
Hide file tree
Showing 24 changed files with 801 additions and 0 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,16 @@



H:\

Programs




Images



Games

@@ -0,0 +1,20 @@
C:\
Documents
Images
Image1.jpg
Image2.jpg
Image3.png

Works
Letter.doc
Accountant
Accounting.xls
AnnualReport.xls
Program Files
Skype
Skype.exe
Readme.txt
Mysql
Mysql.exe
Mysql.com

Binary file not shown.
@@ -0,0 +1,173 @@
package test1;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class FileSystemController {

private ArrayList<String> ListOfPaths = new ArrayList<String>();
private FileSystemDatabase FileSystemDatabase;

public FileSystemController()
{
FileSystemDatabase = new FileSystemDatabase();
//empties the FileSystem table on start up, only for testing purposes
FileSystemDatabase.DeleteFileSystem();
}

public static int NumberOfTabs(String line)
{
int tabs = (int)line.chars().filter(c -> c == (int)'\t').count();
return tabs;
}

public static String RemoveTabs(String line)
{
String lineContents = line.replace("\t", "");
return lineContents;
}

/* Checks if the string supplied contains a "."
* Not a perfect solution as directories can contain "."
* A better method would be to compare the end of the files with known file extensions
* */
public static boolean IsFile(String line)
{
if (line.contains("."))
{
return true;
}
return false;
}

/* Takes a file as input and calls the recursive function ExtractFileSystemStructure on that file*/
public void LoadFileSystem(File file) throws IOException
{
try
{
FileReader fr = new FileReader(file);
BufferedReader buffer = new BufferedReader(fr);
String path = "";
ExtractFileSystemStructure(buffer, path, path, -1);
fr.close();
System.out.println("ListOfPaths: " + ListOfPaths);
}
catch(FileNotFoundException e)
{
e.printStackTrace();
return;
}
}

public ArrayList<String> SearchFileSystem(String filter)
{
return FileSystemDatabase.SearchFileSystem(filter);
}

public void SaveCurrentFileSystem()
{
FileSystemDatabase.SaveFileSystem(ListOfPaths);
}

/* Recursive function that adds all paths found in a file system to the ListOfPaths list */
private boolean ExtractFileSystemStructure(BufferedReader buffer, String parentDirectoryPath, String currentPath, int previousLineDepth) throws IOException
{
// Marks the current position of the buffered reader
buffer.mark(1);
String line = buffer.readLine();

// BASE CASE //
//If we reach the end of the file, stop
if (line == null)
{
return false;
}

// EDGE CASE //
//If we read a blank line, skip this line
if (RemoveTabs(line).length() == 0)
{
return ExtractFileSystemStructure(buffer, parentDirectoryPath, currentPath, previousLineDepth);
}


//If we read a line whose number of tabs is less than the previous line, we stop and go back one line
if (NumberOfTabs(line) < previousLineDepth)
{
buffer.reset();
return true;
}

//If we read a line whose number of tabs is greater than the previous line, then we need to include the current file/directory to the current path
if (NumberOfTabs(line) > previousLineDepth)
{
if (IsFile(line))
{
ListOfPaths.add(CreatePath(line, currentPath));
return ExtractFileSystemStructure(buffer, currentPath, parentDirectoryPath, NumberOfTabs(line));
}
else
{
String newPath = CreatePath(line, currentPath);
ListOfPaths.add(newPath);
//function will return true, if there is a directory/file outside of the current directory
//only returns false when the end of the file is reached
if(ExtractFileSystemStructure(buffer, currentPath, newPath, NumberOfTabs(line)))
{
return ExtractFileSystemStructure(buffer, currentPath, newPath, NumberOfTabs(line));
}
return false;
}
}

//If we read a line whose number of tabs is equal to the previous line, then we need don't need to include the entire current path, only the parent directories path
if (NumberOfTabs(line) == previousLineDepth)
{
if (IsFile(line))
{
ListOfPaths.add(CreatePath(line, parentDirectoryPath));
return ExtractFileSystemStructure(buffer, parentDirectoryPath, currentPath, NumberOfTabs(line));
}
else
{
String newPath = CreatePath(line, parentDirectoryPath);
ListOfPaths.add(newPath);
//function will return true, if there is a directory/file outside of the current directory

if(ExtractFileSystemStructure(buffer, parentDirectoryPath, newPath, NumberOfTabs(line)))
{
return ExtractFileSystemStructure(buffer, parentDirectoryPath, newPath, NumberOfTabs(line));
}
return false;
}
}

return false;
}

/* Creates a path based on the current line and path supplied */
private String CreatePath(String line, String path)
{
String newPath;
//this stops an extra \ from appearing if we are at the base of the file system
if (ListOfPaths.size() == 0 || ListOfPaths.size() == 1 || ListOfPaths.get(0) == path)
{
newPath = path + RemoveTabs(line);
}
else
{
newPath = path + "\\" + RemoveTabs(line);
}
return newPath;
}

public ArrayList<String> GetListOfPaths()
{
return ListOfPaths;
}

}
@@ -0,0 +1,107 @@
package test1;
import java.sql.*;
import java.util.ArrayList;

public class FileSystemDatabase {

private Connection conn;

public FileSystemDatabase()
{
try
{
System.out.println("Attempting to connect to database...");
//Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Test1Database", "root", "root");

if (conn !=null)
{
System.out.println("Success");
}
else
{
System.out.println("Failure");
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}

public ArrayList<String> SelectAll()
{
try {
Statement st = conn.createStatement();
String sql;
sql = "SELECT * FROM FileSystem";
ResultSet rs = st.executeQuery(sql);
ArrayList<String> results = new ArrayList<String>();
while(rs.next())
{
results.add(rs.getString("path"));
}
return results;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

public void SaveFileSystem(ArrayList<String> ListOfPaths)
{
try {
String sql;
PreparedStatement preparedStmt;
for (String path : ListOfPaths)
{
sql = "INSERT INTO FileSystem (path) VALUES (?)";
preparedStmt = conn.prepareStatement(sql);
preparedStmt.setString (1, path);
preparedStmt.execute();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/* Empties the FileSystem table */
public void DeleteFileSystem()
{
Statement st;
try {
st = conn.createStatement();
String sql = "TRUNCATE FileSystem";
st.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

/* Returns the paths of any rows that contain the filter supplied */
public ArrayList<String> SearchFileSystem(String filter)
{
Statement st;
try {
st = conn.createStatement();
String sql = "SELECT path FROM FileSystem WHERE path LIKE '%" + filter+ "%'";
ResultSet rs = st.executeQuery(sql);
ArrayList<String> results = new ArrayList<String>();
while(rs.next())
{
results.add(rs.getString("path"));
}
return results;

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

}

0 comments on commit 83e004f

Please sign in to comment.