forked from javaevolved/javaevolved.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath-of.html
More file actions
320 lines (304 loc) · 13 KB
/
path-of.html
File metadata and controls
320 lines (304 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Path.of() factory | java.evolved</title>
<meta name="description" content="Use Path.of() — the modern factory method on the Path interface.">
<meta name="robots" content="index, follow">
<link rel="canonical" href="https://javaevolved.github.io/path-of.html">
<link rel="stylesheet" href="styles.css">
<link rel="icon" href="favicon.svg" type="image/svg+xml">
<link rel="manifest" href="manifest.json">
<meta name="theme-color" content="#f97316">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="apple-mobile-web-app-title" content="java.evolved">
<meta property="og:title" content="Path.of() factory | java.evolved">
<meta property="og:description" content="Use Path.of() — the modern factory method on the Path interface.">
<meta property="og:url" content="https://javaevolved.github.io/path-of.html">
<meta property="og:type" content="article">
<meta property="og:site_name" content="java.evolved">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://javaevolved.github.io/images/social-card.png">
<meta property="og:image:width" content="1200">
<meta property="og:image:height" content="630">
<meta property="og:image:type" content="image/png">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Path.of() factory | java.evolved">
<meta name="twitter:description" content="Use Path.of() — the modern factory method on the Path interface.">
<meta name="twitter:image" content="https://javaevolved.github.io/images/social-card.png">
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "TechArticle",
"headline": "Path.of() factory",
"description": "Use Path.of() \u2014 the modern factory method on the Path interface.",
"url": "https://javaevolved.github.io/path-of.html",
"publisher": {
"@type": "Organization",
"name": "java.evolved",
"url": "https://javaevolved.github.io"
},
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://javaevolved.github.io/path-of.html"
}
}
</script>
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
{
"@type": "ListItem",
"position": 1,
"name": "Home",
"item": "https://javaevolved.github.io/"
},
{
"@type": "ListItem",
"position": 2,
"name": "I/O",
"item": "https://javaevolved.github.io/?cat=io"
},
{
"@type": "ListItem",
"position": 3,
"name": "Path.of() factory"
}
]
}
</script>
</head>
<body data-page="single">
<nav>
<div class="nav-inner">
<a href="/" class="logo">java.<span>evolved</span></a>
<div class="nav-right">
<a href="https://github.com/javaevolved/javaevolved.github.io" target="_blank" rel="noopener" class="github-link" aria-label="View on GitHub">
<svg width="20" height="20" viewBox="0 0 20 20" fill="currentColor">
<circle cx="10" cy="10" r="9" fill="none" stroke="currentColor" stroke-width="1.5"/>
<path d="M10 3C6.13 3 3 6.13 3 10c0 3.09 2 5.71 4.77 6.63.35.06.48-.15.48-.33v-1.16c-1.95.42-2.36-1.07-2.36-1.07-.32-.81-.78-1.03-.78-1.03-.64-.43.05-.42.05-.42.7.05 1.07.72 1.07.72.63 1.08 1.65.77 2.05.59.06-.46.24-.77.44-.95-1.57-.18-3.22-.78-3.22-3.48 0-.77.27-1.4.72-1.89-.07-.18-.31-.9.07-1.87 0 0 .59-.19 1.93.72.56-.16 1.16-.24 1.76-.24s1.2.08 1.76.24c1.34-.91 1.93-.72 1.93-.72.38.97.14 1.69.07 1.87.45.49.72 1.12.72 1.89 0 2.71-1.65 3.3-3.23 3.47.25.22.48.65.48 1.31v1.94c0 .19.13.4.48.33C15 15.71 17 13.09 17 10c0-3.87-3.13-7-7-7z"/>
</svg>
</a>
<a href="/" class="back-link">← All patterns</a>
<div class="nav-arrows">
<a href="/inputstream-transferto.html" aria-label="Previous pattern">←</a>
<a href="/try-with-resources-effectively-final.html" aria-label="Next pattern">→</a>
</div>
</div>
</div>
</nav>
<article class="article">
<div class="breadcrumb">
<a href="/">Home</a>
<span class="sep">/</span>
<a href="/?cat=io">I/O</a>
<span class="sep">/</span>
<span>Path.of() factory</span>
</div>
<div class="tip-header">
<div class="tip-meta">
<span class="badge io">I/O</span>
<span class="badge beginner">beginner</span>
</div>
<h1>Path.of() factory</h1>
<p>Use Path.of() — the modern factory method on the Path interface.</p>
</div>
<section class="compare-section">
<div class="section-label">Code Comparison</div>
<div class="compare-container">
<div class="compare-panel old-panel">
<div class="compare-panel-header">
<span class="compare-tag old">✕ Java 8</span>
<button class="copy-btn" data-code="old">Copy</button>
</div>
<div class="compare-code">
<pre class="code-text">Path path = Paths.get("src", "main",
"java", "App.java");</pre>
</div>
</div>
<div class="compare-panel modern-panel">
<div class="compare-panel-header">
<span class="compare-tag modern">✓ Java 11+</span>
<button class="copy-btn" data-code="modern">Copy</button>
</div>
<div class="compare-code">
<pre class="code-text">Path path = Path.of("src", "main",
"java", "App.java");</pre>
</div>
</div>
</div>
</section>
<section class="why-section">
<div class="section-label">Why the modern way wins</div>
<div class="why-grid">
<div class="why-card">
<div class="why-icon">📐</div>
<h3>Consistent API</h3>
<p>Follows the .of() factory pattern like List.of(), Set.of().</p>
</div>
<div class="why-card">
<div class="why-icon">📖</div>
<h3>Discoverable</h3>
<p>Found on the Path type itself, not a separate Paths class.</p>
</div>
<div class="why-card">
<div class="why-icon">🧹</div>
<h3>One less class</h3>
<p>No need to import the Paths utility class.</p>
</div>
</div>
</section>
<div class="info-grid">
<div class="info-card">
<div class="info-label">Old Approach</div>
<div class="info-value red">Paths.get()</div>
</div>
<div class="info-card">
<div class="info-label">Modern Approach</div>
<div class="info-value green">Path.of()</div>
</div>
<div class="info-card">
<div class="info-label">Since JDK</div>
<div class="info-value accent">11</div>
</div>
<div class="info-card">
<div class="info-label">Difficulty</div>
<div class="info-value blue">beginner</div>
</div>
</div>
<section class="bs-section">
<div class="section-label">JDK Support</div>
<div class="bs-card">
<div class="bs-feature-name">Path.of() factory</div>
<span class="baseline-badge widely">Available</span>
<p class="bs-desc">Widely available since JDK 11 (Sept 2018)</p>
</div>
</section>
<section class="explanation">
<h2>How it works</h2>
<p>Path.of() is a factory method added directly to the Path interface, replacing the separate Paths utility class. It's more discoverable and consistent with List.of(), Map.of(), etc.</p>
</section>
<section class="related">
<h2>Related patterns</h2>
<div class="related-grid">
<a href="/inputstream-transferto.html" class="tip-card">
<div class="tip-card-body">
<div class="tip-card-header">
<div class="tip-badges">
<span class="badge io">I/O</span>
<span class="badge beginner">beginner</span>
</div>
</div>
<h3>InputStream.transferTo()</h3>
</div>
<div class="card-code">
<div class="card-code-layer old-layer">
<div class="mini-label">Java 8</div>
<pre class="code-text">byte[] buf = new byte[8192];
int n;
while ((n = input.read(buf)) != -1) {
output.write(buf, 0, n);
}</pre>
</div>
<div class="card-code-layer modern-layer">
<div class="mini-label">Java 9+</div>
<pre class="code-text">input.transferTo(output);</pre>
</div>
<span class="hover-hint">Hover to see modern ➜</span>
</div>
<div class="tip-card-footer">
<span class="browser-support"><span class="dot"></span>JDK 9+</span>
<span class="arrow-link">→</span>
</div>
</a>
<a href="/reading-files.html" class="tip-card">
<div class="tip-card-body">
<div class="tip-card-header">
<div class="tip-badges">
<span class="badge io">I/O</span>
<span class="badge beginner">beginner</span>
</div>
</div>
<h3>Reading files</h3>
</div>
<div class="card-code">
<div class="card-code-layer old-layer">
<div class="mini-label">Java 8</div>
<pre class="code-text">StringBuilder sb = new StringBuilder();
try (BufferedReader br =
new BufferedReader(
new FileReader("data.txt"))) {
String line;
while ((line = br.readLine()) != null)
sb.append(line).append("\n");
}
String content = sb.toString();</pre>
</div>
<div class="card-code-layer modern-layer">
<div class="mini-label">Java 11+</div>
<pre class="code-text">String content =
Files.readString(Path.of("data.txt"));</pre>
</div>
<span class="hover-hint">Hover to see modern ➜</span>
</div>
<div class="tip-card-footer">
<span class="browser-support"><span class="dot"></span>JDK 11+</span>
<span class="arrow-link">→</span>
</div>
</a>
<a href="/try-with-resources-effectively-final.html" class="tip-card">
<div class="tip-card-body">
<div class="tip-card-header">
<div class="tip-badges">
<span class="badge io">I/O</span>
<span class="badge beginner">beginner</span>
</div>
</div>
<h3>Try-with-resources improvement</h3>
</div>
<div class="card-code">
<div class="card-code-layer old-layer">
<div class="mini-label">Java 8</div>
<pre class="code-text">Connection conn = getConnection();
// Must re-declare in try
try (Connection c = conn) {
use(c);
}</pre>
</div>
<div class="card-code-layer modern-layer">
<div class="mini-label">Java 9+</div>
<pre class="code-text">Connection conn = getConnection();
// Use existing variable directly
try (conn) {
use(conn);
}</pre>
</div>
<span class="hover-hint">Hover to see modern ➜</span>
</div>
<div class="tip-card-footer">
<span class="browser-support"><span class="dot"></span>JDK 9+</span>
<span class="arrow-link">→</span>
</div>
</a>
</div>
</section>
</article>
</section>
<div class="social-share">
<span class="share-label">Share</span>
<a href="https://x.com/intent/tweet?url=https%3A%2F%2Fjavaevolved.github.io%2Fpath-of.html&text=Path.of%28%29%20factory%20%E2%80%93%20java.evolved" target="_blank" rel="noopener" class="share-btn share-x" aria-label="Share on X">𝕏</a>
<a href="https://bsky.app/intent/compose?text=Path.of%28%29%20factory%20%E2%80%93%20java.evolved%20https%3A%2F%2Fjavaevolved.github.io%2Fpath-of.html" target="_blank" rel="noopener" class="share-btn share-bsky" aria-label="Share on Bluesky">🦋</a>
<a href="https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fjavaevolved.github.io%2Fpath-of.html" target="_blank" rel="noopener" class="share-btn share-li" aria-label="Share on LinkedIn">in</a>
<a href="https://www.reddit.com/submit?url=https%3A%2F%2Fjavaevolved.github.io%2Fpath-of.html&title=Path.of%28%29%20factory%20%E2%80%93%20java.evolved" target="_blank" rel="noopener" class="share-btn share-reddit" aria-label="Share on Reddit">⬡</a>
</div>
<footer>
<p>A project by <a href="https://github.com/brunoborges" target="_blank" rel="noopener">Bruno Borges</a></p>
<p><a href="https://github.com/javaevolved/javaevolved.github.io" target="_blank" rel="noopener">View on GitHub</a></p>
</footer>
<script src="app.js"></script>
</body>
</html>