List of usage examples for java.io FileNotFoundException printStackTrace
public void printStackTrace()
From source file:Main.java
public static String readFile(Context context, String fileName) { if (!exists(context, fileName)) { return null; }/*from ww w . j av a 2 s .c o m*/ FileInputStream fis = null; String content = null; try { fis = context.openFileInput(fileName); if (fis != null) { byte[] buffer = new byte[1024]; ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); while (true) { int readLength = fis.read(buffer); if (readLength == -1) break; arrayOutputStream.write(buffer, 0, readLength); } fis.close(); arrayOutputStream.close(); content = new String(arrayOutputStream.toByteArray()); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); content = null; } finally { try { if (fis != null) fis.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } return content; }
From source file:Main.java
public static String readFile(String filePath) { if (filePath == null || !new File(filePath).exists()) { return null; }/* ww w.java2 s . c o m*/ FileInputStream fis = null; String content = null; try { fis = new FileInputStream(filePath); if (fis != null) { byte[] buffer = new byte[1024]; ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); while (true) { int readLength = fis.read(buffer); if (readLength == -1) break; arrayOutputStream.write(buffer, 0, readLength); } fis.close(); arrayOutputStream.close(); content = new String(arrayOutputStream.toByteArray()); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); content = null; } finally { try { if (fis != null) fis.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } return content; }
From source file:Main.java
public static boolean saveMyBitmap(File f, Bitmap mBitmap) throws IOException { boolean saveComplete = true; try {/*from www . j a v a 2 s . co m*/ f.createNewFile(); FileOutputStream fOut = null; fOut = new FileOutputStream(f); int width = mBitmap.getWidth(); int height = mBitmap.getHeight(); int finalWidth = 800; int finalHeight = (int) (finalWidth * 1.0 * (height * 1.0 / width * 1.0)); double x = width * finalHeight; double y = height * finalWidth; if (x > y) { finalHeight = (int) (y / (double) width); } else if (x < y) { finalWidth = (int) (x / (double) height); } if (finalWidth > width && finalHeight > height) { finalWidth = width; finalHeight = height; } Matrix matrix = new Matrix(); matrix.reset(); float scaleWidth = ((float) finalWidth) / (float) width; float scaleHeight = ((float) finalHeight) / (float) height; matrix.postScale(scaleWidth, scaleHeight); mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, (int) width, (int) height, matrix, true); mBitmap.compress(Bitmap.CompressFormat.JPEG, 80, fOut); fOut.flush(); fOut.close(); mBitmap.recycle(); System.gc(); } catch (FileNotFoundException e) { saveComplete = false; e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); saveComplete = false; } return saveComplete; }
From source file:examples.utils.CifarReader.java
public static List<double[]> rawDouble(String workingDir, String file) { try {//from w ww . j av a 2s. co m return rawDouble(new BufferedInputStream(new FileInputStream(new File(workingDir, file)))); } catch (FileNotFoundException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static synchronized ArrayList<String> readFile(String filename) { BufferedReader buffreader;// w w w . j a v a 2 s.co m ArrayList<String> lines = null; FileInputStream is; BufferedReader reader; File file = new File(filename); if (file.exists()) { lines = new ArrayList<String>(); try { is = new FileInputStream(file); reader = new BufferedReader(new InputStreamReader(is)); String line = reader.readLine(); int i = 0; while (line != null) { lines.add(line); // System.out.println(line); line = reader.readLine(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return lines; }
From source file:Main.java
public static boolean storeImage(Context context, Bitmap bmp, boolean isRotate) { // use the current data&time for image file name String takenTime_YYMMDD_HHMMSS = new SimpleDateFormat(DATA_FORMAT).format(new Date()); // saved bitmap: full path String path = PIC_ROOT_PATH + takenTime_YYMMDD_HHMMSS; File f = new File(path); if (f != null && !f.getParentFile().exists()) { f.getParentFile().mkdirs();//w w w . ja v a 2 s . c o m } if (isRotate) { Matrix matrix = new Matrix(); matrix.reset(); matrix.postRotate(90); bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true); } try { FileOutputStream out = new FileOutputStream(f); bmp.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } return updateGallery(context, bmp, takenTime_YYMMDD_HHMMSS); }
From source file:Main.java
/** * This method handels the reading of data from a specific file. * //w w w. j a v a 2 s.c o m * @param file the file, to read from. * @param blockSize the length of the data-block, which should be read from the specified file. * @return the data read from the file. */ public static byte[] readFile(File file, long blockSize) { FileInputStream fis; byte[] fileContent = null; try { fis = new FileInputStream(file); FileChannel fileChannel = fis.getChannel(); int bytesToRead; fileChannel.position(readStreamPosition); int dataLen = fis.available(); // if there is a zero block size specified, read the whole file if (blockSize == 0L) { bytesToRead = dataLen; } else { bytesToRead = (int) blockSize; } fileContent = new byte[bytesToRead]; // reading the data for (int i = 0; i < bytesToRead; i++) { fileContent[i] = (byte) fis.read(); } // storing read-position readStreamPosition = fileChannel.position(); fis.close(); fileChannel.close(); // zero blockSize indicates, that reading of this file is completed, // stream position reset if (blockSize == 0L) { readStreamPosition = 0L; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return fileContent; }
From source file:io.github.bunnyblue.droidfix.classcomputer.cache.HashUtil.java
public static String getClassMd5(String path) { FileInputStream fis = null;/*from w w w. j a va2 s .co m*/ try { fis = new FileInputStream(new File(path)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } String md5 = null; try { md5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(fis); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return md5; }
From source file:Main.java
public static void copyFile(String sourcePath, String toPath) { File sourceFile = new File(sourcePath); File targetFile = new File(toPath); createDipPath(toPath);//ww w . j av a 2s . com try { BufferedInputStream inBuff = null; BufferedOutputStream outBuff = null; try { inBuff = new BufferedInputStream(new FileInputStream(sourceFile)); outBuff = new BufferedOutputStream(new FileOutputStream(targetFile)); byte[] b = new byte[1024 * 5]; int len; while ((len = inBuff.read(b)) != -1) { outBuff.write(b, 0, len); } outBuff.flush(); } finally { if (inBuff != null) inBuff.close(); if (outBuff != null) outBuff.close(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:Main.java
/** * Save image to the SD card/*from w w w.ja va2s . co m*/ * * @param photoBitmap * @param photoName * @param path */ public static void savePhotoToSDCard(Bitmap photoBitmap, String path, String photoName) { if (checkSDCardAvailable()) { File dir = new File(path); if (!dir.exists()) { dir.mkdirs(); } File photoFile = new File(path, photoName + ".png"); FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(photoFile); if (photoBitmap != null) { if (photoBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)) { fileOutputStream.flush(); // fileOutputStream.close(); } } } catch (FileNotFoundException e) { photoFile.delete(); e.printStackTrace(); } catch (IOException e) { photoFile.delete(); e.printStackTrace(); } finally { try { if (fileOutputStream != null) { fileOutputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } }