Skip to content

Commit

Permalink
Merge pull request #82 from studas/lista09/ex01ex02
Browse files Browse the repository at this point in the history
Lista09/ex01ex02
  • Loading branch information
cirillom authored Dec 6, 2022
2 parents 83a2490 + 31256ab commit 6c50fdc
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ hs_err_pid*
/lista07/EX01/build/
/lista06/ex05/dist/
/lista09/ex04/target/
/lista09/ex01ex02/target/
14 changes: 14 additions & 0 deletions lista09/ex01ex02/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.usp</groupId>
<artifactId>ex01ex02</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<exec.mainClass>com.usp.ex01ex02.Ex01ex02</exec.mainClass>
</properties>
</project>
73 changes: 73 additions & 0 deletions lista09/ex01ex02/src/main/java/com/usp/ex01ex02/Ex01ex02.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.usp.ex01ex02;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;

/**
*
* @author cillor
*/
public class Ex01ex02 {

public static void main(String[] args) {
String path = "/home/cillor/OneDriv/Documentos/Comprovantes/BoletosAp";
String filter = ".pdf";

System.out.println("Scanning with no filter:");
scanFiles(path);

System.out.println("\n\nScanning with filter:");
scanFiles(path, filter);
}

static void scanFiles(String sAFilePath) {
File fonte = new File(sAFilePath);
if (fonte.isDirectory()) {
File[] files = fonte.listFiles();
if (files != null) {
for (File file : files) {
try {
if (file.isDirectory())
scanFiles(file.getCanonicalPath());
else
System.out.println(file.getName());
} catch (IOException ex) {
System.out.println("Erro:" + ex.getMessage());
}
}
}
}
}

static void scanFiles(String sAFilePath, String type) {

File fonte = new File(sAFilePath);
if (fonte.isDirectory()) {

FilenameFilter textFilter = (File dir, String name) -> {

String path = dir.getAbsolutePath() + "/" + name;
File file = new File(path);
if (file.isDirectory()) {
return true; //filtra dentro os diretorios, permitindo a recursão por todo mundo
}

return name.toLowerCase().endsWith(type); //filtra fora os diretorios
};

File[] files = fonte.listFiles(textFilter);

for (File file : files) {
try {
if (file.isDirectory())
scanFiles(file.getCanonicalPath(), type);
else
System.out.println(file.getName());
} catch (IOException ex) {
System.out.println("Erro:" + ex.getMessage());
}
}
}
}
}

0 comments on commit 6c50fdc

Please sign in to comment.