?
Solved

Copy file

Posted on 2003-03-28
15
Medium Priority
?
488 Views
Last Modified: 2010-03-31
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
Comment
Question by:tosangi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +2
15 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 92 total points
ID: 8226665
int c = -1;
while ((c = ip.read()) > -1) {
  op.write(c);
}
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8226698
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
 

Author Comment

by:tosangi
ID: 8226806
No Luck:( andbody has complete code ??
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:tosangi
ID: 8226818
No Luck:( andbody has complete code ??
0
 

Author Comment

by:tosangi
ID: 8226823
Wehn complied i get the error stating "cannot resolve symbols : ip.read() ,op.close(),ip.close()"
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8226829
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
 

Author Comment

by:tosangi
ID: 8226832
Wehn complied i get the error stating "cannot resolve symbols : ip.read() ,op.close(),ip.close()"
0
 

Author Comment

by:tosangi
ID: 8226838
Wehn complied i get the error stating "cannot resolve symbols : ip.read() ,op.close(),ip.close()"
0
 

Author Comment

by:tosangi
ID: 8226852
Wehn complied i get the error stating "cannot resolve symbols : ip.read() ,op.close(),ip.close()"
0
 
LVL 18

Assisted Solution

by:bobbit31
bobbit31 earned 88 total points
ID: 8226865
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8227343
You should be able to get it going with bobbit31's code (not to mention David Flanagan and CEHJ ;-))
0
 

Expert Comment

by:CleanupPing
ID: 9058426
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
 
LVL 18

Expert Comment

by:bobbit31
ID: 9072189
split points b/w CEHJ and bobbit31
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9735301
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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month12 days, 21 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question