From 3bc080241bf5f2b58ba70f3c0b8bfc46cc71e95a Mon Sep 17 00:00:00 2001 From: benfry Date: Mon, 19 Jan 2004 19:12:49 +0000 Subject: [PATCH] changes to copyFile() routine to make it faster and spew exceptions --- app/PdeBase.java | 55 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 18 deletions(-) diff --git a/app/PdeBase.java b/app/PdeBase.java index 8aa5c6fc2..ca67f0c69 100644 --- a/app/PdeBase.java +++ b/app/PdeBase.java @@ -352,29 +352,48 @@ public class PdeBase { } - static public void copyFile(File afile, File bfile) { - try { - FileInputStream from = new FileInputStream(afile); - FileOutputStream to = new FileOutputStream(bfile); - byte[] buffer = new byte[4096]; - int bytesRead; - while ((bytesRead = from.read(buffer)) != -1) { - to.write(buffer, 0, bytesRead); - } - to.flush(); - from.close(); // ?? - from = null; - to.close(); // ?? - to = null; + static public void copyFile(File afile, File bfile) throws IOException { + //try { + InputStream from = new BufferedInputStream(new FileInputStream(afile)); + OutputStream to = new BufferedOutputStream(new FileOutputStream(bfile)); + byte[] buffer = new byte[16 * 1024]; + int bytesRead; + while ((bytesRead = from.read(buffer)) != -1) { + to.write(buffer, 0, bytesRead); + } + to.flush(); + from.close(); // ?? + from = null; + to.close(); // ?? + to = null; #ifdef JDK13 - bfile.setLastModified(afile.lastModified()); // jdk13 required + bfile.setLastModified(afile.lastModified()); // jdk13+ required #endif - } catch (IOException e) { - e.printStackTrace(); - } + //} catch (IOException e) { + // e.printStackTrace(); + //} } + + /** + * Spew the contents of a String object out to a file. + */ + static public void saveFile(String contents, File location) + throws IOException { + + BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(contents.getBytes()))); + PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(location))); + + String line = null; + while ((line = reader.readLine()) != null) { + writer.println(line); + } + writer.flush(); + writer.close(); + } + + static public void copyDir(File sourceDir, File targetDir) { String files[] = sourceDir.list(); for (int i = 0; i < files.length; i++) {