Skip to content

Instantly share code, notes, and snippets.

@golenishchev
Created January 6, 2016 05:43
Show Gist options
  • Save golenishchev/f48c5994b74433902934 to your computer and use it in GitHub Desktop.
Save golenishchev/f48c5994b74433902934 to your computer and use it in GitHub Desktop.
Lesson14 v3. Delete file
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.File;
public class FileManager{
public static void copyFile(String firstFile, String secondFile) throws Exception {
try (
BufferedReader br = new BufferedReader(new FileReader(firstFile));
PrintWriter pw = new PrintWriter(new FileWriter(secondFile));
) {
int line;
while ((line = br.read()) != -1) {
pw.write(line);
}
}
System.out.println("All done");
}
public static void deleteFile(String fileToBeDeleted) throws Exception {
File file = new File(fileToBeDeleted);
if(file.delete()) {
System.out.println(file.getName() + " is deleted");
} else {
System.out.println("deletion failed");
}
}
public static void main(String[] args) throws Exception {
String firstFile = "first.txt";
String secondFile = "second3.txt";
String fileToBeDeleted = "somefile.txt";
copyFile(firstFile,secondFile);
deleteFile(fileToBeDeleted);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment