List of usage examples for java.io FileNotFoundException printStackTrace
public void printStackTrace()
From source file:Main.java
/** * Convert an image to a byte array//from w ww .ja v a2 s . co m * @param uri * @param context * @return */ public static byte[] convertImageToByte(Uri uri, Context context) { byte[] data = null; try { ContentResolver cr = context.getContentResolver(); InputStream inputStream = cr.openInputStream(uri); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); data = baos.toByteArray(); if (inputStream != null) { try { inputStream.close(); } catch (Exception e2) { } } if (baos != null) { try { baos.close(); } catch (Exception e2) { } } } catch (FileNotFoundException e) { e.printStackTrace(); } return data; }
From source file:Main.java
static Document parseFile(String fname) { InputStream in = null;/*from w w w .ja v a 2 s . c o m*/ try { in = new FileInputStream(fname); } catch (FileNotFoundException e) { e.printStackTrace(); System.exit(1); } return parseInputStream(in); }
From source file:Main.java
public static byte[] getBytesFromFile(File file) { byte[] buffer = null; try {//from w w w . jav a 2 s. c o m if (file.exists()) { FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(1000); byte[] b = new byte[1000]; int n; while ((n = fis.read(b)) != -1) { bos.write(b, 0, n); } fis.close(); bos.close(); buffer = bos.toByteArray(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return buffer; }
From source file:Main.java
public static final Object RestoreObject(final String path) { FileInputStream fileInputStream = null; ObjectInputStream objectInputStream = null; Object object = null;//from w w w. ja va 2 s .c o m File file = new File(path); if (!file.exists()) { return null; } try { fileInputStream = new FileInputStream(file); objectInputStream = new ObjectInputStream(fileInputStream); object = objectInputStream.readObject(); return object; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { try { if (objectInputStream != null) { objectInputStream.close(); } if (fileInputStream != null) { fileInputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } return object; }
From source file:com.netpet.spools.spring.springcase.ActionFactory.java
public static Action getAction(String actionName) { Properties pro = new Properties(); try {//w ww. j a va 2 s . c om pro.load(new FileInputStream(System.getProperty("user.dir") + "/springTest/" + "config.properties")); String actionImplName = (String) pro.get(actionName); String actionMessage = (String) pro.get(actionName + "_msg"); Object obj = Class.forName(actionImplName).newInstance(); BeanUtils.setProperty(obj, "message", actionMessage); return (Action) obj; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static void copyfile(String fromFilePath, String toFilePath, Boolean rewrite) { File fromFile = new File(fromFilePath); File toFile = new File(toFilePath); if (!fromFile.exists() || !fromFile.isFile() || !fromFile.canRead()) { return;//w ww . ja va 2 s .c om } if (!toFile.getParentFile().exists()) { toFile.getParentFile().mkdirs(); } if (toFile.exists() && rewrite) { toFile.delete(); } try { FileInputStream fosfrom = new FileInputStream(fromFile); FileOutputStream fosto = new FileOutputStream(toFile); byte[] bt = new byte[1024]; int c; while ((c = fosfrom.read(bt)) > 0) { fosto.write(bt, 0, c); } fosfrom.close(); fosto.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:Main.java
public static String readAssets(Context context, String fileName) { InputStream is = null;//from w w w.j av a 2 s .c om String content = null; try { is = context.getAssets().open(fileName); if (is != null) { byte[] buffer = new byte[1024]; ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); while (true) { int readLength = is.read(buffer); if (readLength == -1) break; arrayOutputStream.write(buffer, 0, readLength); } is.close(); arrayOutputStream.close(); content = new String(arrayOutputStream.toByteArray()); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); content = null; } finally { try { if (is != null) is.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } return content; }
From source file:Main.java
public static void copyFile(File targetFile, File file) { if (targetFile.exists()) { return;/*ww w . ja va2s . c o m*/ } else { createFile(targetFile, true); } try { FileInputStream is = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(targetFile); byte[] buffer = new byte[1024 * 5]; int len; while ((len = is.read(buffer)) != -1) { fos.write(buffer, 0, len); } is.close(); fos.flush(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:Main.java
public static byte[] getBytesFromFile(String fileFullPath) { byte[] buffer = null; try {/*from w w w. j a v a 2 s. c o m*/ File file = new File(fileFullPath); if (file.exists()) { FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(1000); byte[] b = new byte[1000]; int n; while ((n = fis.read(b)) != -1) { bos.write(b, 0, n); } fis.close(); bos.close(); buffer = bos.toByteArray(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return buffer; }
From source file:Main.java
public static Boolean writeToSDFile(String directory, String file_name, String text) { // Find the root of the external storage. // See/*from ww w .j a va2 s . c o m*/ // http://developer.android.com/guide/topics/data/data-storage.html#filesExternal File root = Environment.getExternalStorageDirectory(); // See // http://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder File dir = new File(root.getAbsolutePath() + "/" + directory); dir.mkdirs(); File file = new File(dir, file_name); try { FileOutputStream f = new FileOutputStream(file); PrintWriter pw = new PrintWriter(f); pw.println(text); pw.flush(); pw.close(); f.close(); // Log.v(TAG, "file written to sd card"); return true; } catch (FileNotFoundException e) { e.printStackTrace(); // Log.i(TAG, "******* File not found. Did you" + // " add a WRITE_EXTERNAL_STORAGE permission to the manifest?"); return false; } catch (IOException e) { e.printStackTrace(); return false; } }