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