1.先將所有文件名稱,顯示出來,點擊裝載文件內容。
裝載所有文件名稱:
讀取文件名,
private static void getFile(String path){
// get file list where the path has
File file = new File(path);
// get the folder list
File[] array = file.listFiles();
for(int i=0;i<array.length;i++){
if(array[i].isFile()){
// only take file name
System.out.println("^^^^^" + array[i].getName());
// take file path and name
System.out.println("#####" + array[i]);
// take file path and name
System.out.println("*****" + array[i].getPath());
}else if(array[i].isDirectory()){
getFile(array[i].getPath());
}
}
}
讀取內容:
@Override
public String retrieveLogContent(String fileName,String id) {
String output = "";
File file = new File(logPath + File.separator + fileName);
if (file.exists()) {
if (file.isFile()) {
try {
BufferedReader input = new BufferedReader(new FileReader(file));
StringBuffer buffer = new StringBuffer();
String text;
while ((text = input.readLine()) != null)
buffer.append(text);
output = buffer.toString();
} catch (IOException e) {
log.error("fail to read file:[" + e.getMessage() + "]");
}
}
} else {
log.warn("the specified file:[" + file.getAbsolutePath() + "] does not exists");
}
return output.length() == 0 ? "Sorry, we cannot find any content from this file" : output;
}