Java tutorial
//package com.java2s; /* * @(#)IOUtils.java * * $Date: 2012-07-03 01:10:05 -0500 (Tue, 03 Jul 2012) $ * * Copyright (c) 2011 by Jeremy Wood. * All rights reserved. * * The copyright of this software is owned by Jeremy Wood. * You may not use, copy or modify this software, except in * accordance with the license agreement you entered into with * Jeremy Wood. For details see accompanying license terms. * * This software is probably, but not necessarily, discussed here: * http://javagraphics.java.net/ * * That site should also contain the most recent official version * of this software. (See the SVN repository for more details.) */ import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.util.Properties; public class Main { static private byte[] b1; /** Writes a copy of a file. * * @param src the file to copy * @param dst the location to write the new file * @throws IOException */ public synchronized static void copy(File src, File dst) throws IOException { if (b1 == null) b1 = new byte[4096]; FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream(src); dst.getParentFile().mkdirs(); dst.createNewFile(); out = new FileOutputStream(dst); int k = in.read(b1); while (k != -1) { out.write(b1, 0, k); k = in.read(b1); } } finally { try { in.close(); } catch (Throwable t) { t.printStackTrace(); } try { out.close(); } catch (Throwable t) { t.printStackTrace(); } } } /** Read data into the destination array. * * @param in the InputStream to read. * @param dest the destination to write to * @return the number of bytes read (note this will be less than dest.length when the end of the stream is reached). * @throws IOException */ public static int read(InputStream in, byte[] dest) throws IOException { int length = dest.length; int read = 0; int k = in.read(dest, read, length - read); while (k != -1 && read < dest.length) { read += k; k = in.read(dest, read, dest.length - read); } if (k != -1) { read += k; } return read; } public static String read(File file) throws IOException { FileInputStream in = null; try { in = new FileInputStream(file); return read(in); } finally { try { if (in != null) in.close(); } catch (Throwable t) { } } } public static String read(InputStream in) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(in)); StringBuffer sb = null; String s = br.readLine(); while (s != null) { if (sb == null) { sb = new StringBuffer(); sb.append(s); } else { sb.append("\n"); sb.append(s); } s = br.readLine(); } return sb.toString(); } /** Write the text provided to a File. * * @param file the file to write to. * @param text the text to write. * @throws IOException */ public static void write(File file, String text) throws IOException { FileOutputStream out = null; try { file.getParentFile().mkdirs(); file.delete(); file.createNewFile(); out = new FileOutputStream(file); OutputStreamWriter writer = new OutputStreamWriter(out); writer.write(text); writer.flush(); } finally { try { out.close(); } catch (Throwable t) { t.printStackTrace(); } } } /** Writes the entire InputStream into the destination file. * @param in the input stream to write * @param dest the file to write to. */ public static void write(InputStream in, File dest) throws IOException { OutputStream out = null; try { if (dest.exists() == false) dest.createNewFile(); out = new FileOutputStream(dest); write(in, out); } finally { if (out != null) { try { out.close(); } catch (Throwable t) { } } } } /** Writes a <code>Properties</code> object to * a <code>File</code>. */ public static void write(Properties p, File file) throws IOException { OutputStream out = null; try { out = new FileOutputStream(file); p.store(out, ""); } finally { if (out != null) { try { out.close(); } catch (Throwable t) { } } } } /** Writes a file to an OutputStream. * @param file the file to write. * @param out the stream to write to. */ public static void write(File file, OutputStream out) throws IOException { InputStream in = null; try { in = new FileInputStream(file); write(in, out); } finally { if (in != null) { try { in.close(); } catch (Throwable t) { } } } } /** Writes the InputStream into the OutputStream. * This does not close anything. * @param in the data to read. * @param out the destination to write to. */ public synchronized static void write(InputStream in, OutputStream out) throws IOException { if (b1 == null) b1 = new byte[4096]; int t = in.read(b1); while (t != -1) { out.write(b1, 0, t); t = in.read(b1); } } /** Delete this file and all its children, if any. * @return true if the delete was successful. */ public static boolean delete(File file) { boolean success = true; if (file.isDirectory()) { File[] children = file.listFiles(); for (int a = 0; a < children.length; a++) { if (delete(children[a]) == false) success = false; } } if (file.delete() == false) success = false; return success; } }