gpt4 book ai didi

java - 如何将数组中的文件连接到新文件夹中?

转载 作者:行者123 更新时间:2023-11-30 08:39:51 25 4
gpt4 key购买 nike

<分区>

我正在尝试编写一个带有两个参数的 java 程序,dirNamefileName。该程序将搜索 dirName 中以 .java 结尾的所有文件,然后将它们连接到一个名为 fileName 的新文件夹中。到目前为止,我有一种方法可以在 dirName 中搜索 .java 文件,然后我将它们放入一个名为 list 的文件数组中,但现在我正在努力以迭代方式将文件添加到这个数组到我的新文件夹 fileName。这是我目前所拥有的:

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

}

}

感谢您的宝贵时间。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com