Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

One Machine to Another  RSS feed

 
Navi Singh
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Folks,
Q)Copying a file residing on another machine in the network to my machine or vice-versa.
Is it possible ??.
I tried using the following code.
It say's path not correct.
Please help
CODE START'S HERE:-
import java.io.*;
import java.lang.Object;
public class serialy
{
public static void main(String args[]) throws IOException
{
try{
File Fin=new File("Hp-machine/c:/aaa/lp.java");
File Fout=new File("ConnectMe2.java");
FileReader fr=new FileReader(Fin);
FileWriter wr=new FileWriter(Fout);
int c;
while ((c = fr.read()) != -1)

wr.write(c);

fr.close();
wr.close();
}
catch(Exception e)
{
System.out.println("Something Is Wrong"+e);
}
}}
CODE ENDS HERE.
Thank's In advance.
-PRAVEEN
 
Navi Singh
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C'mon Guys,
Please help.
-praveen
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Praveen,
Below is a code snippet that was adapted from an example I found in the book "Core Java 2" by Cay Horstmann and Gary Cornell:
http://authors.phptr.com/corejava/
Here is the code:

As you can see, you use the "java.net.URL" class, and the URL in the above example uses FTP to transfer the file from the remote computer. Replace <username> with the login name for the remote computer; replace <host> with the name of the remote computer. It's not exactly what you asked for -- but I think you can figure out the rest, right?
Hope it helps.
Good Luck,
Avi.
 
Navi Singh
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Avi,
Thanks for responding.
I have the following problem:-
The Code start's :
import java.util.*;
import java.io.*;
import java.lang.Object;
import java.net.*;
public class serialy

{
public static void main(String args[]) throws Exception,IOException
{

try{
System.out.println("1st Step");
java.net.URL location = new java.net.URL("ftp://:@HP-MACHINE/c:/aaa/lp.java");
System.out.println("2st Step");
InputStream is = location.openStream();
System.out.println("3st Step");
// OutputStreamReader
InputStreamReader isr = new InputStreamReader(is);
System.out.println("4st Step");
BufferedReader br = new BufferedReader(isr);
System.out.println("5st Step");
String nextLine = null;
while ((nextLine = br.readLine()) != null)
{
System.out.println(nextLine);
}


}
catch(Exception e)
{
System.out.println("Something Is Wrong"+e);
}

}
}
Code Ends.....
The error is it executes till the 2nd step,then
exit's and prints the following message:=
"Something Is Wrongjava.net.ConnectException: Connection refused: connect"
Please advice.
--Praveen
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Praveen,
Excuse me, but I don't understand. Did you misread my code? Did you forget to copy part of it? Did you decide you could do it better? Or did you just make a mistake in the code snippet you have posted? Because in that code snippet, your URL string does not include a username and password. [As Homer Simpson would say, "D'OH"]
Forgive me, I did not define the FTP, but the definition demands a username and password when you try to connect to another computer. If you don't supply those details, the remote computer wil refuse your connection. [Now I hope you realize why you get the "Connection refused" error message!]
If you don't want to supply a username and password, then I guess you'll probably have to find an alternative to FTP for transferring your files. I believe there are ways to establish a connection when using FTP that don't require you to supply a username and password -- but I don't know what they are -- I suggest you do some research on FTP to find out.
Good Luck,
Avi.
 
gautham kasinath
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well.. lemme try and explain..
to write a server from point A to B.. you need a sender and a receiver. When B is a receiver, A is the sender.. technically, A can be a client or a server and vice-versa for B.
Assuming B is a server, A is the client... A connects to B thru socket and reads the file in the files input stream ( which is local to A ) and dumps what it reads to the sockets output stream ( or to B, as put in lay mans terms ).
When you use the Url connection to have a ftp://... then you have to depend on B to be an FTP server.
Now, the table is turned when you have to write a file from B to A.
Do you need more clarification??
Lupo
 
Navi Singh
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Guy's thank's for the response.
Replying to Avi,
the machine i am trying to read does not have any userid/password,so the fields are blank
Anyways i tried on another machine which has only userid/no-password it gave the following error :--
Something Is Wrongjava.io.FileNotFoundException: CWD /C:/ppp/nav.java: 550 /C:/
ppp/nav.java: The filename, directory name, or volume label syntax is incorrect.

-----
The file and the folder r there in the machine named "Ora".
Praveen.
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Praveen, my mistake. Gautham is correct in stating that the machine where the file is located needs to be an FTP server in order for you to use FTP. I just assumed that it was. (As Homer Simpson would say... ;-)
So you need to use a different protocol, or a different method to transfer the file. So forgetting java for a minute, how do you know what file you need, and what machine it is located on? I mean, in order to transfer from one machine to another, you need them to be connected via a network. Then you need to be able to connect to your source machine from your target machine and transfer the file across the network. There are many ways to do this, but the most suitable way depends on your situation -- for example FTP is not suitable because the source machine is not an FTP server. Perhaps if you told me more about the two computers (like operating system, etc) and how they are connected (LAN, WAN, Intranet, etc), I may be able to suggest a more suitable method.
Cheers,
Avi.
 
Praveen
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Guys,
Thanks to both of you,
Good to know something new all the time.
Both the machine are in the same
work-group called : abc
connected in LAN n/w.
Machine where the residing file, i am trying to read :Win98
Machine Reading from (where java code resides):WinXP.
---That's all.
Once again Thanks in advance.
--Praveen.
 
gautham kasinath
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
howdy!!
Well, no matter what OS you are running and if you in the same network, LAN, WAN or VPN.. the client - server is something that is true.. one might argue that there is P2P. Which again has either of the two patries involved being a client or a server when needed..
So you have to make sure you have both a server running and a client.. the server and client may independently be your code or an existing program..
Lupo
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well then, I made another mistake. Your problem (in your original code) is probably an incorrect file name. For Windows, I usually do something like:

You need the double backslash ("\\") so that java interprets it correctly (as actually a single backslash). And you should set up a mapped network drive to your source machine (the Win98 machine, I assume). I have used (as an example) drive H: -- but you can use whatever you want. You might need to ask your network/system administrator to help you with that.
Hopefully, this will help you.
Good Luck,
Avi.
 
Navi Singh
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Avi,
the Original code works fine if both the files are in the same machine,
problem arises when one file(the file i am trying to read) resides on a different machine in the same N/w and same Workgroup.

--Praveen.
[ January 18, 2003: Message edited by: Pravin Palia ]
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's why I suggested setting up a mapped network drive -- this should be possible if both computers are on the same LAN. Is this not possible?
Alternatively, you may be able to use a "file" URL for opening the file. Example:

Note, the above is untested -- I never remember exactly how to write file URL's for Windows machines (I hardly ever use them).
So, does this help you?
Good Luck,
Avi.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!