Results 1 to 3 of 3
Thread: I Need Help!
- 01-31-2008, 09:24 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 2
- Rep Power
- 0
I Need Help!
I need help with a part of my program. I need a program that is made with a static method which copies to two files which it has in the parameter.
Sort of like this filecopy.kopier("fil1.txt", "fil2.txt");
It has to copy the infomation for 1 to 2. Can anyone help?
Some examples would have been fine!:;D
- 02-02-2008, 12:18 AM #2
Member
- Join Date
- Jan 2008
- Posts
- 20
- Rep Power
- 0
Here is a file copy example
Java Code:boolean fileCopied = false; InputStream in = null; OutputStream out = null; String sepr = System.getProperty("file.separator"); try { in = new FileInputStream(path+sepr+srcFileName); File newFile = new File(path+sepr+targetFileName); out = new FileOutputStream(path+sepr+targetFileName); byte[] buffer = new byte[2048]; while (true) { synchronized (buffer) { int length = in.read(buffer); if (length != -1) { out.write(buffer, 0, length); } else break; } } File oldFile = new File(path+sepr+srcFileName); fileCopied = newFile.exists(); } catch(Throwable t){ //handle exceptions here. } finally { try{ if (in != null) { in.close();} if (out != null) { out.close(); } }catch(Throwable t1){} } return fileCopied;
Hope this helpsSincerely, Your friends at www.javaadvice.com
- 02-02-2008, 12:29 AM #3
or using nio
Java Code:public static void main(String[] args) { FileChannel in = null, out = null; try { in = new FileInputStream(args[0]).getChannel(); out = new FileOutputStream(args[1]).getChannel(); out.transferFrom(in, 0, in.size()); } catch (IOException e) { e.printStackTrace(); } finally{ try{ if (in != null) {in.close();} if (out != null) {out.close();} } catch (IOException ioe) { // just eat the exception } } }Last edited by jelly; 02-02-2008 at 12:31 AM.
-- Hope that helps


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks