0

Consider there is a file with the name "test.txt" and it has below file structure (refer to the image attached) red indicates folders and blue indicates files. Now is it possible to write a code to identify parent nodes and child nodes and assign values to each node in a way that it specifies who is the parent?

My database structure: Here id is an auto_increment value, where the title can be a folder name or a file name (that is a parent and child node names), and parent_id is the foreign key that refers to the id.

CREATE TABLE category (
  id int(10) unsigned NOT NULL AUTO_INCREMENT,
  title varchar(255) NOT NULL,
  parent_id int(10) unsigned DEFAULT NULL,
  PRIMARY KEY (id),
  FOREIGN KEY (parent_id) REFERENCES category (id) 
    ON DELETE CASCADE ON UPDATE CASCADE);

Content of the file

nbk
  • 45,398
  • 8
  • 30
  • 47
  • Yes, it is possible. You'll need to learn how to access your database with Java. This will involve the JDBC (Java DataBase Connector) which is the java.sql.* package. You'll have to get the appropriate drive for the mysql database. – NomadMaker Apr 02 '20 at 16:59
  • ^ "drive" = "driver". – markspace Apr 02 '20 at 17:47

1 Answers1

0

Get the title (say dir) from Database using JDBC as suggested in above comments and then pick either of these options from this SO thread and use it appropriately in your service/view.

Once you get the title, just to give a hint, this is all you need:

public static void main(String[] args) {
    String maindirpath = "/your/main/dir/title";
    System.out.println("Printing files under : " + maindirpath);
    File maindir = new File(maindirpath);
    if (maindir.exists() && maindir.isDirectory()) {
        File arr[] = maindir.listFiles();
        RecursivePrint(arr, 0);
    }
}

static void RecursivePrint(File[] arr, int level) {
    for (File f : arr) {
        for (int i = 0; i < level; i++)
            System.out.print("\t");
        if (f.isFile())
            System.out.println("|_ " + f.getName());
        else if (f.isDirectory()) {
            System.out.println("[" + f.getName() + "]");
            RecursivePrint(f.listFiles(), level + 1);
        }
    }
}

Its a simple implementation. Hope you got the idea.

Ajay Kumar
  • 2,906
  • 3
  • 23
  • 46
  • This code prints files in a given directory (or title according to you that is fetched from DB). But that's not what I was looking for. I have a text file with the above structure...and I want to read that structure (identify parent and corresponding child nodes) and insert that data into the database. My apologies for the confusion. – niharika gadde Apr 07 '20 at 19:03
  • Okay in that case, replace system.out.println with your dbservice.save method. How about that? Apologies if I am still not able to understand. Hoping you got the idea now. – Ajay Kumar Apr 07 '20 at 21:30
  • The screenshot I have attached is the content of a text file. I just have to read the red-colored text as folders/parent nodes and blue colored text as files/child nodes. The code you have written above reads files from a directory... but in my case, there is only one text file with the text that looks like a file structure... – niharika gadde Apr 08 '20 at 00:19
  • Share some lines from your "that" text file (as is). So that its easier for everyone in SO to understand to find out the starting point. – Ajay Kumar Apr 08 '20 at 02:02