/*******************************************************************************************************************|
|   Assignment 2                                                                                                     |
|   Program is meant to extract a record from each file and create a new file for each record                        |
|   Programs creates a new directory and uses the directory for each new file                                        |
|                                                                                                                   |
|   written by Raymond Chappel                                                                                       |
|                                                                                                                   |
********************************************************************************************************************/

import java.io.*;
import java.io.File;
import java.util.Scanner;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Chappel_R_a1B 
{
 public static void main(String argv[])
 {//start main
 //----ask user for name of file they wish to input, used Data Structures and Abstractions with Java texbook by Frank M. Carrano as reference for this----//
     Scanner scanner = new Scanner (System.in);
     System.out.println("Please type in the name of the file you wish to import ('cd_catalog.xml', 'simple.xml', or 'plant_catalog.xml')");
     String fileName = scanner.next();

     //---------------Handles if file chosen is cd_catalog.xml---------------//

     if (fileName.equals("cd_catalog.xml"))
     {//start if
     //--------Create new folder in current directory,used mkyong from http://www.mkyong.com/java/how-to-create-directory-in-java/ as a source----------//
        File direc = new File("Records from cd_catalog.xml");
        if (!direc.exists()) 
        {//start if
            if (direc.mkdir()) 
            {//start if
                System.out.println("Directory is created!");
                    try
                    {//start try

                        File file = new File("cd_catalog.xml"); //imports cd_catalog.xml
                        BufferedWriter outFile=new BufferedWriter(new FileWriter("Records from cd_catalog.xml/test.txt")); // test creates new .txt in current directory
                        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                       DocumentBuilder db = dbf.newDocumentBuilder();
                        Document doc = db.parse(file);
                        doc.getDocumentElement().normalize();
                        NodeList nodeLst = doc.getElementsByTagName("CD");



                    }//end try
                    catch (Exception e) 
                    {//start catch
                     e.printStackTrace();
                    }//end catch
            } //end if
            else 
            {//start else
                System.out.println("Failed to create directory!");
            }//end else
        }//end if


     }//end if

     //------------------------handles if file chosen is simple.xml----------------------//
     if (fileName.equals("simple.xml"))
     {//start if
     //--------Create new folder in current directory,used mkyong from http://www.mkyong.com/java/how-to-create-directory-in-java/ as a source----------//
        File direc = new File("Records from simple.xml.xml");
        if (!direc.exists()) 
        {//start if
            if (direc.mkdir()) 
            {//start if
                System.out.println("Directory is created!");
            } //end if
            else 
            {//start else
                System.out.println("Failed to create directory!");
            }//end else
        }//end if

        //-------------extracts record from xml file and creates a new file for each record------------//
        File file = new File("simple.xml"); //imports simple.xml
        //TODO: code to extract record and create new files

     }//end if

     //--------------------handles if file chosen is plant_catalog.xml-----------------------//
     if (fileName.equals("plant_catalog.xml"))
     {//start if
        //--------Create new folder in current directory,used mkyong from http://www.mkyong.com/java/how-to-create-directory-in-java/ as a source----------//
        File direc = new File("Records from plant_catalog.xml");
        if (!direc.exists()) 
        {//start if
            if (direc.mkdir()) 
            {//start if
                System.out.println("Directory is created!");
            } //end if
            else 
            {//start else
                System.out.println("Failed to create directory!");
            }//end else
        }//end if

        //-------------extracts record from xml file and creates a new file for each record------------//

        File file = new File("plant_catalog.xml"); //imports plant_catalog.xml

     }//end if

 }//End main
}//end class

Recommended Answers

All 5 Replies

Do you have a specific question about this piece of code? Or was this intended to be a code snippet?

I assume he needs help with the
// TODO part.

I would suggest to get rid of a lot of those
//Start
and
//End
comments, it's make the code a bit ... ugly and busy.

you could've also posted the rest here.

To create directories use mkdir() and mkdirs() of class File which returns boolean value.

For example on to create directory in java refer below resource

https://www.flowerbrackets.com/how-to-create-directory-java/

commented: Please check the date of the posts before replying. -3
commented: Be timely. Also, can't see how this answers the top question. Spam? -4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.