Visakh S
Visakh S

Reputation: 1

Does adding two files in this way?

iam adding two files in this way but i got error message which is not add two files in this way.
File employ = new File("E:/employee.xml","E:/one/two/student.xml");

Upvotes: 0

Views: 63

Answers (4)

HJW
HJW

Reputation: 23443

I think you are trying to merge two XML files together into an XML file. You should look into Apache Commons Configurations if you want the merged file to make business sense.

CombinedConfiguration 

http://commons.apache.org/configuration/userguide/howto_combinedconfiguration.html

Upvotes: 1

Abubakkar
Abubakkar

Reputation: 15664

The constructor you used is mentioned in Java API docs as:

public File(String parent,String child)

Creates a new File instance from a parent pathname string and a child pathname string.

If parent is null then the new File instance is created as if by invoking the single-argument File constructor on the given child pathname string.

Otherwise the parent pathname string is taken to denote a directory, and the child pathname string is taken to denote either a directory or a file. If the child pathname string is absolute then it is converted into a relative pathname in a system-dependent way. If parent is the empty string then the new File instance is created by converting child into an abstract pathname and resolving the result against a system-dependent default directory. Otherwise each pathname string is converted into an abstract pathname and the child abstract pathname is resolved against the parent.

Parameters: parent - The parent pathname string child - The child pathname string

Throws: NullPointerException - If child is null

It is not for the purpose ofadding two files. You need to do some work yourself write some logic for adding the two files.

Upvotes: 1

Cacho Santa
Cacho Santa

Reputation: 6914

If by "adding two files" you mean that you want to concatenate two files, try this:

import java.io.*;  
import java.io.FileInputStream;  

public class CopyFile{  
    private static void copyfile(String srFile, String dtFile){  
        try{  
            File f1 = new File(srFile);  
            File f2 = new File(dtFile);  
            InputStream in = new FileInputStream(f1);  

            //For Append the file.  
            OutputStream out = new FileOutputStream(f2,true);  

            byte[] buf = new byte[1024];  
            int len;  
            while ((len = in.read(buf)) > 0){  
                out.write(buf, 0, len);  
            }  
            in.close();  
            out.close();  
            System.out.println("File copied.");  
        }  
        catch(FileNotFoundException ex){  
            System.out.println(ex.getMessage() + " in the specified directory.");  
            System.exit(0);  
        }  
        catch(IOException e){  
            System.out.println(e.getMessage());           
        }  
    }  
    public static void main(String[] args){  
        switch(args.length){  
            case 0: System.out.println("File has not mentioned.");  
                    System.exit(0);  
            case 1: System.out.println("Destination file has not mentioned.");  
                    System.exit(0);  
            case 2: copyfile(args[0],args[1]);  
                    System.exit(0);  
            default : System.out.println("Multiple files are not allow.");  
                      System.exit(0);  
        }  
    }  
}    

Hope it helps!

Upvotes: 0

Aniket Inge
Aniket Inge

Reputation: 25705

File employ = new File("E:\\employee.xml");
File employ2 = new File("E:\\one\\two\\student.xml");

??

Upvotes: 0

Related Questions