Ejemplos v2

This commit is contained in:
2024-05-12 17:23:06 +02:00
commit eae1c73134
8 changed files with 208 additions and 0 deletions

90
.gitignore vendored Normal file
View File

@@ -0,0 +1,90 @@
##############################
## Java
##############################
.mtj.tmp/
*.class
*.jar
*.war
*.ear
*.nar
hs_err_pid*
replay_pid*
##############################
## Maven
##############################
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
pom.xml.bak
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar
##############################
## Gradle
##############################
bin/
build/
.gradle
.gradletasknamecache
gradle-app.setting
!gradle-wrapper.jar
##############################
## IntelliJ
##############################
out/
.idea/
.idea_modules/
*.iml
*.ipr
*.iws
##############################
## Eclipse
##############################
.settings/
bin/
tmp/
.metadata
.classpath
.project
*.tmp
*.bak
*.swp
*~.nib
local.properties
.loadpath
.factorypath
##############################
## NetBeans
##############################
nbproject/private/
build/
nbbuild/
dist/
nbdist/
nbactions.xml
nb-configuration.xml
##############################
## Visual Studio Code
##############################
.vscode/
.code-workspace
##############################
## OS X
##############################
.DS_Store
##############################
## Miscellaneous
##############################
*.log

0
README.md Normal file
View File

16
pom.xml Normal file
View File

@@ -0,0 +1,16 @@
<?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>iesthiar</groupId>
<artifactId>t12ficheros-ejemplos</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
</project>

View File

@@ -0,0 +1,26 @@
package iesthiar;
import java.io.File;
public class _231_PruebasFicheros {
public static void main(String[] args) {
// Dos rutas absolutas
File carpetaAbs = new File("/home/lionel/fotos");
File archivoAbs = new File("/home/lionel/fotos/albania1.jpg");
// Dos rutas relativas
File carpetaRel = new File("trabajos");
File archivoRel = new File("trabajos/documento.txt");
// Mostrem sus rutas
mostrarRutas(carpetaAbs);
mostrarRutas(archivoAbs);
mostrarRutas(carpetaRel);
mostrarRutas(archivoRel);
}
public static void mostrarRutas(File f) {
System.out.println("getParent() : " + f.getParent());
System.out.println("getName() : " + f.getName());
System.out.println("getAbsolutePath(): " + f.getAbsolutePath() + "\n");
}
}

View File

@@ -0,0 +1,19 @@
package iesthiar;
import java.io.File;
public class _232_PruebaEstado {
public static void main(String[] args) {
File temp = new File("C:/Temp");
File fotos = new File("C:/Temp/Fotos");
File document = new File("C:/Temp/Documento.txt");
System.out.println(temp.getAbsolutePath() + " ¿existe? " + temp.exists());
mostrarEstado(fotos);
mostrarEstado(document);
}
public static void mostrarEstado(File f) {
System.out.println(f.getAbsolutePath() + " ¿archivo? " + f.isFile());
System.out.println(f.getAbsolutePath() + " ¿carpeta? " + f.isDirectory());
}
}

View File

@@ -0,0 +1,16 @@
package iesthiar;
import java.io.File;
import java.util.Date;
public class _233_PruebaPropiedades {
public static void main(String[] args) {
File documento = new File("C:/Temp/Documento.txt");
System.out.println(documento.getAbsolutePath());
long milisegundos = documento.lastModified();
Date fecha = new Date(milisegundos);
System.out.println("Última modificación (ms): " + milisegundos);
System.out.println("Última modificación (fecha): " + fecha);
System.out.println("Tamaño del archivo: " + documento.length());
}
}

View File

@@ -0,0 +1,20 @@
package iesthiar;
import java.io.File;
public class _234_PruebaGestion {
public static void main(String[] args) {
File fotos = new File("C:/Temp/Fotos");
File doc = new File("C:/Temp/Documento.txt");
boolean mkdirFot = fotos.mkdir();
if (mkdirFot) {
System.out.println("Creada carpeta " + fotos.getName() + "? " + mkdirFot);
} else {
boolean delCa = fotos.delete();
System.out.println("Borrada carpeta " + fotos.getName() + "? " + delCa);
boolean delAr = doc.delete();
System.out.println("Borrado archivo " + doc.getName() + "? " + delAr);
}
}
}

View File

@@ -0,0 +1,21 @@
package iesthiar;
import java.io.File;
public class _235_PruebaListado {
public static void main(String[] args) {
File dir = new File(".");
File[] lista = dir.listFiles();
System.out.println("Contenido de " + dir.getAbsolutePath() + " :");
// Recorremos el array y mostramos el nombre de cada elemento
for (int i = 0; i < lista.length; i++) {
File f = lista[i];
if (f.isDirectory()) {
System.out.println("[DIR] " + f.getName());
} else {
System.out.println("[FIC] " + f.getName());
}
}
}
}