ADD: added initial working version

This commit is contained in:
2025-07-27 15:32:55 +02:00
parent 04e82ce1bd
commit a548a3f951
8 changed files with 123 additions and 34 deletions

View File

@@ -20,8 +20,9 @@ public class App extends Application {
scene = new Scene(loadFXML("primary"), 640, 480);
stage.setScene(scene);
stage.show();
System.out.println("help");
System.err.println("App started with scene: " + scene);
}
@@ -36,6 +37,7 @@ public class App extends Application {
public static void main(String[] args) {
launch();
}
}

View File

@@ -1,6 +1,8 @@
package com.example;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -36,24 +38,43 @@ public class FileScannerTask extends Task<Map<String, Integer>> {
* @throws Exception If an error occurs during the file scanning process.
*/
protected Map<String, Integer> call() throws Exception {
int filesAmount = countFilesInDirectory();
long filesAmount = countFilesInDirectory();
if (filesAmount == 0) {
updateProgress(filesAmount, filesAmount);
return fileTypeCounts; // Return empty map if no files found
}
java.io.File directory = new java.io.File(directoryPath);
if (directory.isDirectory()) {
java.io.File[] files = directory.listFiles();
if (files != null) {
int i = 0;
for (java.io.File file : files) {
if (file.isFile()) {
String fileType = getFileExtension(file);
fileTypeCounts.put(fileType, fileTypeCounts.getOrDefault(fileType, 0) + 1);
}
java.util.concurrent.atomic.AtomicInteger i = new java.util.concurrent.atomic.AtomicInteger(0);
updateProgress(i, filesAmount);
i++;
}
Files.walk(directory.toPath())
.filter(Files::isRegularFile)
.forEach(path -> {
try {
String fileType = getFileExtension(path.toFile());
java.nio.file.attribute.BasicFileAttributes attrs = Files.readAttributes(path, java.nio.file.attribute.BasicFileAttributes.class);
// You can now access metadata like attrs.size(), attrs.creationTime(), attrs.lastModifiedTime(), etc.
fileTypeCounts.put(fileType, fileTypeCounts.getOrDefault(fileType, 0) + 1);
updateProgress(i.get(), filesAmount);
i.incrementAndGet();
} catch (Exception e) {
e.printStackTrace();
}
});
// java.io.File[] files = directory.listFiles();
// if (files != null) {
// for (java.io.File file : files) {
// if (file.isFile()) {
// String fileType = getFileExtension(file);
// fileTypeCounts.put(fileType, fileTypeCounts.getOrDefault(fileType, 0) + 1);
// }
// updateProgress(i.get(), filesAmount);
// i.incrementAndGet();
// }
// }
}
return fileTypeCounts;
@@ -64,14 +85,25 @@ public class FileScannerTask extends Task<Map<String, Integer>> {
*
* @param directoryPath The path to the directory.
*/
public int countFilesInDirectory() {
public long countFilesInDirectory() {
java.io.File directory = new java.io.File(directoryPath);
if (directory.isDirectory()) {
java.io.File[] files = directory.listFiles();
if (files != null) {
return files.length;
long fileCount = 0;
try {
fileCount = Files.walk(Paths.get(directoryPath))
.filter(Files::isRegularFile)
.count();
System.out.println("Number of files in directory: " + fileCount);
return fileCount;
} catch (java.io.IOException e) {
e.printStackTrace();
}
// java.io.File[] files = directory.listFiles();
// if (files != null) {
// return files.length;
// }
}
return 0;
}

View File

@@ -34,6 +34,8 @@ public class FileSorterController {
@FXML
private void scan() throws IOException {
progressBar.setProgress(0);
errormessages.setText(""); // Clear previous error messages
filesDiagram.getData().clear(); // Clear previous data from the bar chart
// Check if the directory is valid
@@ -81,6 +83,12 @@ public class FileSorterController {
* @param fileTypeCounts A map where keys are file types and values are their respective counts.
*/
private void updateBarChart(Map<String, Integer> fileTypeCounts) {
if (fileTypeCounts == null || fileTypeCounts.isEmpty()) {
System.out.println("No files found or no file types to display.");
errormessages.setText("No files found or no file types to display.");
return;
}else{
fileTypeCounts.forEach((fileType, count) -> {
javafx.scene.chart.XYChart.Series<String, Number> series = new javafx.scene.chart.XYChart.Series<>();
series.setName(fileType);
@@ -88,6 +96,7 @@ public class FileSorterController {
filesDiagram.getData().add(series);
});
}
}
@FXML
@@ -135,9 +144,15 @@ public class FileSorterController {
filesamount.setText(String.valueOf(result.values().stream().mapToInt(Integer::intValue).sum()));
});
service.start();
// try {
// service.wait();
// } catch (InterruptedException e) {
// e.printStackTrace();
// errormessages.setText("Operation was interrupted.");
// }
}
progressBar.paddingProperty().unbind();
// progressBar.setProgress(0); // Reset progress bar before starting the service
progressBar.progressProperty().unbind();
progressBar.setProgress(0); // Reset progress bar before starting the service
final FileSorterService service = new FileSorterService();
service.setDirectoryPath(directory.getAbsolutePath());

View File

@@ -30,6 +30,30 @@ public class FileSorterTask extends Task<Integer> {
protected Integer call() throws Exception {
System.out.println("Scanning directory: " + directoryPath);
int filesAmount = FileScannerTask.countFilesInDirectory(directoryPath);
java.nio.file.Files.walk(java.nio.file.Paths.get(directoryPath))
.filter(java.nio.file.Files::isRegularFile)
.forEach(path -> {
try {
// System.out.println("Processing file: " + path);
String[] parts = path.toString().split(java.util.regex.Pattern.quote(java.io.File.separator));
java.nio.file.attribute.BasicFileAttributes attrs = java.nio.file.Files.readAttributes(path, java.nio.file.attribute.BasicFileAttributes.class);
// System.out.println(parts[(parts.length - 2)] + " - Creation Year: " + getCreationYear(path));
if (parts[(parts.length - 2)].equals(String.valueOf(getCreationYear(path)))) {
// System.out.println("Skipping file: " + path + " due to invalid year");
return; // Skip files with invalid year
}else {
System.out.println("Processing file: " + path + " with valid year");
}
// You can now access metadata like attrs.size(), attrs.creationTime(), attrs.lastModifiedTime(), etc.
// Here you can add logic to handle file types if needed
} catch (Exception e) {
e.printStackTrace();
}
});
// Use a Set to track which year folders have already been created
AtomicInteger i = new AtomicInteger(0);
java.util.Set<String> createdFolders = new java.util.HashSet<>();
@@ -39,11 +63,8 @@ public class FileSorterTask extends Task<Integer> {
try {
java.nio.file.attribute.BasicFileAttributes attrs = java.nio.file.Files.readAttributes(path, java.nio.file.attribute.BasicFileAttributes.class);
// Use the earlier of creationTime and lastModifiedTime
java.time.Instant creationInstant = attrs.creationTime().toInstant();
java.time.Instant modifiedInstant = attrs.lastModifiedTime().toInstant();
java.time.Instant instant = creationInstant.isBefore(modifiedInstant) ? creationInstant : modifiedInstant;
java.time.ZonedDateTime zdt = java.time.ZonedDateTime.ofInstant(instant, java.time.ZoneId.systemDefault());
String year = String.valueOf(zdt.getYear());
String year = String.valueOf(getCreationYear(path));
java.nio.file.Path targetDir = java.nio.file.Paths.get(directoryPath, year);
// Only create the directory if it hasn't been created yet
@@ -52,7 +73,7 @@ public class FileSorterTask extends Task<Integer> {
}
java.nio.file.Path targetPath = targetDir.resolve(path.getFileName());
java.nio.file.Files.copy(path, targetPath, java.nio.file.StandardCopyOption.REPLACE_EXISTING);
java.nio.file.Files.move(path, targetPath, new java.nio.file.CopyOption[0]);
updateProgress(i.get(), filesAmount);
i.incrementAndGet();
} catch (Exception e) {
@@ -62,6 +83,25 @@ public class FileSorterTask extends Task<Integer> {
// Implementation of file scanning logic goes here
return 0; // Placeholder return value
}
/**
* Gets the creation year of a file based on its attributes.
*
* @param path The path to the file.
* @return The year of creation or -1 if an error occurs.
*/
private int getCreationYear(java.nio.file.Path path) {
try {
java.nio.file.attribute.BasicFileAttributes attrs = java.nio.file.Files.readAttributes(path, java.nio.file.attribute.BasicFileAttributes.class);
java.time.Instant creationInstant = attrs.creationTime().toInstant();
java.time.Instant modifiedInstant = attrs.lastModifiedTime().toInstant();
java.time.Instant instant = creationInstant.isBefore(modifiedInstant) ? creationInstant : modifiedInstant;
java.time.ZonedDateTime zdt = java.time.ZonedDateTime.ofInstant(instant, java.time.ZoneId.systemDefault());
return zdt.getYear();
} catch (java.io.IOException e) {
e.printStackTrace();
return -1; // Return an invalid year if an error occurs
}
}
}

Binary file not shown.