List of usage examples for java.io FileNotFoundException printStackTrace
public void printStackTrace()
From source file:Main.java
public static List<String> getTextToList(File file) { FileInputStream fileInputStream = null; InputStreamReader inputStreamReader = null; BufferedReader bufferedReader = null; List<String> list = new ArrayList<String>(); try {//w w w .j a v a 2s .c o m fileInputStream = new FileInputStream(file); inputStreamReader = new InputStreamReader(fileInputStream); bufferedReader = new BufferedReader(inputStreamReader); String text; while ((text = bufferedReader.readLine()) != null) { list.add(text); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fileInputStream != null) { fileInputStream.close(); } if (inputStreamReader != null) { inputStreamReader.close(); } if (bufferedReader != null) { bufferedReader.close(); } } catch (IOException e) { e.printStackTrace(); } } return list; }
From source file:Main.java
public static Bitmap getSimilarBitmap(File f, int width, int height) { try {/*from ww w .j a v a 2 s. c o m*/ BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; FileInputStream stream1 = new FileInputStream(f); BitmapFactory.decodeStream(stream1, null, o); stream1.close(); float widthScale = o.outWidth / width; float heightScale = o.outHeight / height; float scale = widthScale > heightScale ? widthScale : heightScale; if (scale > 8) { o.inSampleSize = 8; } else if (scale > 6) { o.inSampleSize = 6; } else if (scale > 4) { o.inSampleSize = 4; } else if (scale > 2) { o.inSampleSize = 2; } else { o.inSampleSize = 1; } o.inJustDecodeBounds = false; FileInputStream stream2 = new FileInputStream(f); Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o); stream2.close(); return bitmap; } catch (FileNotFoundException e) { } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static void saveImage(String imagePath, Bitmap bm) { if (bm == null || imagePath == null || "".equals(imagePath)) { return;/* w w w. j a va2s.c o m*/ } File f = new File(imagePath); if (f.exists()) { return; } else { try { File parentFile = f.getParentFile(); if (!parentFile.exists()) { parentFile.mkdirs(); } f.createNewFile(); FileOutputStream fos; fos = new FileOutputStream(f); bm.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.close(); } catch (FileNotFoundException e) { f.delete(); e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); f.delete(); } } }
From source file:Main.java
@SuppressWarnings("unchecked") static boolean loadSharedPreferencesFromFile(Context context, File src) { boolean res = false; ObjectInputStream input = null; try {//from w w w. ja va 2 s . co m GZIPInputStream inputGZIP = new GZIPInputStream(new FileInputStream(src)); input = new ObjectInputStream(inputGZIP); Editor prefEdit = PreferenceManager.getDefaultSharedPreferences(context).edit(); prefEdit.clear(); Map<String, Object> entries = (Map<String, Object>) input.readObject(); for (Entry<String, ?> entry : entries.entrySet()) { Object v = entry.getValue(); String key = entry.getKey(); if (v instanceof Boolean) prefEdit.putBoolean(key, ((Boolean) v).booleanValue()); else if (v instanceof Float) prefEdit.putFloat(key, ((Float) v).floatValue()); else if (v instanceof Integer) prefEdit.putInt(key, ((Integer) v).intValue()); else if (v instanceof Long) prefEdit.putLong(key, ((Long) v).longValue()); else if (v instanceof String) prefEdit.putString(key, ((String) v)); } prefEdit.commit(); res = true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { try { if (input != null) { input.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return res; }
From source file:Main.java
/** * read file to a string// w w w. j av a2 s .c om * * @param context * @param file * @return */ public static String loadString(File file) { if (null == file || !file.exists()) { return ""; } FileInputStream fis = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { fis = new FileInputStream(file); int restSize = fis.available(); int bufSize = restSize > BUF_SIZE ? BUF_SIZE : restSize; byte[] buf = new byte[bufSize]; while (fis.read(buf) != -1) { baos.write(buf); restSize -= bufSize; if (restSize <= 0) break; if (restSize < bufSize) bufSize = restSize; buf = new byte[bufSize]; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } return baos.toString(); }
From source file:Main.java
public static Bitmap convertToMutable(Bitmap srcBitmap, String cacheDirPath, String tempFileName) { try {/*from w w w . ja v a2 s . c o m*/ // this is the file going to use temporally to save the bytes. // This file will not be a image, it will store the raw image data. int index = tempFileName.lastIndexOf("."); if (index != -1) tempFileName = tempFileName.substring(0, index); File file = new File(cacheDirPath + File.separator + tempFileName + ".tmp"); // Open an RandomAccessFile // Make sure you have added uses-permission // android:name="android.permission.WRITE_EXTERNAL_STORAGE" // into AndroidManifest.xml file RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); // get the width and height of the source bitmap. int width = srcBitmap.getWidth(); int height = srcBitmap.getHeight(); Config type = srcBitmap.getConfig(); // Copy the byte to the file // Assume source bitmap loaded using options.inPreferredConfig = // Config.ARGB_8888; FileChannel channel = randomAccessFile.getChannel(); MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0, srcBitmap.getRowBytes() * height); srcBitmap.copyPixelsToBuffer(map); // recycle the source bitmap, this will be no longer used. srcBitmap.recycle(); System.gc();// try to force the bytes from the imgIn to be released // Create a new bitmap to load the bitmap again. Probably the memory // will be available. srcBitmap = Bitmap.createBitmap(width, height, type); map.position(0); // load it back from temporary srcBitmap.copyPixelsFromBuffer(map); // close the temporary file and channel , then delete that also channel.close(); randomAccessFile.close(); // delete the temp file file.delete(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return srcBitmap; }
From source file:com.gemalto.split.additionalservices.ViveService_RemoveRecordsFromCsvFile.java
public static void loadExceptionRecords(String exceptionFileName) { logger.info("Loading Records of the Exception File"); BufferedReader br = null;/*w w w . ja v a2s . c o m*/ String line = ""; String cvsSplitBy = ","; try { br = new BufferedReader(new FileReader(exceptionFileName)); while ((line = br.readLine()) != null) { // use comma as separator String[] exceptionRecordsArray = line.split(cvsSplitBy); Record exceptionRecord = new Record(); exceptionRecord.setPan(exceptionRecordsArray[1]); exceptionRecords.add(exceptionRecord); } logger.info("Loading complete, total of exception records:" + exceptionRecords.size() + " (be careful with the header of the csv file)"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:Main.java
public static void base64ToBitmap(String base64Data, String imgName, String imgFormat) { byte[] bytes = Base64.decode(base64Data, Base64.DEFAULT); Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); File myCaptureFile = new File("/sdcard/", imgName); FileOutputStream fos = null;//from w w w .j a v a 2 s .c o m try { fos = new FileOutputStream(myCaptureFile); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } boolean isTu = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); if (isTu) { // fos.notifyAll(); try { fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } else { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:com.cslc.eils.gameControl.util.PropertiesUtil.java
/** * /*from ww w .java2 s . c o m*/ * * @param propFileUrl * * @return */ public static Properties loadProperties(String propFileUrl) { log.info(":" + propFileUrl); InputStream in = null; try { File path = new File(propFileUrl); if (!path.exists()) { // TODO Throws exp } else { Properties properties = new Properties(); in = new FileInputStream(path); properties.load(in); return properties; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } } return null; }
From source file:Main.java
public static String getStringFromFile(String filePath) { String s = null;/*from ww w . j ava 2 s . c o m*/ File f = new File(filePath); Reader reader = null; try { reader = new BufferedReader(new FileReader(f)); StringBuilder sb = new StringBuilder(); char[] buff = new char[50]; int length; while ((length = reader.read(buff)) != -1) { sb.append(buff, 0, length); } s = sb.toString(); } catch (FileNotFoundException e) { s = null; } catch (IOException e) { e.printStackTrace(); } return s; }