Example usage for java.io IOException IOException

List of usage examples for java.io IOException IOException

Introduction

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

Prototype

public IOException(Throwable cause) 

Source Link

Document

Constructs an IOException with the specified cause and a detail message of (cause==null ?

Usage

From source file:Main.java

public static void copyFile(File src, File dst) throws IOException {
    if (src.isDirectory())
        throw new IOException("Source is a directory");
    InputStream in = new FileInputStream(src);
    OutputStream out = new FileOutputStream(dst);

    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;/*from  w  ww.  ja  v  a 2  s.  c o m*/
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

From source file:Main.java

public static final void deleteOrThrow(@NonNull File file) throws IOException {
    if (!file.exists()) {
        return;/*  ww w.jav  a  2 s. co m*/
    }

    boolean result = file.delete();
    if (!result) {
        throw new IOException("Failed to delete a file, file=" + file.getAbsolutePath());
    }
}

From source file:Main.java

public static void forceMkdir(File directory) throws IOException {
    if (directory.exists()) {
        if (!directory.isDirectory()) {
            String message = "File " + directory + " exists and is "
                    + "not a directory. Unable to create directory.";

            throw new IOException(message);
        }/* w w  w.  j  a va2  s.c  om*/
    } else if (!directory.mkdirs()) {
        if (!directory.isDirectory()) {
            String message = "Unable to create directory " + directory;

            throw new IOException(message);
        }
    }
}

From source file:Main.java

/**
 * Deletes the contents of {@code dir}. Throws an IOException if any file
 * could not be deleted, or if {@code dir} is not a readable directory.
 *//*  w  ww  .  j av  a  2 s.  co m*/
public static void deleteContents(File dir) throws IOException {
    File[] files = dir.listFiles();
    if (files == null) {
        throw new IOException("not a readable directory: " + dir);
    }
    for (File file : files) {
        if (file.isDirectory()) {
            deleteContents(file);
        }
        if (!file.delete()) {
            throw new IOException("failed to delete file: " + file);
        }
    }
}

From source file:Main.java

public static byte[] read(File file) throws IOException {
    byte[] buffer = new byte[(int) file.length()];
    InputStream ios = null;/*  w w w.j  a  va2 s . c o  m*/
    try {
        ios = new FileInputStream(file);
        if (ios.read(buffer) == -1)
            throw new IOException("EOF reached while trying to read the whole file");
    } finally {
        try {
            if (ios != null)
                ios.close();
        } catch (IOException e) {
        }
    }
    return buffer;
}

From source file:Main.java

/**
 * Create a new DOM document/*  w w w .j  ava 2 s.  c o  m*/
 */
public static Document newDocument() throws IOException {
    try {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        return builder.newDocument();
    } catch (ParserConfigurationException e) {
        throw new IOException(e.getMessage());
    }
}

From source file:Main.java

public static File getTempImage() throws IOException {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        File tempFile = new File(Environment.getExternalStorageDirectory(), "temp.jpg");
        tempFile.createNewFile();//from   w w w. j  a v a  2s.  c o  m
        return tempFile;
    }
    throw new IOException("cannot find any sdcard.");
}

From source file:Main.java

private static byte[] getMD5Digest(String data) throws IOException {
    byte[] bytes = null;
    try {/*  w ww  .j ava  2s. c o m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        bytes = md.digest(data.getBytes("UTF-8"));
    } catch (GeneralSecurityException gse) {
        throw new IOException(gse);
    }
    return bytes;
}

From source file:Main.java

public static BufferedImage loadImage(String fileName) throws IOException {
    URL url = Thread.currentThread().getContextClassLoader().getResource(fileName);
    if (url == null)
        throw new IOException("Could not find image: " + fileName);
    return ImageIO.read(url);
}

From source file:Main.java

private static byte[] getSHA1Digest(String data) throws IOException {
    byte[] bytes = null;
    try {/*from   w w w. j a  v  a2  s .c om*/
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        bytes = md.digest(data.getBytes("UTF-8"));
    } catch (GeneralSecurityException gse) {
        throw new IOException(gse.getMessage());
    }
    return bytes;
}