< prev index next >

src/java.base/share/classes/java/nio/file/Files.java

Print this page




3262      *
3263      * @return  a String containing the content read from the file
3264      *
3265      * @throws  IOException
3266      *          if an I/O error occurs reading from the file or a malformed or
3267      *          unmappable byte sequence is read
3268      * @throws  OutOfMemoryError
3269      *          if the file is extremely large, for example larger than {@code 2GB}
3270      * @throws  SecurityException
3271      *          In the case of the default provider, and a security manager is
3272      *          installed, the {@link SecurityManager#checkRead(String) checkRead}
3273      *          method is invoked to check read access to the file.
3274      *
3275      * @since 11
3276      */
3277     public static String readString(Path path, Charset cs) throws IOException {
3278         Objects.requireNonNull(path);
3279         Objects.requireNonNull(cs);
3280 
3281         byte[] ba = readAllBytes(path);
3282         try {
3283             return JLA.newStringNoRepl(ba, cs);
3284         } catch (IllegalArgumentException e) {
3285             throw new IOException(e);
3286         }
3287     }
3288 
3289     /**
3290      * Read all lines from a file. This method ensures that the file is
3291      * closed when all bytes have been read or an I/O error, or other runtime
3292      * exception, is thrown. Bytes from the file are decoded into characters
3293      * using the specified charset.
3294      *
3295      * <p> This method recognizes the following as line terminators:
3296      * <ul>
3297      *   <li> <code>\u000D</code> followed by <code>\u000A</code>,
3298      *     CARRIAGE RETURN followed by LINE FEED </li>
3299      *   <li> <code>\u000A</code>, LINE FEED </li>
3300      *   <li> <code>\u000D</code>, CARRIAGE RETURN </li>
3301      * </ul>
3302      * <p> Additional Unicode line terminators may be recognized in future
3303      * releases.
3304      *
3305      * <p> Note that this method is intended for simple cases where it is
3306      * convenient to read all lines in a single operation. It is not intended


3617      * @throws  UnsupportedOperationException
3618      *          if an unsupported option is specified
3619      * @throws  SecurityException
3620      *          In the case of the default provider, and a security manager is
3621      *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
3622      *          method is invoked to check write access to the file. The {@link
3623      *          SecurityManager#checkDelete(String) checkDelete} method is
3624      *          invoked to check delete access if the file is opened with the
3625      *          {@code DELETE_ON_CLOSE} option.
3626      *
3627      * @since 11
3628      */
3629     public static Path writeString(Path path, CharSequence csq, Charset cs, OpenOption... options)
3630             throws IOException
3631     {
3632         // ensure the text is not null before opening file
3633         Objects.requireNonNull(path);
3634         Objects.requireNonNull(csq);
3635         Objects.requireNonNull(cs);
3636 
3637         try {
3638             byte[] bytes = JLA.getBytesNoRepl(String.valueOf(csq), cs);
3639             write(path, bytes, options);
3640         } catch (IllegalArgumentException e) {
3641             throw new IOException(e);
3642         }
3643 
3644         return path;
3645     }
3646 
3647     // -- Stream APIs --
3648 
3649     /**
3650      * Return a lazily populated {@code Stream}, the elements of
3651      * which are the entries in the directory.  The listing is not recursive.
3652      *
3653      * <p> The elements of the stream are {@link Path} objects that are
3654      * obtained as if by {@link Path#resolve(Path) resolving} the name of the
3655      * directory entry against {@code dir}. Some file systems maintain special
3656      * links to the directory itself and the directory's parent directory.
3657      * Entries representing these links are not included.
3658      *
3659      * <p> The stream is <i>weakly consistent</i>. It is thread safe but does
3660      * not freeze the directory while iterating, so it may (or may not)
3661      * reflect updates to the directory that occur after returning from this
3662      * method.




3262      *
3263      * @return  a String containing the content read from the file
3264      *
3265      * @throws  IOException
3266      *          if an I/O error occurs reading from the file or a malformed or
3267      *          unmappable byte sequence is read
3268      * @throws  OutOfMemoryError
3269      *          if the file is extremely large, for example larger than {@code 2GB}
3270      * @throws  SecurityException
3271      *          In the case of the default provider, and a security manager is
3272      *          installed, the {@link SecurityManager#checkRead(String) checkRead}
3273      *          method is invoked to check read access to the file.
3274      *
3275      * @since 11
3276      */
3277     public static String readString(Path path, Charset cs) throws IOException {
3278         Objects.requireNonNull(path);
3279         Objects.requireNonNull(cs);
3280 
3281         byte[] ba = readAllBytes(path);

3282         return JLA.newStringNoRepl(ba, cs);



3283     }
3284 
3285     /**
3286      * Read all lines from a file. This method ensures that the file is
3287      * closed when all bytes have been read or an I/O error, or other runtime
3288      * exception, is thrown. Bytes from the file are decoded into characters
3289      * using the specified charset.
3290      *
3291      * <p> This method recognizes the following as line terminators:
3292      * <ul>
3293      *   <li> <code>\u000D</code> followed by <code>\u000A</code>,
3294      *     CARRIAGE RETURN followed by LINE FEED </li>
3295      *   <li> <code>\u000A</code>, LINE FEED </li>
3296      *   <li> <code>\u000D</code>, CARRIAGE RETURN </li>
3297      * </ul>
3298      * <p> Additional Unicode line terminators may be recognized in future
3299      * releases.
3300      *
3301      * <p> Note that this method is intended for simple cases where it is
3302      * convenient to read all lines in a single operation. It is not intended


3613      * @throws  UnsupportedOperationException
3614      *          if an unsupported option is specified
3615      * @throws  SecurityException
3616      *          In the case of the default provider, and a security manager is
3617      *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
3618      *          method is invoked to check write access to the file. The {@link
3619      *          SecurityManager#checkDelete(String) checkDelete} method is
3620      *          invoked to check delete access if the file is opened with the
3621      *          {@code DELETE_ON_CLOSE} option.
3622      *
3623      * @since 11
3624      */
3625     public static Path writeString(Path path, CharSequence csq, Charset cs, OpenOption... options)
3626             throws IOException
3627     {
3628         // ensure the text is not null before opening file
3629         Objects.requireNonNull(path);
3630         Objects.requireNonNull(csq);
3631         Objects.requireNonNull(cs);
3632 

3633         byte[] bytes = JLA.getBytesNoRepl(String.valueOf(csq), cs);
3634         write(path, bytes, options);



3635 
3636         return path;
3637     }
3638 
3639     // -- Stream APIs --
3640 
3641     /**
3642      * Return a lazily populated {@code Stream}, the elements of
3643      * which are the entries in the directory.  The listing is not recursive.
3644      *
3645      * <p> The elements of the stream are {@link Path} objects that are
3646      * obtained as if by {@link Path#resolve(Path) resolving} the name of the
3647      * directory entry against {@code dir}. Some file systems maintain special
3648      * links to the directory itself and the directory's parent directory.
3649      * Entries representing these links are not included.
3650      *
3651      * <p> The stream is <i>weakly consistent</i>. It is thread safe but does
3652      * not freeze the directory while iterating, so it may (or may not)
3653      * reflect updates to the directory that occur after returning from this
3654      * method.


< prev index next >