Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- public class TurtleFilesManager {
- public String[] neededPrograms = { "install.lua", "setLabel.lua" };
- public String mainPath = "C:\\Users\\noah1\\Twitch\\Minecraft\\Instances\\CC Simple\\saves\\Testing\\computercraft\\computer";
- public TurtleFilesManager() {
- // listDirs(mainPath);
- updatePrograms(mainPath);
- // listDirs(mainPath);
- }
- public void updatePrograms(String path) {
- File sourceFolder = new File(path + "\\src");
- File[] ids = new File(path).listFiles();
- int copies = 0, failedCopies = 0, deletions = 0, failedDeletions = 0;
- if (ids != null) {
- for (File id : ids) {
- if (!id.getName().equals("src")) {
- File[] programs = id.listFiles();
- String existingPrograms = "";
- if (programs != null)
- for (File ep : programs)
- existingPrograms += ep.getName();
- for (String neededProgram : neededPrograms) {
- System.out.println("Updating " + id.getName() + "\\" + neededProgram + ":");
- File newProgram = new File(id.getAbsolutePath() + "\\" + neededProgram);
- if (existingPrograms.contains(neededProgram)) {
- System.out.print(" > Found old -> Deleting... ");
- if (newProgram.delete()) {
- deletions++;
- System.out.println("Success!");
- } else {
- failedDeletions++;
- System.out.println("Failed!");
- }
- } else {
- System.out.println(" > No old found");
- }
- System.out.print(" > Copying... ");
- File sourceProgram = new File(sourceFolder.getAbsolutePath() + "\\" + neededProgram);
- String reason = copyFile(sourceProgram, newProgram);
- if (newProgram.exists()) {
- copies++;
- System.out.println("Success!");
- } else {
- failedCopies++;
- System.out.println("Failed!");
- System.out.println(" > Reason: " + reason);
- }
- System.out.println();
- }
- }
- }
- }
- System.out.println(
- ">>> Copied " + copies + " file" + (copies == 1 ? "" : "s") + " (" + failedCopies + " failed) <<<");
- System.out.println(">>> Deleted " + deletions + " file" + (deletions == 1 ? "" : "s") + " (" + failedDeletions
- + " failed) <<<\n");
- }
- public String copyFile(File source, File dest) {
- if (!source.exists())
- return "source file doesn't exist";
- try {
- InputStream is = new FileInputStream(source);
- OutputStream os = new FileOutputStream(dest);
- byte[] buffer = new byte[1024];
- int length;
- while ((length = is.read(buffer)) > 0) {
- os.write(buffer, 0, length);
- }
- is.close();
- os.close();
- return "successful";
- } catch (Exception e) {
- e.printStackTrace();
- return "failed";
- }
- }
- public void listDirs(String path) {
- File allFilesDir = new File(path);
- File[] folders = allFilesDir.listFiles();
- System.out.println("=== All Computer Files ===");
- for (File file : folders) {
- System.out.println("ID: " + file.getName());
- File[] programs = file.listFiles();
- if (programs != null)
- for (File p : programs) {
- if (p.getName().contains(".lua")) {
- System.out.println(" -" + p.getName());
- }
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement