Skip to content

Instantly share code, notes, and snippets.

@biodunalfet
Created January 9, 2017 17:04
Show Gist options
  • Save biodunalfet/1b08c92fc5babe7bd464004b1b086773 to your computer and use it in GitHub Desktop.
Save biodunalfet/1b08c92fc5babe7bd464004b1b086773 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
import java.io.*;
public class quest7 {
public static void main(String[] args){
print("Enter path to existing source file e.g myfolder/file1.txt : ");
Scanner input = new Scanner(System.in);
String sourcePath = input.nextLine();
print("Enter path and name of destination file e.g anotherFolder/copiedFile.txt: ");
String destPath = input.nextLine();
File source = new File(sourcePath);
File dest = new File(destPath);
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(source);
os = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
printLine("File transer successful!");
is.close();
os.close();
if (source.delete()){
print(sourcePath + " deleted!");
}
else{
print("Error deleting " + sourcePath);
}
}
catch(Exception e){
e.printStackTrace();
}
}
public static void print(String text){
System.out.print(text);
}
public static void printLine(String text){
System.out.println(text);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment