Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-31-2008, 11:24 PM
Member
 
Join Date: Nov 2007
Posts: 2
pijamatoje is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-02-2008, 02:18 AM
Member
 
Join Date: Jan 2008
Posts: 20
JAdmin is on a distinguished road
Here is a file copy example

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 helps
__________________
Sincerely, Your friends at
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-02-2008, 02:29 AM
jelly's Avatar
Member
 
Join Date: Jan 2008
Location: Somerset, UK
Posts: 46
jelly is on a distinguished road
or using nio

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 } } }
__________________
-- Hope that helps

Last edited by jelly : 02-02-2008 at 02:31 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 04:39 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org