Skip to content

Commit 3ef7e75

Browse files
committed
feat(file-io): 🎸 paths, translated
Refers: #9
1 parent 8d4c0a2 commit 3ef7e75

File tree

12 files changed

+137
-138
lines changed

12 files changed

+137
-138
lines changed

book/07-file-io/sections/01-paths.asc

Lines changed: 75 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,28 @@
33

44
=== Paths
55

6-
.Objetivo
6+
.Objective
77
--------------------------------------------------
88
Operate on file and directory paths by using the Paths class
9-
-
10-
Operar em arquivos e diretórios usando a classe Paths
119
--------------------------------------------------
1210

13-
As classes `Path` e `Paths` são novidades do Java 7.
11+
The `Path` and `Paths` classes are new to Java 7.
1412

15-
A classe `Path` representa um arquivo ou um diretório no sistema de arquivos, e a maioria das suas operações não altera diretamente arquivos ou diretórios.
13+
The `Path` class represents a file or directory in the file system, and most of its operations do not directly change files or directories.
1614

17-
A classe `Paths` contém métodos estáticos para a criação de `Path`.
15+
The `Paths` class contains static methods for creating `Path`.
1816

19-
Para que os exemplos executem independente do sistema, será utilizado o diretório do usuário, que no Java está disponível em uma propriedade da JVM chamada `user.home`.
17+
For the examples to run system-independent, the user directory will be used, which in Java is available in a JVM property called `user.home`.
2018

21-
. Existem inúmeras formas de obter uma instância de `Path`.
19+
. There are numerous ways to get an instance of `Path`.
2220
+
2321
[source,java,indent=0]
2422
.{java-package}/paths/Paths_Creation.java
2523
----
2624
include::{section-java-package}/paths/Paths_Creation.java[tag=code]
2725
----
2826
+
29-
.Saída no console
27+
.console output
3028
[source,console]
3129
----
3230
User home: /home/rinaldo
@@ -42,189 +40,189 @@ Path 9: /home/rinaldo/arquivo.txt
4240
Path 10: /home/rinaldo
4341
----
4442

45-
. É possível criar uma instância de `Path` apontando para um diretório ou arquivo que não existe.
43+
. You can create an instance of `Path` by pointing to a directory or file that does not exist.
4644
+
4745
[source,java,indent=0]
4846
.{java-package}/paths/Paths_CreationDoesntExists.java
4947
----
5048
include::{section-java-package}/paths/Paths_CreationDoesntExists.java[tag=code]
5149
----
5250
+
53-
.Saída no console
51+
.console output
5452
[source,console]
5553
----
5654
User home: /home/rinaldo
57-
Path: /home/rinaldo/arquivoQueNaoExiste.txt
55+
Path: /home/rinaldo/fileThatDoesNotExist.txt
5856
----
5957

60-
. É possível converter um `Path` para um `File`.
58+
. It is possible to convert a `Path` to a `File`.
6159
+
6260
[source,java,indent=0]
6361
.{java-package}/paths/Paths_ToFile.java
6462
----
6563
include::{section-java-package}/paths/Paths_ToFile.java[tag=code]
6664
----
6765
+
68-
.Saída no console
66+
.console output
6967
[source,console]
7068
----
7169
User home: /home/rinaldo
72-
Path: /home/rinaldo/arquivoQueNaoExiste.txt
73-
File: /home/rinaldo/arquivoQueNaoExiste.txt
70+
Path: /home/rinaldo/fileThatDoesNotExist.txt
71+
File: /home/rinaldo/fileThatDoesNotExist.txt
7472
----
7573

76-
. Existem inúmeros método no `Path` para recuperar informações a seu respeito.
74+
. There are numerous methods in `Path` to retrieve information about it.
7775
+
7876
[source,java,indent=0]
7977
.{java-package}/paths/Paths_Information.java
8078
----
8179
include::{section-java-package}/paths/Paths_Information.java[tag=code]
8280
----
8381
+
84-
.Saída no console
82+
.console output
8583
[source,console]
8684
----
8785
User home: /home/rinaldo
8886
89-
Representação em String: /home/rinaldo/arquivos/arquivo.txt
90-
Nome do Arquivo: arquivo.txt
91-
Diretório Superior: /home/rinaldo/arquivos
92-
Diretório Raiz: /
93-
É absoluto? true
87+
String representation: /home/rinaldo/files/file.txt
88+
File Name: file.txt
89+
Top Directory: /home/rinaldo/files
90+
Root Directory: /
91+
Is it absolute?: true
9492
95-
Representação em String: home/rinaldo/arquivos
96-
Nome do Arquivo: arquivos
97-
Diretório Superior: home/rinaldo
98-
Diretório Raiz: null
99-
É absoluto? false
93+
String representation: home/rinaldo/files
94+
File Name: files
95+
Top Directory: home/rinaldo
96+
Root Directory: null
97+
Is it absolute?: false
10098
----
10199

