/** * Reads lines from source files. */ public static String[] readLines(File file, String encoding) throws IOException { if (!file.exists()) { throw new FileNotFoundException(MSG_NOT_FOUND + file); } if (!file.isFile()) { throw new IOException(MSG_NOT_A_FILE + file); } List<String> list = new ArrayList<>(); InputStream in = null; try { in = new FileInputStream(file); if (encoding.startsWith("UTF")) { in = new UnicodeInputStream(in, encoding); } BufferedReader br = new BufferedReader(new InputStreamReader(in, encoding)); String strLine; while ((strLine = br.readLine()) != null) { list.add(strLine); } } finally { StreamUtil.close(in); } return list.toArray(new String[list.size()]); }
public MorfetteFileIterator(String filename) { try { reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "UTF-8")); primeNext(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } }
public static void copyfile(File input, File output) { try { InputStream in = new FileInputStream(input); OutputStream out = new FileOutputStream(output); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); } catch (FileNotFoundException ex) { System.out .println(ex.getMessage() + " in the specified directory."); System.exit(0); } catch (IOException e) { System.out.println(e.getMessage()); } } }
private Set<String> loadMWEs() { Set<String> mweSet = Generics.newHashSet(); try { BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(mweFile), "UTF-8")); for (String line; (line = br.readLine()) != null;) { mweSet.add(line.trim()); } br.close(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return mweSet; }
FileNotFoundException fnf = new FileNotFoundException(fileName); ThrowableUtil.addSuppressedAndClear(fnf, suppressed); throw fnf; FileNotFoundException fnf = new FileNotFoundException(path); ThrowableUtil.addSuppressedAndClear(fnf, suppressed); throw fnf; in = url.openStream(); out = new FileOutputStream(tmpFile); if (TRY_TO_PATCH_SHADED_ID && PlatformDependent.isOsx() && !packagePrefix.isEmpty()) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(in.available()); while ((length = in.read(buffer)) > 0) { byteArrayOutputStream.write(buffer, 0, length); out.write(bytes); } else { while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); out.flush();
@ExpectWarning("RE,RV") public static void main(String[] args) throws IOException { String name = "Mr. Ed"; name = name.replaceAll(".", "s."); System.out.println(name); // FIXME:FindBugs only catches this error with name.indexOf(String) if (name.indexOf("s") > 0) System.out.println("Yay"); else System.out.println("Boo"); String result; try { BufferedReader findFiles = new BufferedReader(new FileReader("/mainList.txt")); if (findFiles.readLine() != null) result = findFiles.readLine(); findFiles.close(); } catch (FileNotFoundException e) { System.exit(7); e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } LineNumberReader tmp = new LineNumberReader(new FileReader("/mainList.txt")); int count = 0; while (tmp.readLine() != null) count++; tmp.close(); }
private static void writeFromJarToTempFile(String path, File temp) throws IOException { // Prepare buffer for data copying byte[] buffer = new byte[1024]; int readBytes; // Open and check input stream InputStream is = SigarNativeBindingLoader.class.getResourceAsStream(path); if (is == null) { throw new FileNotFoundException("File " + path + " was not found inside JAR."); } // Open output stream and copy data between source file in JAR and the temporary file OutputStream os = new FileOutputStream(temp); try { while ((readBytes = is.read(buffer)) != -1) { os.write(buffer, 0, readBytes); } } finally { // If read/write fails, close streams safely before throwing an exception os.close(); is.close(); } } }
throw new FileNotFoundException("File not found: " + file); long len = file.length(); if (len < BLOCK_LEN + HEADER_LEN + skip) InputStream in = null; try { in = new FileInputStream(file); if (skip > 0) DataHelper.skip(in, skip); return getComment(buffer); } finally { if (in != null) try { in.close(); } catch (IOException ioe) {}
/** * read the content of znode file, expects a single line. */ public static String readMyEphemeralNodeOnDisk() throws IOException { String fileName = getMyEphemeralNodeFileName(); if (fileName == null){ throw new FileNotFoundException("No filename; set environment variable HBASE_ZNODE_FILE"); } FileReader znodeFile = new FileReader(fileName); BufferedReader br = null; try { br = new BufferedReader(znodeFile); String file_content = br.readLine(); return file_content; } finally { if (br != null) br.close(); } }
public static File extractToTmp(String resourcePath, String tmpPrefix, Class clazz) throws BrutException { try { InputStream in = clazz.getResourceAsStream(resourcePath); if (in == null) { throw new FileNotFoundException(resourcePath); } File fileOut = File.createTempFile(tmpPrefix, null); fileOut.deleteOnExit(); OutputStream out = new FileOutputStream(fileOut); IOUtils.copy(in, out); in.close(); out.close(); return fileOut; } catch (IOException ex) { throw new BrutException("Could not extract resource: " + resourcePath, ex); } } }
BufferedReader input = null; try { fin = new FileReader(file); input = new BufferedReader(fin); String line; StringBuilder contents = new StringBuilder(); String EOL = System.getProperty("line.separator"); while ((line = input.readLine()) != null) { contents.append(line).append(EOL); } catch (java.io.FileNotFoundException e) { LOGGER.log(Level.WARNING, "failed to find file: {0}", e.getMessage()); } catch (java.io.IOException e) { LOGGER.log(Level.WARNING, "failed to read file: {0}", if (input != null) { try { input.close(); } catch (Exception e) { fin.close(); } catch (Exception e) {
if ( source == null ) throw new FileNotFoundException( "Could not find resource '" + resource + "' to unzip" ); try ( OutputStream file = new BufferedOutputStream( new FileOutputStream( new File( targetDirectory, entry.getName() ) ) ) ) file.write( scratch, 0, read ); toCopy -= read; source.close();
InputStream inputStream = null; try { inputStream = new FileInputStream(filePath); BitmapFactory.decodeStream(inputStream, null, options); inputStream.close(); } catch (FileNotFoundException exception) { exception.printStackTrace(); } catch (IOException exception) { exception.printStackTrace(); InputStream inputStream = null; try { inputStream = new FileInputStream(filePath); BitmapFactory.decodeStream(inputStream, null, options); inputStream.close(); } catch (IOException exception) { exception.printStackTrace();
if (file.exists()) { try { in = new FileInputStream(file); properties = new Properties(); properties.load(in); loadProperties(properties); } catch (FileNotFoundException e) { LogUtil.writeErrorLog(e.getMessage(), e); } catch (IOException e) { LogUtil.writeErrorLog(e.getMessage(), e); if (null != in) { try { in.close(); } catch (IOException e) { LogUtil.writeErrorLog(e.getMessage(), e);
public Reader reader() throws IOException { final InputStream inputStream = this.servletContext.getResourceAsStream(this.path); if (inputStream == null) { throw new FileNotFoundException(String.format("ServletContext resource \"%s\" does not exist", this.path)); } if (!StringUtils.isEmptyOrWhitespace(this.characterEncoding)) { return new BufferedReader(new InputStreamReader(new BufferedInputStream(inputStream), this.characterEncoding)); } return new BufferedReader(new InputStreamReader(new BufferedInputStream(inputStream))); }
public static File from(Context context, Uri uri) throws IOException { InputStream inputStream = context.getContentResolver().openInputStream(uri); String fileName = getFileName(context, uri); String[] splitName = splitFileName(fileName); File tempFile = File.createTempFile(splitName[0], splitName[1]); tempFile = rename(tempFile, fileName); tempFile.deleteOnExit(); FileOutputStream out = null; try { out = new FileOutputStream(tempFile); } catch (FileNotFoundException e) { e.printStackTrace(); } if (inputStream != null) { copy(inputStream, out); inputStream.close(); } if (out != null) { out.close(); } return tempFile; }
public SequenceReader(String file,boolean hasTarget, String charsetName) { this.hasTarget = hasTarget; try { reader = new BufferedReader(new UnicodeReader( new FileInputStream(file), charsetName)); } catch (FileNotFoundException e) { e.printStackTrace(); } }
fis = new FileInputStream(file); } catch (final FileNotFoundException fnfe) { fis = null; if (file.exists()) { try { fis = new FileInputStream(file); filename = baseName + extension; break openStream; throw new FileNotFoundException("Unable to locate file " + originalFile); final String serializationName; try { bufferedInStream.mark(4096); final InputStream in = filename.endsWith(".gz") ? new GZIPInputStream(bufferedInStream) : bufferedInStream; final DataInputStream dis = new DataInputStream(in); serializationName = dis.readUTF(); bufferedInStream.reset(); } catch (final EOFException eof) { fis.close(); return new EmptyRecordReader(); throw new FileNotFoundException("Cannot create TOC Reader because the file " + tocFile + " does not exist"); throw new FileNotFoundException("Cannot create TOC Reader because the file " + tocFile + " does not exist");
public static void Writer(String path, String charEncoding, String content) { OutputStream fos = null; try { fos = new FileOutputStream(new File(path)); fos.write(content.getBytes(charEncoding)); fos.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { close(fos); } }
/** * Resolve the given resource URL to a {@code java.io.File}, * i.e. to a file in the file system. * @param resourceUrl the resource URL to resolve * @param description a description of the original resource that * the URL was created for (for example, a class path location) * @return a corresponding File object * @throws FileNotFoundException if the URL cannot be resolved to * a file in the file system */ public static File getFile(URL resourceUrl, String description) throws FileNotFoundException { Assert.notNull(resourceUrl, "Resource URL must not be null"); if (!URL_PROTOCOL_FILE.equals(resourceUrl.getProtocol())) { throw new FileNotFoundException( description + " cannot be resolved to absolute file path " + "because it does not reside in the file system: " + resourceUrl); } try { return new File(toURI(resourceUrl).getSchemeSpecificPart()); } catch (URISyntaxException ex) { // Fallback for URLs that are not valid URIs (should hardly ever happen). return new File(resourceUrl.getFile()); } }