FileInputStream inputStream = new FileInputStream("foo.txt"); try { String everything = IOUtils.toString(inputStream); } finally { inputStream.close(); }
FileInputStream fis = new FileInputStream(new File("foo")); String md5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(fis); fis.close()
public static String readTxt(File file, String charsetName) throws IOException { FileInputStream is = new FileInputStream(file); byte[] targetArray = new byte[is.available()]; int len; int off = 0; while ((len = is.read(targetArray, off, targetArray.length - off)) != -1 && off < targetArray.length) { off += len; } is.close(); return new String(targetArray, charsetName); }
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath(); String fileName = "myFile.txt"; // Not sure if the / is on the path or not File f = new File(baseDir + File.Separator + fileName); FileInputStream fiStream = new FileInputStream(f); byte[] bytes; // You might not get the whole file, lookup File I/O examples for Java fiStream.read(bytes); fiStream.close();
@ExpectWarning("NP") public void method3() throws IOException { Properties pro = new Properties(); FileInputStream fis = null; try { fis = new FileInputStream("/myFile.properties"); pro.load(fis); } finally { fis.close(); System.out.println("FILEINPUTSTREAM_CLOSED_SUCCESSFULLY"); } } @ExpectWarning("NP")
@Test public void gifDrawableCreatedFromInputStream() throws Exception { final AssetFileDescriptor assetFileDescriptor = InstrumentationRegistry.getInstrumentation() .getContext().getResources().openRawResourceFd(R.raw.test); final byte[] buffer = new byte[(int) assetFileDescriptor.getDeclaredLength()]; final FileInputStream inputStream = assetFileDescriptor.createInputStream(); final int bufferedByteCount = inputStream.read(buffer); inputStream.close(); assetFileDescriptor.close(); assertThat(bufferedByteCount).isEqualTo(buffer.length); final InputStream responseStream = new ByteArrayInputStream(buffer); final GifDrawable gifDrawable = new GifDrawable(responseStream); assertThat(gifDrawable.getError()).isEqualTo(GifError.NO_ERROR); assertThat(gifDrawable.getIntrinsicWidth()).isEqualTo(278); assertThat(gifDrawable.getIntrinsicHeight()).isEqualTo(183); } }
File file = new File("a.txt"); FileInputStream fis = new FileInputStream(file); byte[] data = new byte[(int) file.length()]; fis.read(data); fis.close(); String str = new String(data, "UTF-8");
public static String readFile(String path) throws IOException { FileInputStream stream = new FileInputStream(new File(path)); try { FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); return Charset.defaultCharset().decode(bb).toString(); } finally { stream.close(); } }
FileInputStream fis = new FileInputStream("awesomefile.csv"); InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); CSVReader reader = new CSVReader(isr); for (String[] row; (row = reader.readNext()) != null;) { System.out.println(Arrays.toString(row)); } reader.close(); isr.close(); fis.close();
int length = (int) file.length(); byte[] bytes = new byte[length]; FileInputStream in = new FileInputStream(file); try { in.read(bytes); } finally { in.close(); } String contents = new String(bytes);
public static byte[] readAllBytes(String filename) throws IOException { FileInputStream is = new FileInputStream(filename); try { return DaoUtil.readAllBytes(is); } finally { is.close(); } }
public void copy(File src, File dst) throws IOException { FileInputStream inStream = new FileInputStream(src); FileOutputStream outStream = new FileOutputStream(dst); FileChannel inChannel = inStream.getChannel(); FileChannel outChannel = outStream.getChannel(); inChannel.transferTo(0, inChannel.size(), outChannel); inStream.close(); outStream.close(); }
http://www.java2s.com/Code/Java/File-Input-Output/ConvertInputStreamtoString.htm public static String convertStreamToString(InputStream is) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line).append("\n"); } reader.close(); return sb.toString(); } public static String getStringFromFile (String filePath) throws Exception { File fl = new File(filePath); FileInputStream fin = new FileInputStream(fl); String ret = convertStreamToString(fin); //Make sure you close all streams. fin.close(); return ret; }
FileInputStream fis = new FileInputStream("test/offending_bom.txt"); UnicodeBOMInputStream ubis = new UnicodeBOMInputStream(fis); System.out.println("detected BOM: " + ubis.getBOM()); BufferedReader br = new BufferedReader(isr); System.out.println(br.readLine()); fis.close(); fis = new FileInputStream("test/offending_bom.txt"); ubis = new UnicodeBOMInputStream(fis); isr = new InputStreamReader(ubis); System.out.println(br.readLine()); fis.close();
private byte[] CopySdcardbytes(File file) throws IOException { FileInputStream in = new FileInputStream(file); ByteArrayOutputStream out = new ByteArrayOutputStream(1024); byte[] temp = new byte[1024]; int size = 0; while ((size = in.read(temp)) != -1) { out.write(temp, 0, size); } in.close(); byte[] bytes = out.toByteArray(); return bytes; }
public static byte[] readAllBytes(File file) throws IOException { FileInputStream is = new FileInputStream(file); try { return DaoUtil.readAllBytes(is); } finally { is.close(); } }
FileInputStream in = new FileInputStream("First.properties"); Properties props = new Properties(); props.load(in); in.close(); FileOutputStream out = new FileOutputStream("First.properties"); props.setProperty("country", "america"); props.store(out, null); out.close();
public static <T> T getYamlDataObject(String path, Class<T> clazz) { Yaml ymal = new Yaml(); FileInputStream fis = null; try { fis = new FileInputStream(new File(path)); return ymal.loadAs(fis, clazz); } catch (Exception e) { throw new AclException(String.format("The file for Plain mode was not found , paths %s", path), e); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { throw new AclException("close transport fileInputStream Exception", e); } } } }
private static void loadSettings() { FileInputStream st = null; try { st = new FileInputStream(PROPERTIES_FILE_NAME); PROPERTIES.load(st); } catch (final IOException e) { // ignore } finally { if (st != null) { try { st.close(); } catch (final IOException ex) { // ignore } } } for (final String name : new String[]{PROPERTY_CONSUMER_KEY, PROPERTY_CONSUMER_SECRET, PROPERTY_TOKEN, PROPERTY_TOKEN_SECRET}) { final String value = System.getProperty(name); if (value != null) { PROPERTIES.setProperty(name, value); } } if (PROPERTIES.getProperty(PROPERTY_CONSUMER_KEY) == null || PROPERTIES.getProperty(PROPERTY_CONSUMER_SECRET) == null) { System.out.println("No consumerKey and/or consumerSecret found in twitterclient.properties file. " + "You have to provide these as system properties. See README.html for more information."); System.exit(1); } }
OutputStream out = response.getOutputStream(); FileInputStream in = new FileInputStream(my_file); byte[] buffer = new byte[4096]; int length; while ((length = in.read(buffer)) > 0){ out.write(buffer, 0, length); } in.close(); out.flush();