Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: File handling

  1. #1
    Junior Member
    Join Date
    Apr 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb File handling

    Hihi, I have some questions here and I hope that you all can help me out with them. So I was trying to create a Java program that mimics some of the functions that git has. But I need some guidance on the suitable ways to do it. Let's call the program not-git. It has the similar basic functions that git has, including the 1. status command showing the status of files in the respective folder (tracked/not tracked), 2. add command adding a file to the repository while tracking it (e.g. add "file.txt"), 3. commit command to commit the files being tracked with an increasing counter (commit id). 4. revert command to reverse the version of current file into the previous version with a new commit ID (e.g. revert 1 will revert current version into version of commitID 1 but now the new version will have a new commitID, if before reverting the commitID is 2 now the commitID will be 3 for this version). I have attached some images below (I tried uploading ppt, docs and pdf but none of them works) if the explanation above isn't clear enough. I have some ideas in mind but not sure whether it is feasible, so I will create a LinkedList that accepts file as its parameter and store it in a node when an add command is issued, and there will be a counter with a default value of 1 that increases per commit. Whenever a revert command is issued, the list will be used to lookup the content of the node index. The functions mentioned above were just the basic requirements to achieve the passing marks, to achieve better grades we were suggested to add extra features like modifying the status command into showing not only the tracked files but also the new files, untracked files, and the modified files. The other extra features to improve the program are also included in the file uploaded. Thank you for taking your time reading this and I appreciate any help provided!

    https://ibb.co/9qXyTRZ
    https://ibb.co/qyF5yLp
    https://ibb.co/5MZd71G
    https://ibb.co/tJ3hmc7
    https://ibb.co/9vdjYSN
    https://ibb.co/hB7FhSW

    The main concern here is how do I collect a pile of files and store them into one node of LinkedList. And also when I revert how do obtain the files from the prev commit and replace the current ones. Thank you in advance for any help incoming!
    And also how to know when the content of the file is altered (modified)? Since the assignment requires to display the status of files not only tracked/untracked but also modified/new.

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStreamReader;
     
    public class Main {
     
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            notGit newGit = new notGit();
            String s;
            System.out.println("Enter \"not-git\" to initialise the program:");
            do {
                s = br.readLine();
                if (!"not-git".equals(s)) {
                    System.out.println("Please enter the instructed command:");
                }
            } while (!"not-git".equals(s));
            newGit.check(s);
            while ((s = br.readLine()) != null) {
                if ("not-git".equals(s)) {
                    newGit = new notGit();
                }
                newGit.check(s);
            }
        }
    }

    import java.util.*;
    import java.io.*;
     
    public class notGit {
     
        LinkedList list; //stores file history
        LinkedList list1; //stores list for each commit
        int commitID; //increases per commit
     
        public notGit() { //initialization
            this.list = new LinkedList();
            this.list1 = new LinkedList();
            this.commitID = 1;
        }
     
        void add(File file) {
            list.add(file);
            System.out.printf("\"%s\" is now being tracked\n", file.getName());
        }
     
        void commit() {
            list1.add(list);
            list.clear();
            System.out.printf("files committed with commit id \"%d\"\n", commitID++);
        }
     
        void revert(int i) {
            list = (LinkedList) list1.get(i - 1);
            System.out.printf("revert to commit-id \"%d\" and committed with commit-id \"%d\"", --commitID, commitID += 2);
        }
     
        void status() {
            if (list.isEmpty()) {
                System.out.println("No file is being tracked.");
            } else {
                System.out.println("tracked files:");
                for (int i = 0; i < list.size(); i++) {
                    File n = (File) list.get(i);
                    System.out.println(n.getName());
                }
            }
        }
     
        void check(String s) throws IOException {
            if ("not-git".equals(s)) {
                System.out.println("Welcome to not-git!");
            } else if ("status".equals(s)) {
                status();
            } else if (s.startsWith("add")) {
                String[] arr = s.split("\"");
                File newF = new File(arr[1]);
                add(newF);
            } else if ("commit".equals(s)) {
                commit();
            } else if (s.startsWith("revert")) {
                String[] arr = s.split(" ");
                revert(Integer.parseInt(arr[1]));
            } else {
                System.out.println("Please enter the correct command: ");
            }
        }
    }
    Attached Images Attached Images
    Last edited by nathanieloyt; April 27th, 2019 at 08:39 PM.

Similar Threads

  1. java File handling
    By parthicseraj in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 20th, 2014, 02:11 AM
  2. [SOLVED] File handling issue
    By aesguitar in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 11th, 2014, 08:55 PM
  3. java file handling
    By MartinMc in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 16th, 2012, 04:07 PM
  4. Regarding File Handling
    By ravjot28 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 1st, 2010, 07:45 PM
  5. File handling in java
    By srikanth in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: February 22nd, 2010, 02:11 AM