import java.io.File;
/** /** * 将一个文件内容,写入另一个文件中 * @param filePath 源文件路径 文件夹 * @param toFilePath 目标文件路径。 * @throws IOException 抛出IO异常 */ public void getFileToFile(String filePath,String toFilePath) throws IOException{ File des = new File(toFilePath); if (!des.exists()) { // 判断是否存在,不存在就创建 des.createNewFile(); // 创建文件 } ArrayList<String> filePathlist =this.getFilePaths(filePath); if(filePathlist ==null && filePathlist.size()<=0) return; //如果文件夹下是空的,直接退出。 OutputStream output = null; FileInputStream input = null; output = new FileOutputStream(des); for (int i = 0; i < filePathlist.size(); i++) { input = new FileInputStream(filePathlist.get(i)); byte[] buffer = new byte[1024]; while (-1 != (input.read(buffer))) { output.write(buffer); } String temp = "\r\n"; output.write(temp.getBytes());//换行 } if (output != null) { try { if (input != null) output.close(); input.close(); } catch (IOException e) { e.printStackTrace(); } } } } |
|