Guest User

Untitled

a guest
Apr 20th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 KB | None | 0 0
  1.     public static void outputTree(Node node, String toPath) throws IOException {
  2.         try {
  3.             FileOutputStream fileOut = new FileOutputStream(toPath);
  4.             ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
  5.             objectOut.writeObject(node);
  6.             objectOut.flush();
  7.             objectOut.close();
  8.             echo("*** Outputted Data Successfully To File");
  9.             echo();
  10.         } catch (Exception e) {
  11.             echo("Error Outputting File: " + e.getMessage());
  12.         }
  13.     }
  14.  
  15.     public static Node readTreeAtPath(String path) throws IOException {
  16.         try {
  17.             createTreeAtPath(path, false);
  18.             FileInputStream fileIn = new FileInputStream(path);
  19.             ObjectInputStream objectIn = new ObjectInputStream(fileIn);
  20.             Node node;
  21.             node = (Node)objectIn.readObject();
  22.             return node;
  23.         } catch (Exception e) {
  24.             echo("Error Reading File: " + e.getMessage());
  25.         }
  26.         return null;
  27.     }
  28.    
  29.     public static void deleteTreeAtPath(String path) {
  30.         File file = new File(path);
  31.         file.delete();
  32.     }
  33.    
  34.     public static void createTreeAtPath(String path, boolean overwrite) throws IOException {
  35.         File file = new File(path);
  36.        
  37.         if (file.exists() && overwrite == true) {
  38.             deleteTreeAtPath(path);
  39.         } else if (file.exists() == false) {
  40.             echo("*** Creating new binary tree...");
  41.             echo();
  42.             Node node = new Node();
  43.             node.type = NodeType.QUESTION;
  44.             node.question = "Has it ever existed?";
  45.             outputTree(node, path);
  46.         }
  47.     }
Add Comment
Please, Sign In to add comment