除下載文件外都已測試通過
package com.goldpac.ocs.common.gatewaymanagerservice.service.impl;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
public class test {
public static void main(String args[]) throws IOException {
String filepath = "C:\\Users\\jian.li\\Desktop";
//讀寫目錄下匹配指定格式的文件
getfile(filepath);
String pathname = "C:\\Users\\jian.li\\Desktop\\input - 副本.log";
//刪除文件
delFile(pathname);
}
//讀取文件
public static void readFile(String pathname) {
String text = "";
//防止文件建立或讀取失敗,用catch捕捉錯誤並打印,也可以throw;
//不關閉文件會導致資源的泄露,讀寫文件都同理
//Java7的try-with-resources可以優雅關閉文件,異常時自動關閉文件;詳細解讀https://stackoverflow.com/a/12665271
try (FileReader reader = new FileReader(pathname);
// 建立一個對象,它把文件內容轉成計算機能讀懂的語言
BufferedReader br = new BufferedReader(reader)
) {
String line;
// 一次讀入一行數據
while ((line = br.readLine()) != null) {
text = text + line + "\r\n";
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(text);
//組裝每次讀取的每一行,寫入操作
writeFile(text);
}
//寫入文件
public static void writeFile(String line) {
try {
// 相對路徑,如果沒有則會建立一個新的output.log文件
File writeName = new File("C:\\Users\\jian.li\\Desktop\\output.log");
writeName.createNewFile(); // 創建新文件,有同名的文件的話直接覆蓋
try (FileWriter writer = new FileWriter(writeName);
BufferedWriter out = new BufferedWriter(writer)
) {
out.write(line);
// 把緩存區內容壓入文件
out.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
//讀取文件路徑(匹配指定文件夾內以.log結尾的文件路徑,自己可擴展)
public static void getfile(String filepath) throws FileNotFoundException, IOException {
try {
File file = new File(filepath);
if (!file.isDirectory()) {
System.out.println("文件");
System.out.println("path=" + file.getPath());
System.out.println("absolutepath=" + file.getAbsolutePath());
System.out.println("name=" + file.getName());
} else if (file.isDirectory()) {
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + "\\" + filelist[i]);
//只輸出文件以.log結尾的路徑
if (!readfile.isDirectory() && "log".equals(readfile.getName().substring(readfile.getName().lastIndexOf(".")+1,readfile.getName().length()))) {
System.out.println("path=" + readfile.getPath());
System.out.println("absolutepath="
+ readfile.getAbsolutePath());
System.out.println("name=" + readfile.getName());
//調用寫入方法
readFile(readfile.getPath());
} else if (readfile.isDirectory()) {
getfile(filepath + "\\" + filelist[i]);
}
}
}
} catch (FileNotFoundException e) {
System.out.println("readfile() Exception:" + e.getMessage());
}
}
//刪除文件
public static boolean delFile(String path) {
System.out.println("進入deFile方法 psth =: "+path);
boolean flag = false;
File file = new File(path);
System.out.println("操作文件文件,文件列表如下~~__--~~~~~~~~~~~~~~~~~~~:::"+file);
if (!file.exists()) {
return flag;
}
try{
flag = file.delete();
}catch (Exception e){
e.printStackTrace();
}
return flag;
}
//下載文件 advertis爲文件名 file爲文件所在路徑
public static void downFile( String advertis,HttpServletResponse response) throws IOException{
String file = "" + "/"+advertis;
FileInputStream fileInputStream = new FileInputStream(file);
//設置Http響應頭告訴瀏覽器下載這個附件,下載的文件名也是在這裏設置的
response.setHeader("Content-Disposition", "attachment;Filename=" + URLEncoder.encode(advertis, "UTF-8"));
OutputStream outputStream = response.getOutputStream();
byte[] bytes = new byte[2048];
int len = 0;
while ((len = fileInputStream.read(bytes))>0){
outputStream.write(bytes,0,len);
}
fileInputStream.close();
outputStream.close();
}
}