Example usage for java.io FileNotFoundException printStackTrace

List of usage examples for java.io FileNotFoundException printStackTrace

Introduction

In this page you can find the example usage for java.io FileNotFoundException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:Main.java

public static void put(String s, String name) {
    try {//from   w ww . j  a  v  a  2  s.c  om
        File saveFile = new File(Environment.getExternalStorageDirectory() + "/hhtlog/" + name + ".txt");
        if (!saveFile.exists()) {
            File dir = new File(saveFile.getParent());
            dir.mkdirs();
            saveFile.createNewFile();
        }

        FileOutputStream outStream = new FileOutputStream(saveFile);
        outStream.write(s.getBytes());
        outStream.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static byte[] getBytes(String filePath) {
    byte[] buffer = null;
    try {//from w  w w  . ja v  a 2  s.  c o m
        File file = new File(filePath);
        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 void saveImage(Bitmap bmp) {
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/abcxyz");
    myDir.mkdirs();/*  w w  w .jav a 2s .c  o  m*/
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String name = "Image-" + n + ".jpg";
    File file = new File(myDir, name);
    if (file.exists())
        file.delete();

    try {
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void saveMyBitmap(String bitName, Bitmap mBitmap) throws IOException {
    String myJpgPath = Environment.getExternalStorageDirectory() + "/pepper/" + "1.png";
    File tmp = new File("/sdcard/pepper/");
    if (!tmp.exists()) {
        tmp.mkdir();/*  www. ja v a  2 s.com*/
    }
    File f = new File(myJpgPath);
    f.createNewFile();
    FileOutputStream fOut = null;
    try {
        fOut = new FileOutputStream(f);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    try {
        fOut.flush();
        fOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void saveBitmap2file(Bitmap bmp, File file) {

    OutputStream stream = null;// w w w  .j  a  v a 2s  .co m
    try {
        stream = new FileOutputStream(file.getAbsolutePath());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);

    try {
        stream.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        stream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

private static void saveToFile(byte[] data, String path) {
    if (data == null || data.length == 0) {
        return;/*from  www.j  av a2s.  co  m*/
    }
    if (TextUtils.isEmpty(path)) {
        return;
    }
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(path, true);
        fos.write(data);
        fos.flush();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:Main.java

public static String readTxtFile(String filePath) {
    File file = new File(filePath);
    if (file == null || !file.exists()) {
        return null;
    }/*from   w w w.jav  a2s  . co  m*/
    try {
        Scanner scanner = new Scanner(file);
        StringBuilder sb = new StringBuilder();
        while (scanner.hasNextLine()) {
            sb.append(scanner.nextLine());
        }
        scanner.close();
        return sb.toString();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String scaleImageFile(String filepath, int targetSize) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    File file = new File(filepath);
    float scaleFactor = file.length() / targetSize;
    options.inSampleSize = Math.round(scaleFactor);
    Bitmap bitmap = BitmapFactory.decodeFile(filepath, options);

    int dotPos = filepath.lastIndexOf('.');
    String newFilePath = String.format("%s_scaled.%s", filepath.substring(0, dotPos),
            filepath.substring(dotPos + 1, filepath.length()));
    FileOutputStream fos = null;// w w  w.java  2s  . c  o  m
    try {
        fos = new FileOutputStream(newFilePath);
        if (!bitmap.compress(CompressFormat.JPEG, 90, fos)) {
            newFilePath = null;
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        newFilePath = null;
    }
    bitmap.recycle();
    return newFilePath;
}

From source file:Main.java

/**
 * dump bipartite community affiliation into a text file with node names
 * /*from   ww w. ja  v a 2 s.c om*/
 * @param OutFNm
 * @param CmtyVV
 * @param NIDNmH
 */
static void dumpCmtyVV(final String OutFNm, Vector<Vector<Integer>> CmtyVV, Hashtable<Integer, String> NIDNmH) {
    PrintWriter f;
    try {
        f = new PrintWriter(OutFNm);

        for (int c = 0; c < CmtyVV.size(); c++) {
            for (int u = 0; u < CmtyVV.get(c).size(); u++) {
                if (NIDNmH.containsKey(CmtyVV.get(c).get(u))) {
                    f.printf("%s\t", NIDNmH.get(CmtyVV.get(c).get(u)));
                } else {
                    f.printf("%d\t", (int) CmtyVV.get(c).get(u));
                }
            }
            f.printf("\n");
        }
        f.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String GetCachedString(String filename, Context ctx) {
    try {//  w ww  .  ja  v a 2  s  .c  om
        InputStream inputStream = ctx.openFileInput(filename);

        if (inputStream != null) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String receiveString = "";
            StringBuilder stringBuilder = new StringBuilder();

            while ((receiveString = bufferedReader.readLine()) != null) {
                stringBuilder.append(receiveString);
            }

            inputStream.close();
            return stringBuilder.toString();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";

}