iam以这种方式添加了两个文件,但是我收到了错误消息,这不会以这种方式添加两个文件 文件使用=新文件(“E:/employee.xml”,“E:/one/two/student.xml”);
答案 0 :(得分:1)
您使用的构造函数在Java API文档中提到:
公共文件(String parent,String child)
从父路径名字符串和子项创建新的
File
实例 pathname string。如果parent为null,则创建新的File实例,就像创建一样 在给定子节点上调用单参数File构造函数 pathname string。
否则父路径名字符串用于表示目录, 并且子路径名字符串用于表示目录 或文件。如果子路径名字符串是绝对的,那么它是 以系统相关的方式转换为相对路径名。如果 parent是空字符串,然后创建新的File实例 将子项转换为抽象路径名并解析结果 针对系统相关的默认目录。否则每个路径名 string被转换为抽象路径名和子抽象 pathname是针对父级解析的。
参数:
parent
- 父路径名字符串child
- 孩子 路径名字符串引发:
NullPointerException
- 如果孩子是null
这不是为了添加两个文件。您需要自己编写一些工作来添加两个文件。
答案 1 :(得分:1)
我认为您正在尝试将两个XML文件合并到一个XML文件中。如果您希望合并文件具有商业意义,则应该查看Apache Commons Configurations
。
CombinedConfiguration
http://commons.apache.org/configuration/userguide/howto_combinedconfiguration.html
答案 2 :(得分:0)
File employ = new File("E:\\employee.xml");
File employ2 = new File("E:\\one\\two\\student.xml");
...
答案 3 :(得分:0)
如果通过“添加两个文件”表示您想要连接两个文件,请尝试以下操作:
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);
}
}
}
希望它有所帮助!