Results 1 to 3 of 3

Thread: I Need Help!

  1. #1
    pijamatoje is offline Member
    Join Date
    Nov 2007
    Posts
    2
    Rep Power
    0

    Default 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

  2. #2
    JAdmin is offline Member
    Join Date
    Jan 2008
    Posts
    20
    Rep Power
    0

    Default

    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 helps
    Sincerely, Your friends at www.javaadvice.com

  3. #3
    jelly's Avatar
    jelly is offline Member
    Join Date
    Jan 2008
    Location
    Somerset, UK
    Posts
    46
    Rep Power
    0

    Default

    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 01:31 AM.
    -- Hope that helps

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •