How to concatenate files from an array to into a new folder?
本问题已经有最佳答案,请猛点这里访问。
我正在尝试编写一个Java程序,它将采用两个参数,EDCOX1,0,EDCX1,1。程序将搜索以
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | import java.io.File; import java.io.FileInputStream; import java.io.FilenameFilter; import java.util.ArrayList; public class TwoFiles { File dir; File name; public TwoFiles(File dirName, File fileName) { dir = dirName; name = fileName; } public void setDir(File m) { this.dir = m; } public File getDir() { return dir; } public void setNewFolder(File n) { this.name = n; } public File getNewFolder() { return name; } public File[] Finder(File dir) { dir = getDir(); return dir.listFiles(new FilenameFilter() { public boolean accept(File dir, String filename) { return name.endsWith(".java"); } } ); } public static void main(String[] args) { File folder = null; File newFolder = null; Integer b = null; TwoFiles tf = new TwoFiles(folder, newFolder); folder = tf.getDir(); newFolder = tf.getNewFolder(); File[] list = tf.Finder(folder); //add to an array //here is where I've been experimenting to add files in `list` to new folder, `fileName`. for (File file : list) { FileInputStream inFile = new FileInputStream(file); while ((b = inFile.read()) != -1) newFolder.write(b); inFile.close(); } //copy files from array (list) into newFolder } } |
谢谢你的时间。
newfolder变量的类型为file。你不能写这个。我想,您的代码甚至不会编译。您必须在循环前面创建一个输出流:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | FileOutputStream fos = new FileOutputStream( newFolder); try { for (File file : list) { FileInputStream inFile = new FileInputStream(file); while ((b = inFile.read()) != -1) fos.write(b); inFile.close(); } } finally { fos.close(); } |
您可以使用apache commons io copy directory()和iofilefilter(用于
这是我对你问题的看法:我创建了另一个构造函数,在这个构造函数中,您只能将想要连接的文件的路径放到目录/文件夹,以及连接结果的文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | public class TwoFiles { private File dir; private File name; public TwoFiles(File dirName, File fileName) { dir = dirName; name = fileName; } public TwoFiles(String dirName, String destinationFileName) throws IOException{ dir=new File(dirName); if(!dir.isDirectory()){ throw new FileNotFoundException();//here your exception in case when dirName is file name instead folder name } name=new File(destinationFileName); if(!name.exists()){ name.createNewFile(); } } public void setDir(File m) { this.dir = m; } public File getDir() { return dir; } public void setNewFolder(File n) { this.name = n; } public File getNewFolder() { return name; } public void concatenateFiles() throws IOException{ File[] files=dir.listFiles(); for(File file: files){ if(file.getName().endsWith(".java")){ //check is right file prescribe(name, file); } } } /** prescribe file to new destination */ private void prescribe(File destination, File file) throws IOException { FileInputStream inFile = new FileInputStream(file); FileOutputStream writer=new FileOutputStream(destination, true); //true means next file will be write beginning from end of the file int x; while((x=inFile.read())!=-1){ writer.write(x); } String test=" "; //next line in file writer.write(test.getBytes()); writer.close(); inFile.close(); } public static void main(String...strings){ String dirName="C/myApp/model/entity"; String fileName="C:/Users/Dell/Desktop/temp/test.java"; try { new TwoFiles(dirName, fileName).concatenateFiles(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } |
}