/** * Append binary data to the file. See {@link #append(Path, java.io.InputStream)} * * @param path a Path * @param data an InputStream of data to write to the file * @return the file * @throws java.io.IOException if an IOException occurs. * @since 2.3.0 */ public static Path leftShift(Path path, InputStream data) throws IOException { append(path, data); return path; }
/** * Converts this Path to a {@link groovy.lang.Writable} or delegates to default * {@link org.codehaus.groovy.runtime.DefaultGroovyMethods#asType(Object, Class)}. * * @param path a Path * @param c the desired class * @return the converted object * @since 2.3.0 */ @SuppressWarnings("unchecked") public static <T> T asType(Path path, Class<T> c) { if (c == Writable.class) { return (T) asWritable(path); } return DefaultGroovyMethods.asType((Object) path, c); }
/** * Append bytes to the end of a Path. It <strong>will not</strong> be * interpreted as text. * * @param self a Path * @param bytes the byte array to append to the end of the Path * @throws java.io.IOException if an IOException occurs. * @since 2.3.0 */ public static void append(Path self, byte[] bytes) throws IOException { OutputStream stream = null; try { stream = Files.newOutputStream(self, CREATE, APPEND); stream.write(bytes, 0, bytes.length); stream.flush(); OutputStream temp = stream; stream = null; temp.close(); } finally { closeWithWarning(stream); } }
return IOGroovyMethods.withStream(newObjectInputStream(self, classLoader), closure); return IOGroovyMethods.splitEachLine(newReader(self), regex, closure); return IOGroovyMethods.readLines(newReader(self)); return IOGroovyMethods.readLines(newReader(self, charset)); return IOGroovyMethods.getText(newReader(self, charset)); return IOGroovyMethods.getText(newReader(self)); return newWriter(self, charset, append, false);
/** * Iterates through this file line by line, splitting each line using * the given regex separator. For each line, the given closure is called with * a single parameter being the list of strings computed by splitting the line * around matches of the given regular expression. * Finally the resources used for processing the file are closed. * * @param self a Path * @param regex the delimiting regular expression * @param charset opens the file with a specified charset * @param closure a closure * @return the last value returned by the closure * @throws java.io.IOException if an IOException occurs. * @throws java.util.regex.PatternSyntaxException if the regular expression's syntax is invalid * @see org.codehaus.groovy.runtime.IOGroovyMethods#splitEachLine(java.io.Reader, String, groovy.lang.Closure) * @since 2.3.0 */ public static <T> T splitEachLine(Path self, String regex, String charset, @ClosureParams(value = FromString.class, options = {"List<String>", "String[]"}, conflictResolutionStrategy = PickFirstResolver.class) Closure<T> closure) throws IOException { return IOGroovyMethods.splitEachLine(newReader(self, charset), regex, closure); }
private static void appendBuffered(Path file, Object text, String charset, boolean writeBom) throws IOException { BufferedWriter writer = null; try { boolean shouldWriteBom = writeBom && !file.toFile().exists(); writer = newWriter(file, charset, true); if (shouldWriteBom) { IOGroovyMethods.writeUTF16BomIfRequired(writer, charset); } InvokerHelper.write(writer, text); writer.flush(); Writer temp = writer; writer = null; temp.close(); } finally { closeWithWarning(writer); } }
/** * Iterates through this file line by line. Each line is passed * to the given 1 or 2 arg closure. The file is read using a reader * which is closed before this method returns. * * @param self a Path * @param charset opens the file with a specified charset * @param firstLine the line number value used for the first line (default is 1, set to 0 to start counting from 0) * @param closure a closure (arg 1 is line, optional arg 2 is line number) * @return the last value returned by the closure * @throws java.io.IOException if an IOException occurs. * @see org.codehaus.groovy.runtime.IOGroovyMethods#eachLine(java.io.Reader, int, groovy.lang.Closure) * @since 2.3.0 */ public static <T> T eachLine(Path self, String charset, int firstLine, @ClosureParams(value = FromString.class, options = {"String", "String,Integer"}) Closure<T> closure) throws IOException { return IOGroovyMethods.eachLine(newReader(self, charset), firstLine, closure); }
/** * Iterates through the given file object by object. * * @param self a {@code Path} object * @param closure a closure * @throws java.io.IOException if an IOException occurs. * @throws ClassNotFoundException if the class is not found. * @see org.codehaus.groovy.runtime.IOGroovyMethods#eachObject(java.io.ObjectInputStream, groovy.lang.Closure) * @since 2.3.0 */ public static void eachObject(Path self, Closure closure) throws IOException, ClassNotFoundException { IOGroovyMethods.eachObject(newObjectInputStream(self), closure); }
/** * Create a new DataInputStream for this file and passes it into the closure. * This method ensures the stream is closed after the closure returns. * * @param self a Path * @param closure a closure * @return the value returned by the closure * @throws java.io.IOException if an IOException occurs. * @see org.codehaus.groovy.runtime.IOGroovyMethods#withStream(java.io.InputStream, groovy.lang.Closure) * @since 2.3.0 */ public static <T> T withDataInputStream(Path self, @ClosureParams(value = SimpleType.class, options = "java.io.DataInputStream") Closure<T> closure) throws IOException { return IOGroovyMethods.withStream(newDataInputStream(self), closure); }
/** * Create a new DataOutputStream for this file and passes it into the closure. * This method ensures the stream is closed after the closure returns. * * @param self a Path * @param closure a closure * @return the value returned by the closure * @throws java.io.IOException if an IOException occurs. * @see org.codehaus.groovy.runtime.IOGroovyMethods#withStream(java.io.OutputStream, groovy.lang.Closure) * @since 2.3.0 */ public static <T> T withDataOutputStream(Path self, @ClosureParams(value = SimpleType.class, options = "java.io.DataOutputStream") Closure<T> closure) throws IOException { return IOGroovyMethods.withStream(newDataOutputStream(self), closure); }
/** * Create a new ObjectOutputStream for this path and then pass it to the * closure. This method ensures the stream is closed after the closure * returns. * * @param self a Path * @param closure a closure * @return the value returned by the closure * @throws java.io.IOException if an IOException occurs. * @see IOGroovyMethods#withStream(java.io.OutputStream, groovy.lang.Closure) * @since 2.3.0 */ public static <T> T withObjectOutputStream(Path self, @ClosureParams(value = SimpleType.class, options = "java.io.ObjectOutputStream") Closure<T> closure) throws IOException { return IOGroovyMethods.withStream(newObjectOutputStream(self), closure); }
for (Path path : stream) { if (Files.isDirectory(path)) { if (!deleteDir(path)) { return false;
/** * Reads the file into a list of Strings, with one item for each line. * * @param self a Path * @param charset opens the file with a specified charset * @return a List of lines * @throws java.io.IOException if an IOException occurs. * @see org.codehaus.groovy.runtime.IOGroovyMethods#readLines(java.io.Reader) * @since 2.3.0 */ public static List<String> readLines(Path self, String charset) throws IOException { return IOGroovyMethods.readLines(newReader(self, charset)); }
/** * Iterates through this file line by line. Each line is passed * to the given 1 or 2 arg closure. The file is read using a reader * which is closed before this method returns. * * @param self a Path * @param firstLine the line number value used for the first line (default is 1, set to 0 to start counting from 0) * @param closure a closure (arg 1 is line, optional arg 2 is line number) * @return the last value returned by the closure * @throws java.io.IOException if an IOException occurs. * @see org.codehaus.groovy.runtime.IOGroovyMethods#eachLine(java.io.Reader, int, groovy.lang.Closure) * @since 2.3.0 */ public static <T> T eachLine(Path self, int firstLine, @ClosureParams(value = FromString.class, options = {"String", "String,Integer"}) Closure<T> closure) throws IOException { return IOGroovyMethods.eachLine(newReader(self), firstLine, closure); }
/** * Create a new ObjectInputStream for this file associated with the given class loader and pass it to the closure. * This method ensures the stream is closed after the closure returns. * * @param self a Path * @param classLoader the class loader to use when loading the class * @param closure a closure * @return the value returned by the closure * @throws java.io.IOException if an IOException occurs. * @see org.codehaus.groovy.runtime.IOGroovyMethods#withStream(java.io.InputStream, groovy.lang.Closure) * @since 2.3.0 */ public static <T> T withObjectInputStream(Path self, ClassLoader classLoader, @ClosureParams(value = SimpleType.class, options = "java.io.ObjectInputStream") Closure<T> closure) throws IOException { return IOGroovyMethods.withStream(newObjectInputStream(self, classLoader), closure); }
/** * Create a new DataInputStream for this file and passes it into the closure. * This method ensures the stream is closed after the closure returns. * * @param self a Path * @param closure a closure * @return the value returned by the closure * @throws java.io.IOException if an IOException occurs. * @see org.codehaus.groovy.runtime.IOGroovyMethods#withStream(java.io.InputStream, groovy.lang.Closure) * @since 2.3.0 */ public static <T> T withDataInputStream(Path self, @ClosureParams(value = SimpleType.class, options = "java.io.DataInputStream") Closure<T> closure) throws IOException { return IOGroovyMethods.withStream(newDataInputStream(self), closure); }
/** * Create a new DataOutputStream for this file and passes it into the closure. * This method ensures the stream is closed after the closure returns. * * @param self a Path * @param closure a closure * @return the value returned by the closure * @throws java.io.IOException if an IOException occurs. * @see org.codehaus.groovy.runtime.IOGroovyMethods#withStream(java.io.OutputStream, groovy.lang.Closure) * @since 2.3.0 */ public static <T> T withDataOutputStream(Path self, @ClosureParams(value = SimpleType.class, options = "java.io.DataOutputStream") Closure<T> closure) throws IOException { return IOGroovyMethods.withStream(newDataOutputStream(self), closure); }
/** * Create a new ObjectOutputStream for this path and then pass it to the * closure. This method ensures the stream is closed after the closure * returns. * * @param self a Path * @param closure a closure * @return the value returned by the closure * @throws java.io.IOException if an IOException occurs. * @see IOGroovyMethods#withStream(java.io.OutputStream, groovy.lang.Closure) * @since 2.3.0 */ public static <T> T withObjectOutputStream(Path self, @ClosureParams(value = SimpleType.class, options = "java.io.ObjectOutputStream") Closure<T> closure) throws IOException { return IOGroovyMethods.withStream(newObjectOutputStream(self), closure); }
for (Path path : stream) { if (Files.isDirectory(path)) { if (!deleteDir(path)) { return false;
/** * Reads the file into a list of Strings, with one item for each line. * * @param self a Path * @return a List of lines * @throws java.io.IOException if an IOException occurs. * @see org.codehaus.groovy.runtime.IOGroovyMethods#readLines(java.io.Reader) * @since 2.3.0 */ public static List<String> readLines(Path self) throws IOException { return IOGroovyMethods.readLines(newReader(self)); }