102-
. É possível recuperar os elementos do `Path` individualmente.
100+
. It is possible to retrieve the elements of `Path` individually.
103101
+
104102
[source,java,indent=0]
105103
.{java-package}/paths/Paths_Names.java
106104
----
107105
include::{section-java-package}/paths/Paths_Names.java[tag=code]
108106
----
109107
+
110-
.Saída no console
108+
.console output
111109
[source,console]
112110
----
113111
User home: /home/rinaldo
114112
115113
home
116114
rinaldo
117-
arquivos
118-
arquivo.txt
115+
files
116+
file.txt
119117
----
120118

121-
. É possível converter um `Path` relativo para um absoluto.
119+
. It is possible to convert a relative `Path` to an absolute.
122120
+
123121
[source,java,indent=0]
124122
.{java-package}/paths/Paths_ToAbsolute.java
125123
----
126124
include::{section-java-package}/paths/Paths_ToAbsolute.java[tag=code]
127125
----
128126
+
129-
.Saída no console
127+
.console output
130128
[source,console]
131129
----
132-
arquivos
133-
É absoluto? false
130+
files
131+
Is it absolute? false
134132
135-
/home/rinaldo/Desenvolvimento/git/java6-to-java8/arquivos
136-
É absoluto? true
133+
/home/rinaldo/Desenvolvimento/git/java6-to-java8/files
134+
Is it absolute? true
137135
----
138136
+
139-
Neste caso a saída do console vai depende do diretório onde a aplicação está sendo executada.
137+
In this case the console output will depend on the directory where the application is running.
140138

141-
. É possível criar _Sub-Paths_ a partir de um `Path`.
139+
. You can create _Sub-Paths_ from a `Path`.
142140
+
143141
[source,java,indent=0]
144142
.{java-package}/paths/Paths_SubPath.java
145143
----
146144
include::{section-java-package}/paths/Paths_SubPath.java[tag=code]
147145
----
148146
+
149-
.Saída no console
147+
.console output
150148
[source,console]
151149
----
152150
User home: /home/rinaldo
153-
Path: /home/rinaldo/arquivos/arquivo1.txt
151+
Path: /home/rinaldo/arquivos/file1.txt
154152
home
155153
home/rinaldo
156-
rinaldo/arquivos
157-
arquivos/arquivo1.txt
154+
rinaldo/files
155+
arquivos/file1.txt
158156
Exception in thread "main" java.lang.IllegalArgumentException
159157
at sun.nio.fs.UnixPath.subpath(UnixPath.java:348)
160158
at sun.nio.fs.UnixPath.subpath(UnixPath.java:43)
161159
at org.j6toj8.fileio.paths.Paths_SubPath.main(Paths_SubPath.java:28)
162160
----
163161

164-
. É possível remover redundâncias de um `Path` com o método `normalize`.
162+
. You can remove redundancies from a `Path` with the `normalize` method.
165163
+
166164
[source,java,indent=0]
167165
.{java-package}/paths/Paths_Normalize.java
168166
----
169167
include::{section-java-package}/paths/Paths_Normalize.java[tag=code]
170168
----
171169
+
172-
.Saída no console
170+
.console output
173171
[source,console]
174172
----
175173
User home: /home/rinaldo
176174
177-
Path: /home/rinaldo/arquivos/./arquivo1.txt
178-
Path normalize: /home/rinaldo/arquivos/arquivo1.txt
175+
Path: /home/rinaldo/files/./file1.txt
176+
Path normalize: /home/rinaldo/files/file1.txt
179177
180-
Path: /home/rinaldo/arquivos/../arquivo1.txt
181-
Path normalize: /home/rinaldo/arquivo1.txt
178+
Path: /home/rinaldo/files/../file1.txt
179+
Path normalize: /home/rinaldo/file1.txt
182180
----
183181

