• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to copy objects?

 
Sajee Joseph
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Suppose that i have a java file say 'myObject.java' at a particular url. Now i want to copy that java file to C:\
Which is the easiest means to achieving this?
..Regards
 
Paramagurusamy Balasubramanian
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi joseph,
Here is the sample code to achieve your task.
/* fileWriteMethod.java */
import java.net.*;
import java.io.*;
import java.util.*;
class fileWriteMethod
{
public static void main(String ar[])
throws IOException
{

URL url=new URL("http://www.joseph.com/myObject.java");
BufferedReader buf=new BufferedReader(new InputStreamReader(url.openStream()));
String in="";
StringBuffer strbuffer=new StringBuffer();
String temp="";


while ((in = buf.readLine()) != null)
{
if(in.length()>0)
strbuffer=strbuffer.append(in);

}

temp=strbuffer.toString();

byte[] b=temp.getBytes();
try
{
FileOutputStream file=new FileOutputStream("c:/file.txt");
file.write(b);
file.close();
}
catch(Exception e)
{
System.out.println("Error :"+e);
}
buf.close();
}
}

The above program reads the file myObject.java from the url and stores in the file "file.txt" in C drive..
Hope i fulfilled your request..
Best Regards,
Paramaguru
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!