/** * Returns all the text in the given file * * @return The text in the file. */ public static String slurpFile(String filename) throws IOException { return slurpFile(filename, defaultEncoding); }
public void serializeWeights(String serializePath) { ObjectOutputStream oos = null; try { oos = IOUtils.writeStreamFromString(serializePath); oos.writeObject(weights); log.info("Serializing weights to " + serializePath + "... done."); } catch (Exception e) { log.info("Serializing weights to " + serializePath + "... FAILED.", e); } finally { IOUtils.closeIgnoringExceptions(oos); } }
/** * Get a input file stream (automatically gunzip/bunzip2 depending on file extension) * @param filename Name of file to open * @return Input stream that can be used to read from the file * @throws IOException if there are exceptions opening the file */ public static InputStream getFileInputStream(String filename) throws IOException { InputStream in = new FileInputStream(filename); if (filename.endsWith(".gz")) { in = new GZIPInputStream(in); } else if (filename.endsWith(".bz2")) { //in = new CBZip2InputStream(in); in = getBZip2PipedInputStream(filename); } return in; }
private InputStream getStream() throws IOException { if (file != null) { return inputStreamFromFile(file); } else if (path != null) { return getInputStreamFromURLOrClasspathOrFileSystem(path); } else { throw new AssertionError("No known path to read"); } }
public static LinkedList<String[]> readCSVStrictly(String filename, int numColumns) throws IOException { return readCSVStrictly(slurpFile(filename).toCharArray(), numColumns); }
public static ClassifierCombiner getClassifier(String loadPath, Properties props) throws IOException, ClassNotFoundException, ClassCastException { ObjectInputStream ois = IOUtils.readStreamFromString(loadPath); ClassifierCombiner returnCC = getClassifier(ois, props); IOUtils.closeIgnoringExceptions(ois); return returnCC; }
return (E) IOUtils.getBufferedReaderFromClasspathOrFileSystem(value); } catch (IOException e) { throw new RuntimeIOException(e); if (value.equalsIgnoreCase("stderr") || value.equalsIgnoreCase("err")) { return (E) new PrintWriter(System.err); } try { return (E) IOUtils.getPrintWriter(value); } catch (IOException e) { throw new RuntimeException(e); throw new IllegalStateException("Could not create output stream (cannot write file): " + value); return (E) IOUtils.getFileOutputStream(value); } catch (IOException e) { throw new RuntimeException(e); return (E) IOUtils.getInputStreamFromURLOrClasspathOrFileSystem(value); } catch (IOException e) { throw new RuntimeException(e);
private ScoredParsesIterator(String filename) throws IOException { this(filename, IOUtils.getBufferedFileReader(filename)); }
public static PrintWriter getPrintWriterIgnoringExceptions(String filename) { try { return getPrintWriter(filename, defaultEncoding); } catch (IOException ioe) { return null; } }
public static void processTextFile(AnnotationPipeline pipeline, String in, String out, String date) throws IOException { String text = IOUtils.slurpFile(in); PrintWriter pw = (out != null)? IOUtils.getPrintWriter(out):new PrintWriter(System.out); String string = textToAnnotatedXml(pipeline, text, date); pw.println(string); pw.flush(); if (out != null) pw.close(); }
private static Stream<SentimentDatum> imdb(String path, SentimentClass label) { return StreamSupport.stream( IOUtils.iterFilesRecursive(new File(path)).spliterator(), true) .map(x -> { try { return new SentimentDatum(IOUtils.slurpFile(x), label); } catch (IOException e) { throw new RuntimeIOException(e); } }); }
private static Stream<SentimentDatum> unlabelled(String path) throws IOException { return StreamSupport.stream( IOUtils.iterFilesRecursive(new File(path)).spliterator(), true) .flatMap(x -> new Document(IOUtils.slurpReader(IOUtils.readerFromFile(x))) .sentences() .stream() .map(y -> new SentimentDatum(y.text(), SentimentClass.NEUTRAL))); }
/** * Iterate over all the files in the directory, recursively. * * @param dir The root directory. * @return All files within the directory. */ public static Iterable<File> iterFilesRecursive(final File dir) { return iterFilesRecursive(dir, (Pattern) null); }
public static DataInputStream getDataInputStream(String filenameUrlOrClassPath) throws IOException { return new DataInputStream(getInputStreamFromURLOrClasspathOrFileSystem(filenameUrlOrClassPath)); }
public static OutputStream getFileOutputStream(String filename, boolean append) throws IOException { OutputStream out = new FileOutputStream(filename, append); if (filename.endsWith(".gz")) { out = new GZIPOutputStream(out); } else if (filename.endsWith(".bz2")) { //out = new CBZip2OutputStream(out); out = getBZip2PipedOutputStream(filename); } return out; }
@Override public Annotation createFromFile(String filename) throws IOException { Reader r = IOUtils.getBufferedFileReader(filename); Annotation anno = create(r); IOUtils.closeIgnoringExceptions(r); return anno; }
public static PrintWriter getPrintWriter(String filename, String encoding) throws IOException { OutputStream out = getFileOutputStream(filename); if (encoding == null) { encoding = defaultEncoding; } return new PrintWriter(new BufferedWriter(new OutputStreamWriter(out, encoding)), true); }
} else { try { in = findStreamInClasspathOrFileSystem(textFileOrUrl); } catch (FileNotFoundException e) { try {
public void close() { IOUtils.closeIgnoringExceptions(docIterator); }
for (File f : iterFilesRecursive(file)) { count += 1; size += f.length(); if (children != null) { for (File child : children) { deleteRecursively(child);