最近在实验使用java读取指定路径下的文件,发现没办法进入共享根目录下的文件夹
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import com.sun.nfs.XFileExtensionAccessor;
import com.sun.xfile.XFile;
import com.sun.xfile.XFileInputStream;
import com.sun.xfile.XFileOutputStream;
import com.sun.xfile.XFileWriter;
import net.sf.jftp.system.logging.Log;
import java.io.*;
/**
Created by fd on 2017/8/14.
*/
public class NFS {
String url;
XFile xfile;
public void NFSconnection(String ip,String dir)
{
url = "nfs://" + ip + "/" + dir;//创建连接
xfile = new XFile(url);
//调用exists()判断是否连接成功
if (xfile.exists()) {
System.out.println("URL is OK!");
} else {
System.out.println("URL is Bad!");
return;
}
}
public void test(String pathname) throws IOException {
String[] fileList = xfile.list();//缺少这一句的话,会出现找不到文件的错误
XFile temp = new XFile(url+"/"+pathname);
XFileInputStream in = new XFileInputStream(temp);
int lent = 0;
byte[] buf = new byte[200];
while((lent = in.read(buf))>0){
System.out.println(new String(buf,0,lent));
}
}
public static void main(String[] args) throws IOException {
String ip ="172.19.152.32";
String dir = "nfs";
NFS nfs = new NFS();
nfs.NFSconnection(ip,dir);
nfs.test("ss");
}
}
文件夹设置了正确的权限了没有,另外你是网络路径?要先建立共享连接。
应该不是这个问题,我用window指令连接的时候没有出现上述问题。用java才会,后来调用list()就可以了,每次进入一个新的目录,都要list()一下。不过不知道为啥要这样
为什么我的xfile = new XFile(url);都创建不了呀?然后后面的exists()就会返回false。但是我的NFS已经配置了呀