2

I am trying to write a java program that will take two arguments, dirName and fileName. The program will search for all the files in dirName that end with .java and then concatenate them into a new folder called fileName. So far I have a method to search for .java files in dirName, I then put them in a file array called list but now I am struggling to iteratively add the files in this array to my new folder, fileName. Here is what I have so far:

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 

    }

}

Thanks for your time.

John Smith
  • 679
  • 1
  • 9
  • 17

3 Answers3

1

You can use the Apache Commons IO copyDirectory() with the IOFileFilter (for .java extensions) to copy your files from one directory to another. Before that you can ensure to create a new directory using forceMkdir() for your filename.

Rg90
  • 581
  • 4
  • 10
  • 28
1

Your newFolder variable is of type File. You cannot write into this. I assume, your code does not even compile. You have to create an output stream in front of your loop:

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();
}
Heri
  • 4,368
  • 1
  • 31
  • 51
0

It's my version of your problem: I created other constructor, where you can put only paths to directory/folder from you want concatenate files, and to file of concatenations result.

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();
    }
}

}

Victor1125
  • 642
  • 5
  • 16