Results 1 to 7 of 7

Thread: Problem in copying a file in java

  1. #1
    Join Date
    Mar 2010
    Posts
    338

    Problem in copying a file in java

    Hello,
    I'm studying Java at the moment and I would like to copy file FileCopy with (). The problem is that it makes me an error when I use it. Here is my code:

    Code:
    public class cpfl (
     
    	
    	public static void hand(String[] args) (
    		
    		File fl = null; File cp = null; 
    		fl = new File("c:\ \test.txt "); 
    		cp = new File("c:\ temp.txt ");
    		
    		if (fl.exists())(
    			
    			/ / cp.createNewFile ( );
    			cp.FileCopy(myfile);
    		)
    		
    		)
     
    	)
    This is not working, any help will be great. Thank you.

  2. #2
    Join Date
    Nov 2009
    Posts
    359

    Re: Problem in copying a file in java

    Check out the following code, this will help you

    Code:
    try (
    BufferedReader buff = new BufferedReader(new FileReader("c: / test.txt")); BufferedWriter buffwrt = new BufferedWriter(new FileWriter("c: / temp.txt")); String ln;
    while((ln = buff.readLine()) ! = null)(
        buffwrt.write(ln); buffwrt.NewLine();
    )
    buffwrt.close();
    )
    catch(Exception e)(
        e.printStackTrace();
    )
    See if this thread can help you
    How to copy a file in JAVA?

  3. #3
    Join Date
    Mar 2010
    Posts
    372

    Re: Problem in copying a file in java

    I would like to make a file copy for example a gif image in java, when he s acts. txt are no problems that it s to another format, making copying s but I can not open file! "I've used a BufferedReader pr read the file and a FileWriter write. Please help me with this, also a example code is appreciated. Thank you.

  4. #4
    Join Date
    Dec 2009
    Posts
    292

    Re: Problem in copying a file in java

    This is the code you are looking for

    Code:
    try ( 
    / / Create the file: 
    dst.createNewFile (); 
    / / Opening streams 
    sfl = new java.io.FileInputStream (source); 
    desfl = new java.io.FileOutputStream (dst); 
    / / Reading by segment 0.5Mo 
    byte bf [] = new byte [512 * 1024]; 
    int lec; 
    while ((lec srfl.read = (bf))! = -1) ( 
    desfl.write (bf, 0, lec); 
    ) 
    
    / / Copy success 
    rst = true; 
    ) Catch (java.io.FileNotFoundException f) ( 
    ) Catch (java.io.IOException e) ( 
    ) Finally ( 
    / / Whatever happens, we close the stream 
    try ( 
    srfl.close (); 
    ) Catch (Exception e) () 
    try ( 
    desfl.close (); 
    ) Catch (Exception e) () 
    ) 
    return (rst); 
    )

  5. #5
    Join Date
    Dec 2009
    Posts
    213

    Re: Problem in copying a file in java

    The problem is that you make a copy in text mode. You must do a binary copy. Passes by simply FileInputStream and a FileOutputStream. Used an array of bytes and it will alone. I guess you are understanding what I am trying to say here.

  6. #6
    Join Date
    Sep 2009
    Posts
    177

    Re: Problem in copying a file in java

    Hello,
    I want reservoir s how they please create a file in java . thank you ahead of liniment.

  7. #7
    Join Date
    Mar 2010
    Posts
    197

    Re: Problem in copying a file in java

    Try the following code

    Code:
    String str = new String () ( 
    try ( 
    BufferedWriter fv = new BufferedWriter (new FileWriter ("name fv.txt")); 
    flc.writeLine (str, 0, str.length ()); 
    flc.close (); 
    ) 
    catch (IOException e) ( 
    System.out.println ("File error:" + e.ToString ()); 
    )

Similar Threads

  1. Prevent file copying from a CD in Java
    By JohnP11 in forum Software Development
    Replies: 1
    Last Post: 25-02-2011, 12:49 AM
  2. Just Cause 2 steam_appi file copying problem
    By Tomthegreat in forum Video Games
    Replies: 5
    Last Post: 25-03-2010, 04:44 PM
  3. Java code for copying text to the clipboard
    By Maya Angelou in forum Software Development
    Replies: 5
    Last Post: 22-03-2010, 02:04 PM
  4. Problem in reading file in java
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 02-02-2010, 03:05 AM
  5. Windows encountered a problem copying file
    By Cris Han in forum Windows XP Support
    Replies: 4
    Last Post: 12-01-2008, 03:07 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,484,720.12824 seconds with 17 queries