JAVA中如何通过IP和端口连接到远程计算机并读取文件?
如此类似也
import javanet;
import javaio;
public class FtpConn
{
public static void main(String [] args)throws Exception
{
URL u=new URL("ftp://ppmm:1111@localhost/read1txt");
URLConnection urlconn=uopenConnection();
BufferedReader br=new BufferedReader(new InputStreamReader(urlconngetInputStream()));
String line;
while(null!=(line=brreadLine()))
{
Systemoutprintln(line);
}
}
}
ftp://ppmm:1111@localhost/read1txt
其中localhost是ftp server地址
ppmm是用户名
1111是密码
匿名用户不用写用户名和密码如
ftp://localhost/read1txt
就可以了
import javaioBufferedReader;
import javaioFile;
import javaioFileReader;
/
@author lmq
/
public class RemoteFile {
public static void main(String[] args) throws Exception {
File remoteFile = new File("//1921687146/test/1txt");// 1921687146是对方机器IP,test是对方那个共享文件夹名字,如果没有共享是访问不到的
//远程文件其实主要是地址,地址弄对了就和本地文件没什么区别 ,windows里面//或者\\\\开头就表示这个文件是网络路径了其实这个地址就像我们再windows里面,点击开始
//然后点击运行,然后输入 \\1921687146/test/1txt访问远程文件一样的
BufferedReader br = new BufferedReader(new FileReader(remoteFile));
String str;
while ((str = brreadLine()) != null) {
Systemoutprintln(str);
}
brclose();
}
}
/
读取某个文件夹下的所有文件
/
public static boolean readfile(String filepath) throws FileNotFoundException, IOException {
try {
File file = new File(filepath);
if (!fileisDirectory()) {
Systemoutprintln("文件");
Systemoutprintln("path=" + filegetPath());
Systemoutprintln("absolutepath=" + filegetAbsolutePath());
Systemoutprintln("name=" + filegetName());
} else if (fileisDirectory()) {
Systemoutprintln("文件夹");
String[] filelist = filelist();
for (int i = 0; i < filelistlength; i++) {
File readfile = new File(filepath + "\\" + filelist[i]);
if (!readfileisDirectory()) {
Systemoutprintln("path=" + readfilegetPath());
Systemoutprintln("absolutepath="
+ readfilegetAbsolutePath());
Systemoutprintln("name=" + readfilegetName());
} else if (readfileisDirectory()) {
readfile(filepath + "\\" + filelist[i]);
}
}
}
} catch (FileNotFoundException e) {
Systemoutprintln("readfile() Exception:" + egetMessage());
}
return true;
}
0条评论