Java implements a copy of a text file

Source: Internet
Author: User

Package Com.starain.io;

Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import java.io.FileNotFoundException;
Import Java.io.FileReader;
Import Java.io.FileWriter;
Import Java.util.Scanner;

public class CopyFile {

public static void Main (string[] args) {

try {
BufferedWriter Writer11 = new BufferedWriter (New FileWriter ("D:/t.txt"));
Writer11.write ("1");
Writer11.newline ();
Writer11.write ("2");
Writer11.newline ();
Writer11.write ("3");
Writer11.newline ();
Writer11.write ("4");
Writer11.newline ();
Writer11.write ("5");
Writer11.newline ();
Writer11.write ("6");
Writer11.newline ();
Writer11.write ("7");
Writer11.newline ();
Writer11.write ("8");
Writer11.newline ();
Writer11.write ("9");
Writer11.newline ();
Writer11.write ("0");
Writer11.newline ();
Writer11.close ();
} catch (Exception e) {
System.out.println ("Failed to create file");
}

System.out.println ("Please enter the source file name (e.g. D:/t.txt), but you need to ensure that the file exists");
Scanner Scanner = new Scanner (system.in);
String sfilepathname = Scanner.next ();
System.out.println ("Please enter the destination file name (e.g. D:/tester2.txt)");
String dfilepathname = Scanner.next ();
Copyandpaste copy = new Copyandpaste ();
Copy.setsfilepathname (Sfilepathname);
Copy.setdfilepathname (Dfilepathname);
Copy.copyandpaste ();
}

}
Class copyandpaste{
Private String sfilepathname;//Source file address
Private String dfilepathname;//destination file address
BufferedReader Bufferedread;
BufferedWriter Bufferedwrite;
String Line;
Public Copyandpaste () {

}
Public Copyandpaste (String sfilepathname,string dfilepathname) {
This.setsfilepathname (Sfilepathname);
This.setdfilepathname (Dfilepathname);
}
public void Setsfilepathname (String sfilepathname) {
This.sfilepathname = Sfilepathname;
}
Public String Getsfilepathname () {
return sfilepathname;
}
public void Setdfilepathname (String dfilepathname) {
This.dfilepathname = Dfilepathname;
}
Public String Getdfilepathname () {
return dfilepathname;
}

public void Copyandpaste () {



try {
Bufferedread = new BufferedReader (new FileReader (Sfilepathname));
} catch (FileNotFoundException e) {
SYSTEM.OUT.PRINTLN ("file read false");
}
try {
Bufferedwrite = new BufferedWriter (new FileWriter (Dfilepathname));
} catch (Exception e) {
System.out.println ("File write false");
}
try {
line = Bufferedread.readline ();
System.out.println ("Text content:");
while (Line!=null) {
System.out.println (line);
Bufferedwrite.write (line);
Bufferedwrite.newline ();
line = Bufferedread.readline ();
}
System.out.println ("Copy complete \nsuccess");
} catch (Exception e) {
System.out.println ("Copy and Paste false");
}
try {
Bufferedread.close ();
} catch (Exception e) {
System.out.println ("read close false");
}
try {
Bufferedwrite.close ();
} catch (Exception e) {
System.out.println ("Write Close false");
}
}
}

Java implements a copy of a text file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.