001/* 002 * Copyright (C) 2007 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.io; 016 017import static com.google.common.base.Preconditions.checkArgument; 018import static com.google.common.base.Preconditions.checkNotNull; 019import static com.google.common.io.FileWriteMode.APPEND; 020 021import com.google.common.annotations.Beta; 022import com.google.common.annotations.GwtIncompatible; 023import com.google.common.base.Joiner; 024import com.google.common.base.Optional; 025import com.google.common.base.Predicate; 026import com.google.common.base.Splitter; 027import com.google.common.collect.ImmutableList; 028import com.google.common.collect.ImmutableSet; 029import com.google.common.collect.Lists; 030import com.google.common.graph.SuccessorsFunction; 031import com.google.common.graph.Traverser; 032import com.google.common.hash.HashCode; 033import com.google.common.hash.HashFunction; 034import com.google.errorprone.annotations.CanIgnoreReturnValue; 035import java.io.BufferedReader; 036import java.io.BufferedWriter; 037import java.io.File; 038import java.io.FileInputStream; 039import java.io.FileNotFoundException; 040import java.io.FileOutputStream; 041import java.io.IOException; 042import java.io.InputStreamReader; 043import java.io.OutputStream; 044import java.io.OutputStreamWriter; 045import java.io.RandomAccessFile; 046import java.nio.MappedByteBuffer; 047import java.nio.channels.FileChannel; 048import java.nio.channels.FileChannel.MapMode; 049import java.nio.charset.Charset; 050import java.nio.charset.StandardCharsets; 051import java.util.ArrayList; 052import java.util.Arrays; 053import java.util.Collections; 054import java.util.List; 055 056/** 057 * Provides utility methods for working with {@linkplain File files}. 058 * 059 * <p>{@link java.nio.file.Path} users will find similar utilities in {@link MoreFiles} and the 060 * JDK's {@link java.nio.file.Files} class. 061 * 062 * @author Chris Nokleberg 063 * @author Colin Decker 064 * @since 1.0 065 */ 066@GwtIncompatible 067public final class Files { 068 069 /** Maximum loop count when creating temp directories. */ 070 private static final int TEMP_DIR_ATTEMPTS = 10000; 071 072 private Files() {} 073 074 /** 075 * Returns a buffered reader that reads from a file using the given character set. 076 * 077 * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link 078 * java.nio.file.Files#newBufferedReader(java.nio.file.Path, Charset)}. 079 * 080 * @param file the file to read from 081 * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for 082 * helpful predefined constants 083 * @return the buffered reader 084 */ 085 @Beta 086 public static BufferedReader newReader(File file, Charset charset) throws FileNotFoundException { 087 checkNotNull(file); 088 checkNotNull(charset); 089 return new BufferedReader(new InputStreamReader(new FileInputStream(file), charset)); 090 } 091 092 /** 093 * Returns a buffered writer that writes to a file using the given character set. 094 * 095 * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link 096 * java.nio.file.Files#newBufferedWriter(java.nio.file.Path, Charset, 097 * java.nio.file.OpenOption...)}. 098 * 099 * @param file the file to write to 100 * @param charset the charset used to encode the output stream; see {@link StandardCharsets} for 101 * helpful predefined constants 102 * @return the buffered writer 103 */ 104 @Beta 105 public static BufferedWriter newWriter(File file, Charset charset) throws FileNotFoundException { 106 checkNotNull(file); 107 checkNotNull(charset); 108 return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset)); 109 } 110 111 /** 112 * Returns a new {@link ByteSource} for reading bytes from the given file. 113 * 114 * @since 14.0 115 */ 116 public static ByteSource asByteSource(File file) { 117 return new FileByteSource(file); 118 } 119 120 private static final class FileByteSource extends ByteSource { 121 122 private final File file; 123 124 private FileByteSource(File file) { 125 this.file = checkNotNull(file); 126 } 127 128 @Override 129 public FileInputStream openStream() throws IOException { 130 return new FileInputStream(file); 131 } 132 133 @Override 134 public Optional<Long> sizeIfKnown() { 135 if (file.isFile()) { 136 return Optional.of(file.length()); 137 } else { 138 return Optional.absent(); 139 } 140 } 141 142 @Override 143 public long size() throws IOException { 144 if (!file.isFile()) { 145 throw new FileNotFoundException(file.toString()); 146 } 147 return file.length(); 148 } 149 150 @Override 151 public byte[] read() throws IOException { 152 Closer closer = Closer.create(); 153 try { 154 FileInputStream in = closer.register(openStream()); 155 return ByteStreams.toByteArray(in, in.getChannel().size()); 156 } catch (Throwable e) { 157 throw closer.rethrow(e); 158 } finally { 159 closer.close(); 160 } 161 } 162 163 @Override 164 public String toString() { 165 return "Files.asByteSource(" + file + ")"; 166 } 167 } 168 169 /** 170 * Returns a new {@link ByteSink} for writing bytes to the given file. The given {@code modes} 171 * control how the file is opened for writing. When no mode is provided, the file will be 172 * truncated before writing. When the {@link FileWriteMode#APPEND APPEND} mode is provided, writes 173 * will append to the end of the file without truncating it. 174 * 175 * @since 14.0 176 */ 177 public static ByteSink asByteSink(File file, FileWriteMode... modes) { 178 return new FileByteSink(file, modes); 179 } 180 181 private static final class FileByteSink extends ByteSink { 182 183 private final File file; 184 private final ImmutableSet<FileWriteMode> modes; 185 186 private FileByteSink(File file, FileWriteMode... modes) { 187 this.file = checkNotNull(file); 188 this.modes = ImmutableSet.copyOf(modes); 189 } 190 191 @Override 192 public FileOutputStream openStream() throws IOException { 193 return new FileOutputStream(file, modes.contains(APPEND)); 194 } 195 196 @Override 197 public String toString() { 198 return "Files.asByteSink(" + file + ", " + modes + ")"; 199 } 200 } 201 202 /** 203 * Returns a new {@link CharSource} for reading character data from the given file using the given 204 * character set. 205 * 206 * @since 14.0 207 */ 208 public static CharSource asCharSource(File file, Charset charset) { 209 return asByteSource(file).asCharSource(charset); 210 } 211 212 /** 213 * Returns a new {@link CharSink} for writing character data to the given file using the given 214 * character set. The given {@code modes} control how the file is opened for writing. When no mode 215 * is provided, the file will be truncated before writing. When the {@link FileWriteMode#APPEND 216 * APPEND} mode is provided, writes will append to the end of the file without truncating it. 217 * 218 * @since 14.0 219 */ 220 public static CharSink asCharSink(File file, Charset charset, FileWriteMode... modes) { 221 return asByteSink(file, modes).asCharSink(charset); 222 } 223 224 /** 225 * Reads all bytes from a file into a byte array. 226 * 227 * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link java.nio.file.Files#readAllBytes}. 228 * 229 * @param file the file to read from 230 * @return a byte array containing all the bytes from file 231 * @throws IllegalArgumentException if the file is bigger than the largest possible byte array 232 * (2^31 - 1) 233 * @throws IOException if an I/O error occurs 234 */ 235 @Beta 236 public static byte[] toByteArray(File file) throws IOException { 237 return asByteSource(file).read(); 238 } 239 240 /** 241 * Reads all characters from a file into a {@link String}, using the given character set. 242 * 243 * @param file the file to read from 244 * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for 245 * helpful predefined constants 246 * @return a string containing all the characters from the file 247 * @throws IOException if an I/O error occurs 248 * @deprecated Prefer {@code asCharSource(file, charset).read()}. This method is scheduled to be 249 * removed in October 2019. 250 */ 251 @Beta 252 @Deprecated 253 public static String toString(File file, Charset charset) throws IOException { 254 return asCharSource(file, charset).read(); 255 } 256 257 /** 258 * Overwrites a file with the contents of a byte array. 259 * 260 * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link 261 * java.nio.file.Files#write(java.nio.file.Path, byte[], java.nio.file.OpenOption...)}. 262 * 263 * @param from the bytes to write 264 * @param to the destination file 265 * @throws IOException if an I/O error occurs 266 */ 267 @Beta 268 public static void write(byte[] from, File to) throws IOException { 269 asByteSink(to).write(from); 270 } 271 272 /** 273 * Writes a character sequence (such as a string) to a file using the given character set. 274 * 275 * @param from the character sequence to write 276 * @param to the destination file 277 * @param charset the charset used to encode the output stream; see {@link StandardCharsets} for 278 * helpful predefined constants 279 * @throws IOException if an I/O error occurs 280 * @deprecated Prefer {@code asCharSink(to, charset).write(from)}. This method is scheduled to be 281 * removed in October 2019. 282 */ 283 @Beta 284 @Deprecated 285 public static void write(CharSequence from, File to, Charset charset) throws IOException { 286 asCharSink(to, charset).write(from); 287 } 288 289 /** 290 * Copies all bytes from a file to an output stream. 291 * 292 * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link 293 * java.nio.file.Files#copy(java.nio.file.Path, OutputStream)}. 294 * 295 * @param from the source file 296 * @param to the output stream 297 * @throws IOException if an I/O error occurs 298 */ 299 @Beta 300 public static void copy(File from, OutputStream to) throws IOException { 301 asByteSource(from).copyTo(to); 302 } 303 304 /** 305 * Copies all the bytes from one file to another. 306 * 307 * <p>Copying is not an atomic operation - in the case of an I/O error, power loss, process 308 * termination, or other problems, {@code to} may not be a complete copy of {@code from}. If you 309 * need to guard against those conditions, you should employ other file-level synchronization. 310 * 311 * <p><b>Warning:</b> If {@code to} represents an existing file, that file will be overwritten 312 * with the contents of {@code from}. If {@code to} and {@code from} refer to the <i>same</i> 313 * file, the contents of that file will be deleted. 314 * 315 * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link 316 * java.nio.file.Files#copy(java.nio.file.Path, java.nio.file.Path, java.nio.file.CopyOption...)}. 317 * 318 * @param from the source file 319 * @param to the destination file 320 * @throws IOException if an I/O error occurs 321 * @throws IllegalArgumentException if {@code from.equals(to)} 322 */ 323 @Beta 324 public static void copy(File from, File to) throws IOException { 325 checkArgument(!from.equals(to), "Source %s and destination %s must be different", from, to); 326 asByteSource(from).copyTo(asByteSink(to)); 327 } 328 329 /** 330 * Copies all characters from a file to an appendable object, using the given character set. 331 * 332 * @param from the source file 333 * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for 334 * helpful predefined constants 335 * @param to the appendable object 336 * @throws IOException if an I/O error occurs 337 * @deprecated Prefer {@code asCharSource(from, charset).copyTo(to)}. This method is scheduled to 338 * be removed in October 2019. 339 */ 340 @Beta 341 @Deprecated 342 public 343 static void copy(File from, Charset charset, Appendable to) throws IOException { 344 asCharSource(from, charset).copyTo(to); 345 } 346 347 /** 348 * Appends a character sequence (such as a string) to a file using the given character set. 349 * 350 * @param from the character sequence to append 351 * @param to the destination file 352 * @param charset the charset used to encode the output stream; see {@link StandardCharsets} for 353 * helpful predefined constants 354 * @throws IOException if an I/O error occurs 355 * @deprecated Prefer {@code asCharSink(to, charset, FileWriteMode.APPEND).write(from)}. This 356 * method is scheduled to be removed in October 2019. 357 */ 358 @Beta 359 @Deprecated 360 public 361 static void append(CharSequence from, File to, Charset charset) throws IOException { 362 asCharSink(to, charset, FileWriteMode.APPEND).write(from); 363 } 364 365 /** 366 * Returns true if the given files exist, are not directories, and contain the same bytes. 367 * 368 * @throws IOException if an I/O error occurs 369 */ 370 @Beta 371 public static boolean equal(File file1, File file2) throws IOException { 372 checkNotNull(file1); 373 checkNotNull(file2); 374 if (file1 == file2 || file1.equals(file2)) { 375 return true; 376 } 377 378 /* 379 * Some operating systems may return zero as the length for files denoting system-dependent 380 * entities such as devices or pipes, in which case we must fall back on comparing the bytes 381 * directly. 382 */ 383 long len1 = file1.length(); 384 long len2 = file2.length(); 385 if (len1 != 0 && len2 != 0 && len1 != len2) { 386 return false; 387 } 388 return asByteSource(file1).contentEquals(asByteSource(file2)); 389 } 390 391 /** 392 * Atomically creates a new directory somewhere beneath the system's temporary directory (as 393 * defined by the {@code java.io.tmpdir} system property), and returns its name. 394 * 395 * <p>Use this method instead of {@link File#createTempFile(String, String)} when you wish to 396 * create a directory, not a regular file. A common pitfall is to call {@code createTempFile}, 397 * delete the file and create a directory in its place, but this leads a race condition which can 398 * be exploited to create security vulnerabilities, especially when executable files are to be 399 * written into the directory. 400 * 401 * <p>Depending on the environmment that this code is run in, the system temporary directory (and 402 * thus the directory this method creates) may be more visible that a program would like - files 403 * written to this directory may be read or overwritten by hostile programs running on the same 404 * machine. 405 * 406 * <p>This method assumes that the temporary volume is writable, has free inodes and free blocks, 407 * and that it will not be called thousands of times per second. 408 * 409 * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link 410 * java.nio.file.Files#createTempDirectory}. 411 * 412 * @return the newly-created directory 413 * @throws IllegalStateException if the directory could not be created 414 * @deprecated For Android users, see the <a 415 * href="https://developer.android.com/training/data-storage" target="_blank">Data and File 416 * Storage overview</a> to select an appropriate temporary directory (perhaps {@code 417 * context.getCacheDir()}). For developers on Java 7 or later, use {@link 418 * java.nio.file.Files#createTempDirectory}, transforming it to a {@link File} using {@link 419 * java.nio.file.Path#toFile() toFile()} if needed. 420 */ 421 @Beta 422 @Deprecated 423 public static File createTempDir() { 424 File baseDir = new File(System.getProperty("java.io.tmpdir")); 425 @SuppressWarnings("GoodTime") // reading system time without TimeSource 426 String baseName = System.currentTimeMillis() + "-"; 427 428 for (int counter = 0; counter < TEMP_DIR_ATTEMPTS; counter++) { 429 File tempDir = new File(baseDir, baseName + counter); 430 if (tempDir.mkdir()) { 431 return tempDir; 432 } 433 } 434 throw new IllegalStateException( 435 "Failed to create directory within " 436 + TEMP_DIR_ATTEMPTS 437 + " attempts (tried " 438 + baseName 439 + "0 to " 440 + baseName 441 + (TEMP_DIR_ATTEMPTS - 1) 442 + ')'); 443 } 444 445 /** 446 * Creates an empty file or updates the last updated timestamp on the same as the unix command of 447 * the same name. 448 * 449 * @param file the file to create or update 450 * @throws IOException if an I/O error occurs 451 */ 452 @Beta 453 @SuppressWarnings("GoodTime") // reading system time without TimeSource 454 public static void touch(File file) throws IOException { 455 checkNotNull(file); 456 if (!file.createNewFile() && !file.setLastModified(System.currentTimeMillis())) { 457 throw new IOException("Unable to update modification time of " + file); 458 } 459 } 460 461 /** 462 * Creates any necessary but nonexistent parent directories of the specified file. Note that if 463 * this operation fails it may have succeeded in creating some (but not all) of the necessary 464 * parent directories. 465 * 466 * @throws IOException if an I/O error occurs, or if any necessary but nonexistent parent 467 * directories of the specified file could not be created. 468 * @since 4.0 469 */ 470 @Beta 471 public static void createParentDirs(File file) throws IOException { 472 checkNotNull(file); 473 File parent = file.getCanonicalFile().getParentFile(); 474 if (parent == null) { 475 /* 476 * The given directory is a filesystem root. All zero of its ancestors exist. This doesn't 477 * mean that the root itself exists -- consider x:\ on a Windows machine without such a drive 478 * -- or even that the caller can create it, but this method makes no such guarantees even for 479 * non-root files. 480 */ 481 return; 482 } 483 parent.mkdirs(); 484 if (!parent.isDirectory()) { 485 throw new IOException("Unable to create parent directories of " + file); 486 } 487 } 488 489 /** 490 * Moves a file from one path to another. This method can rename a file and/or move it to a 491 * different directory. In either case {@code to} must be the target path for the file itself; not 492 * just the new name for the file or the path to the new parent directory. 493 * 494 * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link java.nio.file.Files#move}. 495 * 496 * @param from the source file 497 * @param to the destination file 498 * @throws IOException if an I/O error occurs 499 * @throws IllegalArgumentException if {@code from.equals(to)} 500 */ 501 @Beta 502 public static void move(File from, File to) throws IOException { 503 checkNotNull(from); 504 checkNotNull(to); 505 checkArgument(!from.equals(to), "Source %s and destination %s must be different", from, to); 506 507 if (!from.renameTo(to)) { 508 copy(from, to); 509 if (!from.delete()) { 510 if (!to.delete()) { 511 throw new IOException("Unable to delete " + to); 512 } 513 throw new IOException("Unable to delete " + from); 514 } 515 } 516 } 517 518 /** 519 * Reads the first line from a file. The line does not include line-termination characters, but 520 * does include other leading and trailing whitespace. 521 * 522 * @param file the file to read from 523 * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for 524 * helpful predefined constants 525 * @return the first line, or null if the file is empty 526 * @throws IOException if an I/O error occurs 527 * @deprecated Prefer {@code asCharSource(file, charset).readFirstLine()}. This method is 528 * scheduled to be removed in October 2019. 529 */ 530 @Beta 531 @Deprecated 532 public 533 static String readFirstLine(File file, Charset charset) throws IOException { 534 return asCharSource(file, charset).readFirstLine(); 535 } 536 537 /** 538 * Reads all of the lines from a file. The lines do not include line-termination characters, but 539 * do include other leading and trailing whitespace. 540 * 541 * <p>This method returns a mutable {@code List}. For an {@code ImmutableList}, use {@code 542 * Files.asCharSource(file, charset).readLines()}. 543 * 544 * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link 545 * java.nio.file.Files#readAllLines(java.nio.file.Path, Charset)}. 546 * 547 * @param file the file to read from 548 * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for 549 * helpful predefined constants 550 * @return a mutable {@link List} containing all the lines 551 * @throws IOException if an I/O error occurs 552 */ 553 @Beta 554 public static List<String> readLines(File file, Charset charset) throws IOException { 555 // don't use asCharSource(file, charset).readLines() because that returns 556 // an immutable list, which would change the behavior of this method 557 return asCharSource(file, charset) 558 .readLines( 559 new LineProcessor<List<String>>() { 560 final List<String> result = Lists.newArrayList(); 561 562 @Override 563 public boolean processLine(String line) { 564 result.add(line); 565 return true; 566 } 567 568 @Override 569 public List<String> getResult() { 570 return result; 571 } 572 }); 573 } 574 575 /** 576 * Streams lines from a {@link File}, stopping when our callback returns false, or we have read 577 * all of the lines. 578 * 579 * @param file the file to read from 580 * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for 581 * helpful predefined constants 582 * @param callback the {@link LineProcessor} to use to handle the lines 583 * @return the output of processing the lines 584 * @throws IOException if an I/O error occurs 585 * @deprecated Prefer {@code asCharSource(file, charset).readLines(callback)}. This method is 586 * scheduled to be removed in October 2019. 587 */ 588 @Beta 589 @Deprecated 590 @CanIgnoreReturnValue // some processors won't return a useful result 591 public 592 static <T> T readLines(File file, Charset charset, LineProcessor<T> callback) throws IOException { 593 return asCharSource(file, charset).readLines(callback); 594 } 595 596 /** 597 * Process the bytes of a file. 598 * 599 * <p>(If this seems too complicated, maybe you're looking for {@link #toByteArray}.) 600 * 601 * @param file the file to read 602 * @param processor the object to which the bytes of the file are passed. 603 * @return the result of the byte processor 604 * @throws IOException if an I/O error occurs 605 * @deprecated Prefer {@code asByteSource(file).read(processor)}. This method is scheduled to be 606 * removed in October 2019. 607 */ 608 @Beta 609 @Deprecated 610 @CanIgnoreReturnValue // some processors won't return a useful result 611 public 612 static <T> T readBytes(File file, ByteProcessor<T> processor) throws IOException { 613 return asByteSource(file).read(processor); 614 } 615 616 /** 617 * Computes the hash code of the {@code file} using {@code hashFunction}. 618 * 619 * @param file the file to read 620 * @param hashFunction the hash function to use to hash the data 621 * @return the {@link HashCode} of all of the bytes in the file 622 * @throws IOException if an I/O error occurs 623 * @since 12.0 624 * @deprecated Prefer {@code asByteSource(file).hash(hashFunction)}. This method is scheduled to 625 * be removed in October 2019. 626 */ 627 @Beta 628 @Deprecated 629 public 630 static HashCode hash(File file, HashFunction hashFunction) throws IOException { 631 return asByteSource(file).hash(hashFunction); 632 } 633 634 /** 635 * Fully maps a file read-only in to memory as per {@link 636 * FileChannel#map(java.nio.channels.FileChannel.MapMode, long, long)}. 637 * 638 * <p>Files are mapped from offset 0 to its length. 639 * 640 * <p>This only works for files ≤ {@link Integer#MAX_VALUE} bytes. 641 * 642 * @param file the file to map 643 * @return a read-only buffer reflecting {@code file} 644 * @throws FileNotFoundException if the {@code file} does not exist 645 * @throws IOException if an I/O error occurs 646 * @see FileChannel#map(MapMode, long, long) 647 * @since 2.0 648 */ 649 @Beta 650 public static MappedByteBuffer map(File file) throws IOException { 651 checkNotNull(file); 652 return map(file, MapMode.READ_ONLY); 653 } 654 655 /** 656 * Fully maps a file in to memory as per {@link 657 * FileChannel#map(java.nio.channels.FileChannel.MapMode, long, long)} using the requested {@link 658 * MapMode}. 659 * 660 * <p>Files are mapped from offset 0 to its length. 661 * 662 * <p>This only works for files ≤ {@link Integer#MAX_VALUE} bytes. 663 * 664 * @param file the file to map 665 * @param mode the mode to use when mapping {@code file} 666 * @return a buffer reflecting {@code file} 667 * @throws FileNotFoundException if the {@code file} does not exist 668 * @throws IOException if an I/O error occurs 669 * @see FileChannel#map(MapMode, long, long) 670 * @since 2.0 671 */ 672 @Beta 673 public static MappedByteBuffer map(File file, MapMode mode) throws IOException { 674 return mapInternal(file, mode, -1); 675 } 676 677 /** 678 * Maps a file in to memory as per {@link FileChannel#map(java.nio.channels.FileChannel.MapMode, 679 * long, long)} using the requested {@link MapMode}. 680 * 681 * <p>Files are mapped from offset 0 to {@code size}. 682 * 683 * <p>If the mode is {@link MapMode#READ_WRITE} and the file does not exist, it will be created 684 * with the requested {@code size}. Thus this method is useful for creating memory mapped files 685 * which do not yet exist. 686 * 687 * <p>This only works for files ≤ {@link Integer#MAX_VALUE} bytes. 688 * 689 * @param file the file to map 690 * @param mode the mode to use when mapping {@code file} 691 * @return a buffer reflecting {@code file} 692 * @throws IOException if an I/O error occurs 693 * @see FileChannel#map(MapMode, long, long) 694 * @since 2.0 695 */ 696 @Beta 697 public static MappedByteBuffer map(File file, MapMode mode, long size) throws IOException { 698 checkArgument(size >= 0, "size (%s) may not be negative", size); 699 return mapInternal(file, mode, size); 700 } 701 702 private static MappedByteBuffer mapInternal(File file, MapMode mode, long size) 703 throws IOException { 704 checkNotNull(file); 705 checkNotNull(mode); 706 707 Closer closer = Closer.create(); 708 try { 709 RandomAccessFile raf = 710 closer.register(new RandomAccessFile(file, mode == MapMode.READ_ONLY ? "r" : "rw")); 711 FileChannel channel = closer.register(raf.getChannel()); 712 return channel.map(mode, 0, size == -1 ? channel.size() : size); 713 } catch (Throwable e) { 714 throw closer.rethrow(e); 715 } finally { 716 closer.close(); 717 } 718 } 719 720 /** 721 * Returns the lexically cleaned form of the path name, <i>usually</i> (but not always) equivalent 722 * to the original. The following heuristics are used: 723 * 724 * <ul> 725 * <li>empty string becomes . 726 * <li>. stays as . 727 * <li>fold out ./ 728 * <li>fold out ../ when possible 729 * <li>collapse multiple slashes 730 * <li>delete trailing slashes (unless the path is just "/") 731 * </ul> 732 * 733 * <p>These heuristics do not always match the behavior of the filesystem. In particular, consider 734 * the path {@code a/../b}, which {@code simplifyPath} will change to {@code b}. If {@code a} is a 735 * symlink to {@code x}, {@code a/../b} may refer to a sibling of {@code x}, rather than the 736 * sibling of {@code a} referred to by {@code b}. 737 * 738 * @since 11.0 739 */ 740 @Beta 741 public static String simplifyPath(String pathname) { 742 checkNotNull(pathname); 743 if (pathname.length() == 0) { 744 return "."; 745 } 746 747 // split the path apart 748 Iterable<String> components = Splitter.on('/').omitEmptyStrings().split(pathname); 749 List<String> path = new ArrayList<>(); 750 751 // resolve ., .., and // 752 for (String component : components) { 753 switch (component) { 754 case ".": 755 continue; 756 case "..": 757 if (path.size() > 0 && !path.get(path.size() - 1).equals("..")) { 758 path.remove(path.size() - 1); 759 } else { 760 path.add(".."); 761 } 762 break; 763 default: 764 path.add(component); 765 break; 766 } 767 } 768 769 // put it back together 770 String result = Joiner.on('/').join(path); 771 if (pathname.charAt(0) == '/') { 772 result = "/" + result; 773 } 774 775 while (result.startsWith("/../")) { 776 result = result.substring(3); 777 } 778 if (result.equals("/..")) { 779 result = "/"; 780 } else if ("".equals(result)) { 781 result = "."; 782 } 783 784 return result; 785 } 786 787 /** 788 * Returns the <a href="http://en.wikipedia.org/wiki/Filename_extension">file extension</a> for 789 * the given file name, or the empty string if the file has no extension. The result does not 790 * include the '{@code .}'. 791 * 792 * <p><b>Note:</b> This method simply returns everything after the last '{@code .}' in the file's 793 * name as determined by {@link File#getName}. It does not account for any filesystem-specific 794 * behavior that the {@link File} API does not already account for. For example, on NTFS it will 795 * report {@code "txt"} as the extension for the filename {@code "foo.exe:.txt"} even though NTFS 796 * will drop the {@code ":.txt"} part of the name when the file is actually created on the 797 * filesystem due to NTFS's <a href="https://goo.gl/vTpJi4">Alternate Data Streams</a>. 798 * 799 * @since 11.0 800 */ 801 @Beta 802 public static String getFileExtension(String fullName) { 803 checkNotNull(fullName); 804 String fileName = new File(fullName).getName(); 805 int dotIndex = fileName.lastIndexOf('.'); 806 return (dotIndex == -1) ? "" : fileName.substring(dotIndex + 1); 807 } 808 809 /** 810 * Returns the file name without its <a 811 * href="http://en.wikipedia.org/wiki/Filename_extension">file extension</a> or path. This is 812 * similar to the {@code basename} unix command. The result does not include the '{@code .}'. 813 * 814 * @param file The name of the file to trim the extension from. This can be either a fully 815 * qualified file name (including a path) or just a file name. 816 * @return The file name without its path or extension. 817 * @since 14.0 818 */ 819 @Beta 820 public static String getNameWithoutExtension(String file) { 821 checkNotNull(file); 822 String fileName = new File(file).getName(); 823 int dotIndex = fileName.lastIndexOf('.'); 824 return (dotIndex == -1) ? fileName : fileName.substring(0, dotIndex); 825 } 826 827 /** 828 * Returns a {@link Traverser} instance for the file and directory tree. The returned traverser 829 * starts from a {@link File} and will return all files and directories it encounters. 830 * 831 * <p><b>Warning:</b> {@code File} provides no support for symbolic links, and as such there is no 832 * way to ensure that a symbolic link to a directory is not followed when traversing the tree. In 833 * this case, iterables created by this traverser could contain files that are outside of the 834 * given directory or even be infinite if there is a symbolic link loop. 835 * 836 * <p>If available, consider using {@link MoreFiles#fileTraverser()} instead. It behaves the same 837 * except that it doesn't follow symbolic links and returns {@code Path} instances. 838 * 839 * <p>If the {@link File} passed to one of the {@link Traverser} methods does not exist or is not 840 * a directory, no exception will be thrown and the returned {@link Iterable} will contain a 841 * single element: that file. 842 * 843 * <p>Example: {@code Files.fileTraverser().depthFirstPreOrder(new File("/"))} may return files 844 * with the following paths: {@code ["/", "/etc", "/etc/config.txt", "/etc/fonts", "/home", 845 * "/home/alice", ...]} 846 * 847 * @since 23.5 848 */ 849 @Beta 850 public static Traverser<File> fileTraverser() { 851 return Traverser.forTree(FILE_TREE); 852 } 853 854 private static final SuccessorsFunction<File> FILE_TREE = 855 new SuccessorsFunction<File>() { 856 @Override 857 public Iterable<File> successors(File file) { 858 // check isDirectory() just because it may be faster than listFiles() on a non-directory 859 if (file.isDirectory()) { 860 File[] files = file.listFiles(); 861 if (files != null) { 862 return Collections.unmodifiableList(Arrays.asList(files)); 863 } 864 } 865 866 return ImmutableList.of(); 867 } 868 }; 869 870 /** 871 * Returns a predicate that returns the result of {@link File#isDirectory} on input files. 872 * 873 * @since 15.0 874 */ 875 @Beta 876 public static Predicate<File> isDirectory() { 877 return FilePredicate.IS_DIRECTORY; 878 } 879 880 /** 881 * Returns a predicate that returns the result of {@link File#isFile} on input files. 882 * 883 * @since 15.0 884 */ 885 @Beta 886 public static Predicate<File> isFile() { 887 return FilePredicate.IS_FILE; 888 } 889 890 private enum FilePredicate implements Predicate<File> { 891 IS_DIRECTORY { 892 @Override 893 public boolean apply(File file) { 894 return file.isDirectory(); 895 } 896 897 @Override 898 public String toString() { 899 return "Files.isDirectory()"; 900 } 901 }, 902 903 IS_FILE { 904 @Override 905 public boolean apply(File file) { 906 return file.isFile(); 907 } 908 909 @Override 910 public String toString() { 911 return "Files.isFile()"; 912 } 913 } 914 } 915}