Home > Java > javaTutorial > body text

Java Example - Copy file contents to another file

黄舟
Release: 2017-02-15 10:09:06
Original
1333 people have browsed it

The following example demonstrates using the read and write methods of the BufferedWriter class to copy the content of a file to another file:

/*
 author by w3cschool.cc
 Main.java
 */import java.io.*;public class Main {
   public static void main(String[] args) 
   throws Exception {
      BufferedWriter out1 = new BufferedWriter
      (new FileWriter("srcfile"));
      out1.write("string to be copied\n");
      out1.close();
      InputStream in = new FileInputStream
      (new File("srcfile"));
      OutputStream out = new FileOutputStream
      (new File("destnfile"));
      byte[] buf = new byte[1024];
      int len;
      while ((len = in.read(buf)) > 0) {
         out.write(buf, 0, len);
      }
      in.close();
      out.close();
      BufferedReader in1 = new BufferedReader
      (new FileReader("destnfile"));
      String str;
      while ((str = in1.readLine()) != null) {
         System.out.println(str);
      }
      in.close();
   }}
Copy after login

The output result of running the above code is:

string to be copied
Copy after login

The above is Java example - copy the content of a file to the content of another file. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!