我想将2个文件的内容复制到第三个文件中

时间:2016-02-19 07:51:33

标签: java

我已将一个文件内容副本的代码编写到另一个文件中。但我无法将第二个文件内容复制到第三个文件。 因为我写了以下代码:

    try {
        File infile = new File("d:\\vijay.txt");
        File outfile = new File("d:\\ajay.txt");

        FileInputStream instream = new FileInputStream(infile);
        FileOutputStream outstream = new FileOutputStream(outfile);

        byte[] buffer = new byte[1024];

        int length;

        while ((length = instream.read(buffer)) > 0) {
            outstream.write(buffer, 0, length);

        }
        instream.close();
        outstream.close();

        System.out.println("File Copied successfully");
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }

请帮助我,提前致谢。

4 个答案:

答案 0 :(得分:1)

执行以下操作:

try {
    // the files to be copied
    String[] filePaths = {"file1.txt", "file2.txt"};
    // out file
    File outfile = new File("d:\\ajay.txt");
    FileOutputStream outstream = new FileOutputStream(outfile);

    // loop to all files copied
    for (String filePath : filePaths) {
        FileInputStream instream = new FileInputStream(new File(filePath));
        byte[] buffer = new byte[1024];
        int length;
        while ((length = instream.read(buffer)) > 0) {
            outstream.write(buffer, 0, length);
        }
        // close each file on copy finished
        instream.close();
    }
    // at the end close the output stream
    outstream.close();
    System.out.println("File Copied successfully");
} catch (IOException ioe) {
    ioe.printStackTrace();
}

现在您可以将n个文件复制到一个文件中。

答案 1 :(得分:1)

如果你有Java 7,我建议你使用Files utils。例如:

Path source1 = Paths.get("src1.txt");
Path source2 = Paths.get("src2.txt");

Path destination = Paths.get("dest.txt");    
out = Files.newOutputStream(destination, CREATE, APPEND);

Files.copy(source1, destination, StandardCopyOption.REPLACE_EXISTING);
Files.copy(source2, destination);

答案 2 :(得分:-1)

你可以试试这个:

define('BR','<br />' );

$array_1 = array('1.5mm', '2.5mm', '3.5mm', '4.5mm','5.5mm','6.5mm','7.5mm');
$array_2 = array('G/H', 'E/F', 'BLACK');
$array_3 = array('SI', 'VS-SI', 'SI-I', 'I/PK');

foreach( $array_1 as $size ){
    foreach( $array_2 as $code ){
        foreach( $array_3 as $val ){
            echo $size.' '.$code.' '.$val.BR;   
        }
    }
}

output:
1.5mm G/H SI
1.5mm G/H VS-SI
1.5mm G/H SI-I
1.5mm G/H I/PK
1.5mm E/F SI
1.5mm E/F VS-SI
1.5mm E/F SI-I
1.5mm E/F I/PK
1.5mm BLACK SI
1.5mm BLACK VS-SI
1.5mm BLACK SI-I
1.5mm BLACK I/PK
2.5mm G/H SI
2.5mm G/H VS-SI
2.5mm G/H SI-I
2.5mm G/H I/PK
2.5mm E/F SI
2.5mm E/F VS-SI
2.5mm E/F SI-I
2.5mm E/F I/PK
2.5mm BLACK SI
2.5mm BLACK VS-SI
2.5mm BLACK SI-I
2.5mm BLACK I/PK
3.5mm G/H SI
3.5mm G/H VS-SI
3.5mm G/H SI-I
3.5mm G/H I/PK
3.5mm E/F SI
3.5mm E/F VS-SI
3.5mm E/F SI-I
3.5mm E/F I/PK
3.5mm BLACK SI
3.5mm BLACK VS-SI
3.5mm BLACK SI-I
3.5mm BLACK I/PK
4.5mm G/H SI
4.5mm G/H VS-SI
4.5mm G/H SI-I
4.5mm G/H I/PK
4.5mm E/F SI
4.5mm E/F VS-SI
4.5mm E/F SI-I
4.5mm E/F I/PK
4.5mm BLACK SI
4.5mm BLACK VS-SI
4.5mm BLACK SI-I
4.5mm BLACK I/PK
5.5mm G/H SI
5.5mm G/H VS-SI
5.5mm G/H SI-I
5.5mm G/H I/PK
5.5mm E/F SI
5.5mm E/F VS-SI
5.5mm E/F SI-I
5.5mm E/F I/PK
5.5mm BLACK SI
5.5mm BLACK VS-SI
5.5mm BLACK SI-I
5.5mm BLACK I/PK
6.5mm G/H SI
6.5mm G/H VS-SI
6.5mm G/H SI-I
6.5mm G/H I/PK
6.5mm E/F SI
6.5mm E/F VS-SI
6.5mm E/F SI-I
6.5mm E/F I/PK
6.5mm BLACK SI
6.5mm BLACK VS-SI
6.5mm BLACK SI-I
6.5mm BLACK I/PK
7.5mm G/H SI
7.5mm G/H VS-SI
7.5mm G/H SI-I
7.5mm G/H I/PK
7.5mm E/F SI
7.5mm E/F VS-SI
7.5mm E/F SI-I
7.5mm E/F I/PK
7.5mm BLACK SI
7.5mm BLACK VS-SI
7.5mm BLACK SI-I
7.5mm BLACK I/PK

答案 3 :(得分:-1)

试一试

public class Filescombining
{
       public static void main(String[] args) throws IOException
       {      
       ArrayList<String> list = new ArrayList<String>();
       try
       {
       BufferedReader br = new BufferedReader(new FileReader( "input1.txt"));
         BufferedReader r = new BufferedReader(new FileReader( "input2.txt"));
            String s1 =null;
            String s2 = null;

                         while ((s1 = br.readLine()) != null)
                         {                         
                                        list.add(s1);        
                         }
                         while((s2 = r.readLine()) != null)
                         {    
                                        list.add(s2);    
                         } 
       }
        catch (IOException e)
          {
            e.printStackTrace();
          }

           BufferedWriter writer=null;
           writer = new BufferedWriter(new FileWriter("output.txt"));
            String listWord;              
                   for (int i = 0; i< list.size(); i++)
                  {
                        listWord = list.get(i);
                       writer.write(listWord);
                       writer.write("\n");
                  }
                           System.out.println("completed");
                           writer.close();    
        }
    }

希望我的帮助能够快乐编码