Assignment
Design an algorithm for moving one file, that is copying a file from a source (a directory) to another source, and deleting the original. Consider that folders are files, too.
Step 1: Build a simple working program ([1])
import java.io.*;
public class mv{
public static void main(String args[]){
try{
// File (or directory) to be moved
File file = new File("C:\\ProgrameJava\\file.txt");
// Destination directory
File dir = new File("C:\\ProgrameJava\\myDir");
// Move file to new directory
boolean success = file.renameTo(new File(dir, file.getName()));
if (!success) {
System.out.println("File not moved");
}
}
catch (Exception ioe){
ioe.printStackTrace();
}
}
}
Step 2: Add code to check if source and destination exist, and print appropriate messages on the application console
Step 3: Give the names of the source and the destination as command line arguments
Step 4: Use FileInputStream and FileOutputStream to move the file ([4])
Bibliography
- http://www.java.happycodings.com/Core_Java/code50.html
- http://docs.oracle.com/javase/tutorial/essential/io/move.html
- http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#move%28java.nio.file.Path,%20java.nio.file.Path,%20java.nio.file.CopyOption...%29
- http://myflex.org/yf/java/slides/Unit8.pdf
- http://stackoverflow.com/questions/300559/move-copy-file-operations-in-java
No comments:
Post a Comment