我正在尝试编写一个带有两个参数的java程序,dirName
和fileName
。该程序将搜索以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
}
}
感谢您的时间。
答案 0 :(得分:1)
您可以将Apache Commons IO copyDirectory()与IOFileFilter(用于from rest_framework import serializers
from conjugations.models import Verb, Past
class PastSerializer(serializers.ModelSerializer):
class Meta:
model = Past
fields = ('first_singular','second_singular','third_singular')
class VerbSerializer(serializers.ModelSerializer):
past = PastSerializer()
class Meta:
model = Verb
fields = ('verb','verbal_noun','verbal_adjective','past','present',
'future','habitual_present','conditional','past_habitual',
'past_subjunctive','present_subjunctive','imperative')
def create(self, validated_data):
past_data = validated_data.pop('past')
verb = Verb.objects.create(**validated_data)
Past.objects.create(verb=verb, **past_data)
return verb
扩展名)一起使用,将文件从一个目录复制到另一个目录。在此之前,您可以确保使用.java
为filename
创建新目录。
答案 1 :(得分:1)
您的newFolder变量属于File类型。你不能写这个。我假设,你的代码甚至没有编译。您必须在循环前创建输出流:
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();
}
答案 2 :(得分:0)
这是我的问题版本: 我创建了其他构造函数,你可以在其中只放置目录/文件夹的路径,你想要连接文件,以及连接文件的结果。
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="\n"; //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();
}
}
}