[JAVA]读取路径及其文档

1.采用 File

/*
* 读取指定路径下的文件名和目录名
*/
public void getFileList() {
//File file = new File("\\\\zh29\\");
File file = new File("C:\\Users\\29\\");
File[] fileList = file.listFiles();

HashMap<String, String> spec = null;
HashMap<String, HashMap<String, String>> pro = new HashMap();

for (int i = 0; i < fileList.length; i++) {
if (fileList[i].isFile()) {
String fileName = fileList[i].getName();
System.out.println("文件:" + fileName);
String[] Fname = fileName.split("@");

if(pro.get(Fname[1]) == null){
spec = new HashMap();
spec.put("specno"+i, Fname[2]);
pro.put(Fname[1], spec);
}else{
spec = pro.get(Fname[1]);
spec.put("specno"+i, Fname[2]);
pro.put(Fname[1], spec);
}
}

/*
if (fileList[i].isDirectory()) {
String fileName = fileList[i].getName();
System.out.println("目录:" + fileName);
}
*/
}
//System.out.println(pro.toString());
JSONObject json = new JSONObject();
String js = json.fromObject(pro).toString();
//System.out.println(js);

for(Entry<String, HashMap<String, String>> entry: pro.entrySet()){
System.out.println(entry.getKey());
}

if(pro.get("ECO_UTMS_准直器调节") != null){
HashMap<String,String> mm = pro.get("ECO_UTMS_准直器调节");
for(String key : mm.keySet()){
System.out.println(mm.get(key));
}
for(String v: mm.values()){
System.out.println(v);
}
}

}

 

2. SmbFile

pblic void getFileList2() throws MalformedURLException, SmbException {
String user = "XXXX";
String pass ="XXXX";
String path="smb://zh/";

NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",user, pass);
SmbFile smbFile = new SmbFile(path,auth);

SmbFile[] fileList = smbFile.listFiles();

for (int i = 0; i < fileList.length; i++) {
if (fileList[i].isFile()) {
String fileName = fileList[i].getName();
System.out.println("文件:" + fileName);
}

if (fileList[i].isDirectory()) {
String fileName = fileList[i].getName();
System.out.println("目录:" + fileName);
}
}
}

 

3.读取文档

public String loadcontent(String PATH) {
File file = new File("C:\\Users\\yNA.xml");
BufferedReader reader = null;
StringBuffer sbf = new StringBuffer();

try {
reader = new BufferedReader(new FileReader(file));
String tempStr;
while ((tempStr = reader.readLine()) != null) {
sbf.append(tempStr);
}
reader.close();
return sbf.toString();
} catch (IOException e) {
e.printStackTrace();
}
return sbf.toString();

}
}

上一篇:Python批量修改文件名模板


下一篇:java 中读取某个文件夹下所有文件