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 String getRandomNumByLine(int i) {
    File file = new File("/storage/emulated/0/uber_random");
    LineNumberReader lineNumberReader = null;
    StringBuilder builder = new StringBuilder();
    try {//from w  w w . java 2s .  c  o m
        if (file.exists()) {
            lineNumberReader = new LineNumberReader(new FileReader(file));
            String tmp = null;
            while ((tmp = lineNumberReader.readLine()) != null) {
                builder.append(tmp + "\n");
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (null != lineNumberReader) {
            try {
                lineNumberReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    String fileString = builder.toString();
    Log.d("TAG", "Read num is --->" + fileString);
    String[] split = fileString.split("\\n");
    if (split != null && (split.length >= i)) {
        String value = split[i];
        Log.d("TAG", "Read sub is --->" + value);
        return value;
    }
    return null;
}

From source file:Main.java

public static void save2Cache(Context context, String cover, Bitmap bitmap) {
    File file = new File(context.getCacheDir(), cover + ".png");
    FileOutputStream fileOutputStream = null;
    try {/*www .  ja v  a  2s .c o  m*/
        fileOutputStream = new FileOutputStream(file);
        if (bitmap != null) {
            if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)) {
                fileOutputStream.flush();
            }
        }
    } catch (FileNotFoundException e) {
        file.delete();
        e.printStackTrace();
    } catch (IOException e) {
        file.delete();
        e.printStackTrace();
    } finally {
        try {
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static Uri getSMSLogs(ContentResolver cr, Uri internal, Context context, String timeStamp) {
    String[] smsLogArray = new String[2];
    Uri uri = Uri.parse("content://sms/inbox");
    Cursor cur = cr.query(uri, null, null, null, null);
    FileOutputStream fOut = null;

    try {/*from  w w w . jav  a 2s .com*/
        fOut = context.openFileOutput("sms_logs_" + timeStamp + ".txt", Context.MODE_PRIVATE);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    OutputStreamWriter osw = new OutputStreamWriter(fOut);

    while (cur.moveToNext()) {
        smsLogArray[0] = cur.getString(cur.getColumnIndexOrThrow("address")).toString();
        smsLogArray[1] = cur.getString(cur.getColumnIndexOrThrow("body")).toString();

        writeToOutputStreamArray(smsLogArray, osw);
    }

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

    return internal;
}

From source file:Main.java

public static boolean fileCopy(String from, String to) {
    boolean result = false;

    int size = 1 * 1024;

    FileInputStream in = null;/*from   www  .  j  ava  2 s  .c o  m*/
    FileOutputStream out = null;
    try {
        in = new FileInputStream(from);
        out = new FileOutputStream(to);
        byte[] buffer = new byte[size];
        int bytesRead = -1;
        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
        }
        out.flush();
        result = true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
        }
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
        }
    }
    return result;
}

From source file:currencyexchange.JSONCurrency.java

public static ArrayList<CurrencyUnit> getCurrencyUnitsJSON() {
    try {/*from w ww  .  j a va2s  . com*/
        FileReader reader = new FileReader(filePath);

        JSONParser jsonParser = new JSONParser();
        JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);

        JSONArray currencyUnits = (JSONArray) jsonObject.get("units");

        ArrayList<CurrencyUnit> currencies = new ArrayList<CurrencyUnit>();

        Iterator i = currencyUnits.iterator();
        while (i.hasNext()) {
            JSONObject e = (JSONObject) i.next();
            CurrencyUnit currency = new CurrencyUnit((String) e.get("CountryCurrency"),
                    (String) e.get("Units"));

            currencies.add(currency);
        }
        return currencies;

    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    } catch (ParseException | NullPointerException ex) {
        ex.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static boolean saveBitmapToLocal(String path, String fileName, Bitmap b) {
    if (b == null) {
        return false;
    }//from  w w w .  j  a  va2  s  . co  m

    boolean result = false;
    String storageState = Environment.getExternalStorageState();
    if (storageState.equals(Environment.MEDIA_MOUNTED)) {

        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(path + fileName);
            b.compress(CompressFormat.JPEG, 100, fos);
            fos.flush();
            result = true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
    return result;
}

From source file:Main.java

public static Uri getSMSLogs(ContentResolver cr, Uri internal, Context context, String timeStamp) {
    String[] smsLogArray = new String[2];
    Uri uri = Uri.parse("content://sms/inbox");
    Cursor cur = cr.query(uri, null, null, null, null);
    //Cursor cur= cr.query(uri, smsLogArray, null ,null,null);
    FileOutputStream fOut = null;

    try {/*w  w  w  .ja  v a  2 s  . c om*/
        fOut = context.openFileOutput("sms_logs_" + timeStamp + ".txt", Context.MODE_PRIVATE);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    OutputStreamWriter osw = new OutputStreamWriter(fOut);

    while (cur.moveToNext()) {
        smsLogArray[0] = cur.getString(cur.getColumnIndexOrThrow("address")).toString();
        smsLogArray[1] = cur.getString(cur.getColumnIndexOrThrow("body")).toString();

        writeToOutputStreamArray(smsLogArray, osw);
    }

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

    return internal;
}

From source file:Main.java

/**
 * Get AppName(package name) Name by specific process id.
 *
 * @return App Name, ex) com.api.demo//w w  w  . j  ava  2s .c  o  m
 */
public static String getMyAppName(int pid) {
    final String PATH = "/proc/" + pid + "/cmdline";

    BufferedReader cmdlineReader = null;
    StringBuilder processName = new StringBuilder();
    int c = 0;

    try {
        cmdlineReader = new BufferedReader(new InputStreamReader(new FileInputStream(PATH)));

        while ((c = cmdlineReader.read()) > 0) {
            processName.append((char) c);
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (cmdlineReader != null)
                cmdlineReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return processName.toString();
}

From source file:Main.java

/** Parses a DOM from the given XML file on disk. */
public static Document parseXML(final File file) {
    try {/*from   ww w. j  a va  2s .com*/
        return parseXML(new FileInputStream(file));
    } catch (final FileNotFoundException exc) {
        exc.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static int getLineCount(String path) {
    LineNumberReader lnr = null;//from   w  w  w .  j a va  2  s .c  om
    try {
        lnr = new LineNumberReader(new FileReader(new File(path)));
        lnr.skip(Long.MAX_VALUE);
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (lnr == null)
        return 0;
    return lnr.getLineNumber();
}