• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 490
  • Last Modified:

Copy file

Hi,
Dont know whts wrong with the code...Pls help me fast..Code is here:


import java.io.*;

public class copyFile

{

   public static void main(String s[])

   {

     int bytelen = 0;//no. of bytes copied frm src file
     //String src="d:/sangee/resume/staffing.txt" ;//path of src file
     //String copy="c:/sangee/resume/staffing.txt";//path of dest. file

 try
  {
     InputStream ip = new FileInputStream("d:/sangee/resume/staffing.txt");
   }catch(Exception e){System.out.println(e);}

    try
      {
     OutputStream op =new FileOutputStream("c:/sangee/resume/staffing.txt");
      } catch(Exception ex){System.out.println(ex);}

      try
      {                                                                       while(true)                                                
                                                             
                                                                 
        {
         int i = ip.read();
         if(i < 0)
         break;
          op.write(i);
          bytelen++;
         }

        ip.close();
        op.close();

       }catch(Exception ex1){System.out.println(ex1);}

    }

}
0
tosangi
Asked:
tosangi
  • 6
  • 4
  • 2
  • +2
2 Solutions
 
CEHJCommented:
int c = -1;
while ((c = ip.read()) > -1) {
  op.write(c);
}
0
 
bobbit31Commented:
file separator char is '\' in windows, and since '\' is the escape char in java, you have to have '\\' as shown below.

you could also do something like:
String srcFile = "d:" + File.separatorChar + "sangee" + File.separatorChar + "resume" + ...

 try
  {
     InputStream ip = new FileInputStream("d:\\sangee\\resume\\staffing.txt");
   }catch(Exception e){System.out.println(e);}

    try
      {
     OutputStream op =new FileOutputStream("c:\\sangee\\resume\\staffing.txt");
      } catch(Exception ex){System.out.println(ex);}
0
 
tosangiAuthor Commented:
No Luck:( andbody has complete code ??
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
tosangiAuthor Commented:
No Luck:( andbody has complete code ??
0
 
tosangiAuthor Commented:
Wehn complied i get the error stating "cannot resolve symbols : ip.read() ,op.close(),ip.close()"
0
 
bobbit31Commented:
public static void fileCopy(String from_name, String to_name) throws IOException{
   java.io.File from_file = new java.io.File(from_name);  // Get File objects from Strings
   java.io.File to_file = new java.io.File(to_name);

   FileInputStream from = null;  // Stream to read from source
   FileOutputStream to = null;   // Stream to write to destination
   try {
     from = new FileInputStream(from_file);  // Create input stream
     to = new FileOutputStream(to_file);     // Create output stream
     byte[] buffer = new byte[4096];         // A buffer to hold file contents
     int bytes_read;                         // How many bytes in buffer

     // Read a chunk of bytes into the buffer, then write them out,
     // looping until we reach the end of the file (when read() returns -1).
     // Note the combination of assignment and comparison in this while
     // loop.  This is a common I/O programming idiom.
     while((bytes_read = from.read(buffer)) != -1) // Read bytes until EOF
       to.write(buffer, 0, bytes_read);            //   write bytes
   }
   // Always close the streams, even if exceptions were thrown
   finally {
     if (from != null) try { from.close(); } catch (IOException e) { ; }
     if (to != null) try { to.close(); } catch (IOException e) { ; }
   }
 }

0
 
tosangiAuthor Commented:
Wehn complied i get the error stating "cannot resolve symbols : ip.read() ,op.close(),ip.close()"
0
 
tosangiAuthor Commented:
Wehn complied i get the error stating "cannot resolve symbols : ip.read() ,op.close(),ip.close()"
0
 
tosangiAuthor Commented:
Wehn complied i get the error stating "cannot resolve symbols : ip.read() ,op.close(),ip.close()"
0
 
bobbit31Commented:
and using your code, this should work:

import java.io.*;

public class copyFile

{

  public static void main(String s[])

  {

    int bytelen = 0;//no. of bytes copied frm src file
    String src="d:\\sangee\\resume\\staffing.txt";  //path of src file
    String copy="c:\\sangee\\resume\\staffing.txt"; //path of dest. file

    InputStream ip = null;
    OutputStream op = null;
   
    try {
       ip = new FileInputStream(src);
       op =new FileOutputStream(copy);

       int c = -1;
       while ((c = ip.read()) > -1) {
         op.write(c);
       }

       ip.close();
       op.close();

    } catch(Exception ex1){System.out.println(ex1);}
  }
}
0
 
CEHJCommented:
You should be able to get it going with bobbit31's code (not to mention David Flanagan and CEHJ ;-))
0
 
CleanupPingCommented:
tosangi:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
bobbit31Commented:
split points b/w CEHJ and bobbit31
0
 
jimmackCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Split between CEHJ and bobbit31.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jimmack
EE Cleanup Volunteer
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now