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 inputStreamToFile(InputStream is, File file) {
    OutputStream os = null;/*from www .java2  s  .  c  o m*/
    try {
        os = new FileOutputStream(file);

        int bytesRead = 0;
        byte[] buffer = new byte[3072];

        while ((bytesRead = is.read(buffer, 0, 3072)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        os.close();
        is.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static Object convertXmlFileToObject(Class clazz, String xmlPath) {
    Object xmlObject = null;//from  ww w  . jav a 2s. com
    try {
        JAXBContext context = JAXBContext.newInstance(clazz);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        FileReader fr = null;
        try {
            fr = new FileReader(xmlPath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        xmlObject = unmarshaller.unmarshal(fr);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return xmlObject;
}

From source file:Main.java

public static void printDom(Document dom, String filename) {

    try {//w  w  w  .j  a va2  s.c  o  m
        DOMSource source = new DOMSource(dom);
        StreamResult result;
        result = new StreamResult(new FileOutputStream(filename));

        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();

        transformer.transform(source, result);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static String readFile(File file) throws Exception {
    StringBuffer sb = new StringBuffer();
    try {/*from  w  w w .j  ava2  s.c om*/
        FileInputStream fis = new FileInputStream(file);
        BufferedReader br = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
        String line = null;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        fis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (sb.toString().trim().length() <= 0)
        return null;
    return sb.toString();
}

From source file:Main.java

public static Object objectXmlDecoder(String objSource) {
    Object obj = null;//from w w  w.  j  a v a2  s.co m
    try {
        File fin = new File(objSource);
        if (fin.exists()) {
            FileInputStream fis = new FileInputStream(fin);
            XMLDecoder decoder = new XMLDecoder(fis);
            obj = decoder.readObject();
            fis.close();
            decoder.close();
        }
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    return obj;
}

From source file:Main.java

public static Bitmap getBitmap(Context context, String imageFile) {
    try {//from   w  w  w.  j  a va  2 s.co  m
        if (imageFile != null) {
            if (imageFile.contains("/")) {
                imageFile = imageFile.substring(imageFile.lastIndexOf("/") + 1);
            }
            FileInputStream in = context.openFileInput(imageFile);
            return BitmapFactory.decodeStream(in);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static void save(String path, Bitmap bitmap) {

    String name = path.substring(path.lastIndexOf("/"));
    File file = new File(SAVE_PATH + name);
    try {//  ww w  .j  a  v a 2 s .  c o m
        if (!file.exists()) {
            file.getParentFile().mkdirs();
            file.createNewFile();
        }
        FileOutputStream out = new FileOutputStream(file);
        if (bitmap != null) {
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        }
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String read(File file) {
    if (!file.exists()) {
        return "";
    }//from   www.  ja  va  2 s.c om
    try {
        FileReader reader = new FileReader(file);
        BufferedReader br = new BufferedReader(reader);
        StringBuffer buffer = new StringBuffer();
        String s;
        while ((s = br.readLine()) != null) {
            buffer.append(s);
        }
        return buffer.toString();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static String readTextFromSDcard(String fileName) {

    File file = new File(fileName);
    if (!file.exists()) {
        return null;
    }//from   w  w  w .  jav a2  s. c o  m
    try {
        FileInputStream fileInputStream = new FileInputStream(file);
        int availableLength = fileInputStream.available();
        byte[] buffer = new byte[availableLength];
        fileInputStream.read(buffer);
        fileInputStream.close();

        return new String(buffer, "UTF-8");

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static boolean putBitmapToFile(Bitmap bitmap, File file) {
    if (bitmap == null || file == null)
        return false;
    FileOutputStream fos = null;//w  w  w  . j a va2s .  c  o m
    try {
        fos = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (fos != null)
            try {
                fos.flush();
                fos.close();
                if (!bitmap.isRecycled())
                    bitmap.recycle();
            } catch (IOException ignored) {
            }
    }
    return false;
}