184-
. É possível unir duas instâncias de `Path` com o método `resolve`.
182+
. You can join two instances of `Path` with the `resolve` method.
185183
+
186184
[source,java,indent=0]
187185
.{java-package}/paths/Paths_Resolve.java
188186
----
189187
include::{section-java-package}/paths/Paths_Resolve.java[tag=code]
190188
----
191189
+
192-
.Saída no console
190+
.console output
193191
[source,console]
194192
----
195193
User home: /home/rinaldo
196194
197-
Absoluto + Relativo: /home/rinaldo/arquivos/arquivo1.txt
198-
Relativo + Absoluto: /home/rinaldo/arquivos
199-
Absoluto + Absoluto: /home/rinaldo/arquivos
200-
Relativo + Relativo: arquivo1.txt/arquivo1.txt
195+
Absolute + Relative: /home/rinaldo/files/file1.txt
196+
Relative + Absolute: /home/rinaldo/files
197+
Absolute + Absolute: /home/rinaldo/files
198+
Relative + Relative: file1.txt/file1.txt
201199
----
202200
+
203-
Perceba que sempre que o argumento é um `Path` absoluto, o resultado final é ele mesmo.
201+
Note that whenever the argument is an absolute `Path`, the end result is itself.
204202
+
205-
Quando o argumento é um `Path` relativo, ele é acrescentado ao original, seja este absoluto ou relativo.
203+
When the argument is a relative `Path`, it is appended to the original, either absolute or relative.
206204

207-
. É possível derivar um `Path` de outro com o método `relativize`.
205+
. It is possible to derive one `Path` from another with the `relativize` method.
208206
+
209207
[source,java,indent=0]
210208
.{java-package}/paths/Paths_Relativize.java
211209
----
212210
include::{section-java-package}/paths/Paths_Relativize.java[tag=code]
213211
----
214212
+
215-
.Saída no console
213+
.console output
216214
[source,console]
217215
----
218216
User home: /home/rinaldo
219217
220-
Absoluto 1: /home/rinaldo/arquivos
221-
Absoluto 2: /home/rinaldo/arquivos/arquivo1.txt
222-
Relativo 1: arquivo1.txt
223-
Relativo 2: arquivos/arquivo1.txt
224-
Absoluto 1 + Absoluto 2: arquivo1.txt
225-
Absoluto 2 + Absoluto 1: ..
226-
Relativo 1 + Relativo 2: ../arquivos/arquivo1.txt
227-
Relativo 2 + Relativo 1: ../../arquivo1.txt
218+
Absolute 1: /home/rinaldo/files
219+
Absolute 2: /home/rinaldo/files/file1.txt
220+
Relative 1: file1.txt
221+
Relative 2: files/file1.txt
222+
Absolute 1 + Absolute 2: file1.txt
223+
Absolute 2 + Absolute 1: ..
224+
Relative 1 + Relative 2: ../files/file1.txt
225+
Relative 2 + Relative 1: ../../file1.txt
228226
java.lang.IllegalArgumentException: 'other' is different type of Path
229227
at sun.nio.fs.UnixPath.relativize(UnixPath.java:416)
230228
at sun.nio.fs.UnixPath.relativize(UnixPath.java:43)
@@ -235,41 +233,42 @@ java.lang.IllegalArgumentException: 'other' is different type of Path
235233
at org.j6toj8.fileio.paths.Paths_Relativize.main(Paths_Relativize.java:40)
236234
----
237235
+
238-
Todas essas combinações podem aparecere no exame, então entenda bem como cada uma delas se comporta. Lembre-se principalmente de que não é possível derivar um `Path` absoluto de um relativo, e vice-versa.
236+
All of these combinations may appear on the exam, so understand well how each one behaves. Remember especially that it is not possible to derive an absolute `Path` from a relative, and vice versa.
239237

