流讀取檔案檔案中的路徑並找到路徑檔案進行合併
阿新 • • 發佈:2021-09-08
package Test_packge; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.commons.lang3.StringUtils; public class IOinputAndOut { public static void main(String[] args) throws Exception { InputStream("D:/temp/txt/配置檔案_1631006681442.txt",""); } public static String InputStream(String pathname,String OutUTPath) throws FileNotFoundException { //不傳路徑預設路徑為讀取目錄 if (StringUtils.isBlank(OutUTPath)) { OutUTPath=pathname.substring(0,pathname.lastIndexOf("/")+1); } //根據路徑擷取輸出檔名稱 String fileName=pathname.substring(pathname.lastIndexOf("/")+1,pathname.lastIndexOf(".")); ; File file=new File(pathname); BufferedReader reader = null; String tempString = null; int line =1; String fileOutPath1 = OutUTPath+fileName+".mp4"; FileOutputStream fileOutputStream1 = new FileOutputStream(new File(fileOutPath1)); byte[] bytes1 = new byte[1024]; int length1 = 0; try { // System.out.println("以行為單位讀取檔案內容,一次讀一整行:"); reader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"GBK")); while ((tempString = reader.readLine()) != null) { //System.out.println("Line"+ line + ":" +tempString); line ++ ; //檔案中的路徑取出來 File file1 = new File(tempString); //System.err.println(tempString); if(!file1.exists()) continue; FileInputStream fis1 = new FileInputStream(file1); //取出來的位元組寫入指定檔案 while ((length1 = fis1.read(bytes1)) != -1) { fileOutputStream1.write(bytes1, 0, length1); } } reader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if(reader != null){ try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } System.err.println("檔案儲存路徑"+fileOutPath1); } return "已經完成"; } }