Skip to content

Commit

Permalink
feat(file-io): 🎸 files, translated
Browse files Browse the repository at this point in the history
Refers: #9
  • Loading branch information
rcmoutinho committed Sep 13, 2019
1 parent 3ef7e75 commit fea3267
Show file tree
Hide file tree
Showing 17 changed files with 251 additions and 264 deletions.
243 changes: 121 additions & 122 deletions book/07-file-io/sections/02-files.asc

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions src/org/j6toj8/fileio/files/Files_BasicFileAttributeView.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ public static void main(String[] args) {
String userHome = System.getProperty("user.home");
System.out.println("User home: " + userHome);

Path path = Paths.get(userHome, "arquivo.txt");
Path path = Paths.get(userHome, "file.txt");

try {
BasicFileAttributeView attributesView = Files.getFileAttributeView(path, BasicFileAttributeView.class);
BasicFileAttributes attributesAntigos = attributesView.readAttributes();
BasicFileAttributes oldAttributes = attributesView.readAttributes();

System.out.println("\nData de Criação original: " + attributesAntigos.creationTime());
System.out.println("Último acesso original: " + attributesAntigos.lastAccessTime());
System.out.println("Última modificação original: " + attributesAntigos.lastModifiedTime());
System.out.println("\nOriginal creation date: " + oldAttributes.creationTime());
System.out.println("Original last access: " + oldAttributes.lastAccessTime());
System.out.println("Original last modified: " + oldAttributes.lastModifiedTime());

FileTime fileTime = FileTime.from(Instant.now().plusMillis(10000));
attributesView.setTimes(fileTime, fileTime, fileTime);

BasicFileAttributes attributesNovos = attributesView.readAttributes();
System.out.println("\nData de Criação alterada: " + attributesNovos.creationTime());
System.out.println("Último acesso alterada: " + attributesNovos.lastAccessTime());
System.out.println("Última modificação alterada: " + attributesNovos.lastModifiedTime());
BasicFileAttributes newAttributes = attributesView.readAttributes();
System.out.println("\nCreation date changed: " + newAttributes.creationTime());
System.out.println("Last access changed: " + newAttributes.lastAccessTime());
System.out.println("Last modified changed: " + newAttributes.lastModifiedTime());
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
18 changes: 9 additions & 9 deletions src/org/j6toj8/fileio/files/Files_BasicFileAttributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ public static void main(String[] args) {
String userHome = System.getProperty("user.home");
System.out.println("User home: " + userHome);

Path path = Paths.get(userHome, "arquivo.txt");
Path path = Paths.get(userHome, "file.txt");

try {
BasicFileAttributes attributes = Files.readAttributes(path, BasicFileAttributes.class);
System.out.println("Tamanho: " + attributes.size());
System.out.println("Size: " + attributes.size());

System.out.println("É diretório? " + attributes.isDirectory());
System.out.println("É link simbólico? " + attributes.isSymbolicLink());
System.out.println("É um arquivo comum? " + attributes.isRegularFile());
System.out.println("Não é nenhuma das opções acima? " + attributes.isOther());
System.out.println("Is it directory? " + attributes.isDirectory());
System.out.println("Is it symbolic link? " + attributes.isSymbolicLink());
System.out.println("Is it a common file? " + attributes.isRegularFile());
System.out.println("Not any of the above? " + attributes.isOther());

System.out.println("Data de Criação: " + attributes.creationTime());
System.out.println("Último acesso: " + attributes.lastAccessTime());
System.out.println("Última modificação: " + attributes.lastModifiedTime());
System.out.println("Creation date: " + attributes.creationTime());
System.out.println("Last acess: " + attributes.lastAccessTime());
System.out.println("Last Modified: " + attributes.lastModifiedTime());
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
36 changes: 18 additions & 18 deletions src/org/j6toj8/fileio/files/Files_Checks.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,44 @@ public static void main(String[] args) {
String userHome = System.getProperty("user.home");
System.out.println("User home: " + userHome);

Path path1 = Paths.get(userHome, "arquivo1.txt");
Path path2 = Paths.get(userHome, "arquivos");
Path path1 = Paths.get(userHome, "file1.txt");
Path path2 = Paths.get(userHome, "files");

System.out.println("Path 1: " + path1);
System.out.println("Path 2: " + path2);

System.out.println("Path 1 existe? " + Files.exists(path1));
System.out.println("Path 2 existe? " + Files.exists(path2));
System.out.println("Path 1 exist? " + Files.exists(path1));
System.out.println("Path 2 exist? " + Files.exists(path2));

System.out.println("Path 1 NÃO existe? " + Files.notExists(path1));
System.out.println("Path 2 NÃO existe? " + Files.notExists(path2));
System.out.println("Path 1 does NOT exist? " + Files.notExists(path1));
System.out.println("Path 2 does NOT exist? " + Files.notExists(path2));

System.out.println("Path 1 é um diretório? " + Files.isDirectory(path1));
System.out.println("Path 1 é um arquivo comum? " + Files.isRegularFile(path1));
System.out.println("Path 1 é um link simbólico? " + Files.isSymbolicLink(path1));
System.out.println("Is Path 1 a directory? " + Files.isDirectory(path1));
System.out.println("Is Path 1 a common file? " + Files.isRegularFile(path1));
System.out.println("Is Path 1 a symbolic link? " + Files.isSymbolicLink(path1));

System.out.println("Aplicação possui permissão de leitura no Path 1? " + Files.isReadable(path1));
System.out.println("Aplicação possui permissão de escrita no Path 1? " + Files.isWritable(path1));
System.out.println("Aplicação possui permissão de execução no Path 1? " + Files.isExecutable(path1));
System.out.println("Application has read permission on Path 1? " + Files.isReadable(path1));
System.out.println("Application has write permission on Path 1? " + Files.isWritable(path1));
System.out.println("Application has execution permission on Path 1? " + Files.isExecutable(path1));

try {
System.out.println("Qual o tamanho de Path 1? " + Files.size(path1));
System.out.println("How big is Path 1? " + Files.size(path1));
} catch (IOException e1) {
// Lança exceção se o arquivo não existir
// Throw exception if file does not exist
e1.printStackTrace();
}

try {
System.out.println("Path 1 é oculto? " + Files.isHidden(path1));
System.out.println("Is Path 1 hidden? " + Files.isHidden(path1));
} catch (IOException e) {
// Lança exceção se o arquivo não existir
// Throw exception if file does not exist
e.printStackTrace();
}

try {
System.out.println("Path 1 e Path 1 são iguais? " + Files.isSameFile(path1, path1));
System.out.println("Are Path 1 and Path 1 the same? " + Files.isSameFile(path1, path1));
} catch (IOException e) {
// Lança exceção se o arquivo não existir
// Throw exception if file does not exist
e.printStackTrace();
}

Expand Down
17 changes: 8 additions & 9 deletions src/org/j6toj8/fileio/files/Files_CopyFromPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@ public static void main(String[] args) {
String userHome = System.getProperty("user.home");
System.out.println("User home: " + userHome);

// Utilizando um nome aleatório de arquivo,
// apenas para o exemplo executar inúmeras vezes sem problemas
String nomeAleatorio = "copia" + new Random().nextInt() + ".txt";
// Using a random file name, just for example to run countless times without problems
String randomName = "copy" + new Random().nextInt() + ".txt";

try (FileOutputStream fos = new FileOutputStream(userHome + File.separator + nomeAleatorio)) {
Path pathParaCopia = Paths.get(userHome, "arquivo1.txt");
Files.copy(pathParaCopia, fos);
try (FileOutputStream fos = new FileOutputStream(userHome + File.separator + randomName)) {
Path pathToCopy = Paths.get(userHome, "file1.txt");
Files.copy(pathToCopy, fos);

Path pathCriado = Paths.get(userHome, nomeAleatorio);
System.out.println("Path criado: " + pathCriado);
System.out.println("Path criado existe?: " + Files.exists(pathCriado));
Path createdPath = Paths.get(userHome, randomName);
System.out.println("Created Path: " + createdPath);
System.out.println("Created Path exist?: " + Files.exists(createdPath));
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
17 changes: 8 additions & 9 deletions src/org/j6toj8/fileio/files/Files_CopyPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,21 @@ public static void main(String[] args) {
String userHome = System.getProperty("user.home");
System.out.println("User home: " + userHome);

// Utilizando um nome aleatório de arquivo,
// apenas para o exemplo executar inúmeras vezes sem problemas
String nomeAleatorio = "arquivo" + new Random().nextInt() + ".txt";
// Using a random file name, just for example to run countless times without problems
String randomName = "file" + new Random().nextInt() + ".txt";

Path path1 = Paths.get(userHome, nomeAleatorio);
Path path2 = Paths.get(userHome, nomeAleatorio + "-copia.txt");
Path path1 = Paths.get(userHome, randomName);
Path path2 = Paths.get(userHome, randomName + "-copy.txt");
System.out.println("Path 1: " + path1);
System.out.println("Path 2: " + path2);

try {
System.out.println("Path 1 existe? " + Files.exists(path1));
System.out.println("Path 1 exist? " + Files.exists(path1));
Files.createFile(path1);
System.out.println("Path 1 existe? " + Files.exists(path1));
System.out.println("Path 2 existe? " + Files.exists(path2));
System.out.println("Path 1 exist? " + Files.exists(path1));
System.out.println("Path 2 exist? " + Files.exists(path2));
Files.copy(path1, path2);
System.out.println("Path 2 existe? " + Files.exists(path2));
System.out.println("Path 2 exist? " + Files.exists(path2));
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
15 changes: 7 additions & 8 deletions src/org/j6toj8/fileio/files/Files_CopyToPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ public static void main(String[] args) {
String userHome = System.getProperty("user.home");
System.out.println("User home: " + userHome);

// Utilizando um nome aleatório de arquivo,
// apenas para o exemplo executar inúmeras vezes sem problemas
String nomeAleatorio = "copia" + new Random().nextInt() + ".txt";
// Using a random file name, just for example to run countless times without problems
String randomName = "copy" + new Random().nextInt() + ".txt";

try (FileInputStream fis = new FileInputStream(userHome + File.separator + "arquivo1.txt")) {
Path pathParaCopia = Paths.get(userHome, nomeAleatorio);
System.out.println("Path 2 existe? " + Files.exists(pathParaCopia));
Files.copy(fis, pathParaCopia);
System.out.println("Path 2 existe? " + Files.exists(pathParaCopia));
try (FileInputStream fis = new FileInputStream(userHome + File.separator + "file1.txt")) {
Path pathToCopy = Paths.get(userHome, randomName);
System.out.println("Path 2 exist? " + Files.exists(pathToCopy));
Files.copy(fis, pathToCopy);
System.out.println("Path 2 exist? " + Files.exists(pathToCopy));
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
17 changes: 8 additions & 9 deletions src/org/j6toj8/fileio/files/Files_CreateDirectories.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,24 @@ public static void main(String[] args) {
String userHome = System.getProperty("user.home");
System.out.println("User home: " + userHome);

// Utilizando um nome aleatório de diretório,
// apenas para o exemplo executar inúmeras vezes sem problemas
String nomeAleatorio1 = "arquivo" + new Random().nextInt();
String nomeAleatorio2 = "arquivo" + new Random().nextInt();
String nomeAleatorio3 = "arquivo" + new Random().nextInt();
// Using a random directory name, just for example to run countless times without problems
String randomName1 = "file" + new Random().nextInt();
String randomName2 = "file" + new Random().nextInt();
String randomName3 = "file" + new Random().nextInt();

Path path = Paths.get(userHome, nomeAleatorio1, nomeAleatorio2, nomeAleatorio3);
Path path = Paths.get(userHome, randomName1, randomName2, randomName3);
System.out.println("Path: " + path);

try {
Files.createDirectory(path); // MÉTODO ERRADO, LANÇA EXCEÇÃO
Files.createDirectory(path); // WRONG METHOD, THROWS EXCEPTION
} catch (IOException e) {
e.printStackTrace();
}

try {
System.out.println("Path existe? " + Files.exists(path));
System.out.println("Path exist? " + Files.exists(path));
Files.createDirectories(path);
System.out.println("Path existe? " + Files.exists(path));
System.out.println("Path exist? " + Files.exists(path));
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
11 changes: 5 additions & 6 deletions src/org/j6toj8/fileio/files/Files_CreateDirectory.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@ public static void main(String[] args) {
String userHome = System.getProperty("user.home");
System.out.println("User home: " + userHome);

// Utilizando um nome aleatório de diretório,
// apenas para o exemplo executar inúmeras vezes sem problemas
String nomeAleatorio = "arquivo" + new Random().nextInt();
// Using a random directory name, just for example to run countless times without problems
String randomName = "file" + new Random().nextInt();

Path path = Paths.get(userHome, nomeAleatorio);
Path path = Paths.get(userHome, randomName);
System.out.println("Path: " + path);

try {
System.out.println("Path existe? " + Files.exists(path));
System.out.println("Path exist? " + Files.exists(path));
Files.createDirectory(path);
System.out.println("Path existe? " + Files.exists(path));
System.out.println("Path exist? " + Files.exists(path));
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
11 changes: 5 additions & 6 deletions src/org/j6toj8/fileio/files/Files_CreateFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@ public static void main(String[] args) {
String userHome = System.getProperty("user.home");
System.out.println("User home: " + userHome);

// Utilizando um nome aleatório de arquivo,
// apenas para o exemplo executar inúmeras vezes sem problemas
String nomeAleatorio = "arquivo" + new Random().nextInt() + ".txt";
// Using a random file name, just for example to run countless times without problems
String randomName = "file" + new Random().nextInt() + ".txt";

Path path = Paths.get(userHome, nomeAleatorio);
Path path = Paths.get(userHome, randomName);
System.out.println("Path: " + path);

try {
System.out.println("Path existe? " + Files.exists(path));
System.out.println("Path exist? " + Files.exists(path));
Files.createFile(path);
System.out.println("Path existe? " + Files.exists(path));
System.out.println("Path exist? " + Files.exists(path));
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
19 changes: 9 additions & 10 deletions src/org/j6toj8/fileio/files/Files_DeletePath.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,22 @@ public static void main(String[] args) {
String userHome = System.getProperty("user.home");
System.out.println("User home: " + userHome);

// Utilizando um nome aleatório de arquivo,
// apenas para o exemplo executar inúmeras vezes sem problemas
String nomeAleatorio = "arquivo" + new Random().nextInt() + ".txt";
// Using a random file name, just for example to run countless times without problems
String randomName = "file" + new Random().nextInt() + ".txt";

Path path = Paths.get(userHome, nomeAleatorio);
Path path = Paths.get(userHome, randomName);
System.out.println("Path: " + path);

try {
System.out.println("Path existe? " + Files.exists(path));
System.out.println("Path exist? " + Files.exists(path));
Files.createFile(path);
System.out.println("Path existe? " + Files.exists(path));
System.out.println("Path exist? " + Files.exists(path));

Files.delete(path); // tenta apagadar o Path e lança exceção se ele não existir
System.out.println("Path existe? " + Files.exists(path));
Files.delete(path); // try to delete Path and throw exception if it doesn't exist
System.out.println("Path exist? " + Files.exists(path));

Files.deleteIfExists(path); // tenta apagadar o Path e não faz nada se ele não existir
System.out.println("Path existe? " + Files.exists(path));
Files.deleteIfExists(path); // try to delete the Path and do nothing if it doesn't exist
System.out.println("Path exist? " + Files.exists(path));
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
11 changes: 5 additions & 6 deletions src/org/j6toj8/fileio/files/Files_LastModified.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@ public static void main(String[] args) {
String userHome = System.getProperty("user.home");
System.out.println("User home: " + userHome);

// Utilizando um nome aleatório de arquivo,
// apenas para o exemplo executar inúmeras vezes sem problemas
String nomeAleatorio = "arquivo" + new Random().nextInt() + ".txt";
// Using a random file name, just for example to run countless times without problems
String randomName = "file" + new Random().nextInt() + ".txt";

Path path = Paths.get(userHome, nomeAleatorio);
Path path = Paths.get(userHome, randomName);
System.out.println("Path: " + path);

try {
Files.createFile(path);
System.out.println("Data de Modificação: " + Files.getLastModifiedTime(path));
System.out.println("Date Modified: " + Files.getLastModifiedTime(path));
FileTime fileTime = FileTime.from(Instant.now().plusMillis(10000));
Files.setLastModifiedTime(path, fileTime);
System.out.println("Data de Modificação: " + Files.getLastModifiedTime(path));
System.out.println("Date Modified: " + Files.getLastModifiedTime(path));
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
24 changes: 12 additions & 12 deletions src/org/j6toj8/fileio/files/Files_MoveFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ public static void main(String[] args) {
String userHome = System.getProperty("user.home");
System.out.println("User home: " + userHome);

String nomeAleatorio = "arquivo" + new Random().nextInt() + ".txt";
String randomName = "arquivo" + new Random().nextInt() + ".txt";

Path arquivoOrigem = Paths.get(userHome, nomeAleatorio);
Path arquivoDestino = Paths.get(userHome, nomeAleatorio + "-movido.txt");
System.out.println("Path Arquivo Origem: " + arquivoOrigem);
System.out.println("Path Arquivo Destino: " + arquivoDestino);
Path sourceFile = Paths.get(userHome, randomName);
Path destinationFile = Paths.get(userHome, randomName + "-moved.txt");
System.out.println("Path Source File: " + sourceFile);
System.out.println("Path Destination File: " + destinationFile);

try {
System.out.println("Arquivo origem existe? " + Files.exists(arquivoOrigem));
Files.createFile(arquivoOrigem);
System.out.println("Arquivo origem existe? " + Files.exists(arquivoOrigem));
System.out.println("Source File exist? " + Files.exists(sourceFile));
Files.createFile(sourceFile);
System.out.println("Source File exist? " + Files.exists(sourceFile));

System.out.println("Arquivo destino existe? " + Files.exists(arquivoDestino));
Files.move(arquivoOrigem, arquivoDestino);
System.out.println("Arquivo destino existe? " + Files.exists(arquivoDestino));
System.out.println("Arquivo origem existe? " + Files.exists(arquivoOrigem));
System.out.println("Destination File exist? " + Files.exists(destinationFile));
Files.move(sourceFile, destinationFile);
System.out.println("Destination File exist? " + Files.exists(destinationFile));
System.out.println("Source File exist? " + Files.exists(sourceFile));
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
Loading

0 comments on commit fea3267

Please sign in to comment.