240-
. É possível converter um `Path` sintético, que não aponta de fato para um arquivo no sistema de arquivos, em um `Path` real, que aponta para um arquivo ou diretório que existe no sistema de arquivos.
238+
. It is possible to convert a synthetic `Path`, which does not actually point to a file in the file system, into a real `Path`, which points to a file or directory that exists in the file system.
241239
+
242240
[source,java,indent=0]
243241
.{java-package}/paths/Paths_ToRealPath.java
244242
----
245243
include::{section-java-package}/paths/Paths_ToRealPath.java[tag=code]
246244
----
247245
+
248-
.Saída no console
246+
.console output
249247
[source,console]
250248
----
251249
User home: /home/rinaldo
252-
realPath: /home/rinaldo/arquivo1.txt
253-
java.nio.file.NoSuchFileException: /home/rinaldo/arquivoQueNaoExiste.txt
250+
realPath: /home/rinaldo/file1.txt
251+
java.nio.file.NoSuchFileException: /home/rinaldo/fileThatDoesNotExist.txt
254252
at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
255253
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
256254
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
257255
at sun.nio.fs.UnixPath.toRealPath(UnixPath.java:837)
258256
at org.j6toj8.fileio.paths.Paths_ToRealPath.main(Paths_ToRealPath.java:25)
259257
----
260258
+
261-
Perceba que é lançada exceção caso o arquivo realmente não exista no sistema de arquivos.
259+
Note that an exception is thrown if the file does not actually exist in the file system.
262260

261+
.References
263262
****
264263
265264
* Introducing NIO.2
266265
+
267-
Boyarsky, Jeanne; Selikoff, Scott. OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide (p. 454). Wiley. Edição do Kindle.
266+
Boyarsky, Jeanne; Selikoff, Scott. OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide (p. 454). Wiley. Kindle Edition.
268267
269268
* https://www.baeldung.com/java-nio-2-path[Java NIO2 Path API.]
270269
271270
* https://docs.oracle.com/javase/7/docs/api/java/nio/file/Paths.html[Class Paths.] Java Plataform SE 7.
272271
273272
* https://docs.oracle.com/javase/tutorial/essential/io/pathOps.html[Path Operations.] The Java™ Tutorials.
274273
275-
****
274+
****

src/org/j6toj8/fileio/paths/Paths_Creation.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,47 +9,47 @@ public class Paths_Creation {
99

1010
public static void main(String[] args) {
1111
// tag::code[]
12-
// diretório padrão do usuário
12+
// default user directory
1313
String userHome = System.getProperty("user.home");
1414
System.out.println("User home: " + userHome);
1515

16-
// path absoluto
16+
// absolute path
1717
Path path1 = Paths.get("/home/rinaldo");
1818
System.out.println("Path 1: " + path1);
1919

20-
// path absoluto dividido em strings
20+
// absolute path divided into strings
2121
Path path2 = Paths.get("/", "home", "rinaldo");
2222
System.out.println("Path 2: " + path2);
2323

24-
// path absoluto a partir do userHome
24+
// absolute path from userHome
2525
Path path3 = Paths.get(userHome);
2626
System.out.println("Path 3: " + path3);
2727

28-
// path absoluto para um arquivo
28+
// absolute path to a file
2929
Path path4 = Paths.get("/home/rinaldo/arquivos/arquivo.txt");
3030
System.out.println("Path 4: " + path4);
3131

32-
// path absoluto para um arquivo a partir do userHome
32+
// absolute path to a file from userHome
3333
Path path5 = Paths.get(userHome, "arquivos", "arquivo.txt");
3434
System.out.println("Path 5: " + path5);
3535

36-
// path absoluto em um sistema windows
36+
// absolute path on a windows system
3737
Path path6 = Paths.get("C:/users/rinaldo");
3838
System.out.println("Path 6: " + path6);
3939

40-
// path absoluto windows divido em strings
40+
// absolute path on a windows split into strings
4141
Path path7 = Paths.get("C:", "users", "rinaldo");
4242
System.out.println("Path 7: " + path7);
4343

44-
// path relativo
44+
// relative path
4545
Path path8 = Paths.get("rinaldo");
4646
System.out.println("Path 8: " + path8);
4747

48-
// path a partir de uma URI
48+
// path from a URI
4949
Path path9 = Paths.get(URI.create("file:///home/rinaldo/arquivos/arquivo.txt"));
5050
System.out.println("Path 9: " + path9);
51-
52-
// path sem utilizar a classe Paths - produz o mesmo resultado
51+
52+
// path without using the Paths class - produces the same result
5353
Path path10 = FileSystems.getDefault().getPath("/home/rinaldo");
5454
System.out.println("Path 10: " + path10);
5555
// end::code[]

0 commit comments

Comments
 (0)