ExtJs + SpringMVC 項目中讀取本地日誌文件

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;
	}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章