java - How to parse a text file and create a database record from it -


i'm trying make simple test generator. want button parse text file , add records database. questions , answers in text file. have been searching net examples can't find 1 matches situation.

the text file has header information want ignore until line starts "~ end of syllabus". want "~ end of syllabus" indicate beginning of questions. couple of lines after line "(" in seventh character position. want indicate question number line. question number line unique in "(" in seventh character position. want use indicator mark start of new question. in question number line, first 3 characters "t1a" question group. last part of t1a*01* question number within group.

so, can see need actual question text line , answer lines well. typically after 4 answer lines question terminator indicated "~~". don't know how able questions in text file. keep adding them array string? how access information file , add database. confusing me , way feel learn how works seeing example covers situation. here link text file i'm talking about:http://pastebin.com/3u3uwlhn

code:

public static void main(string args[]) {      string endofsyllabus = "~ end of syllabus";     path objpath = paths.get("2014hamtechnician.txt");     string[]  restoftextfile = null;      if (files.exists(objpath)){          file objfile = objpath.tofile();         try(bufferedreader in = new bufferedreader(                 new filereader(objfile))){              string line = in.readline();             list<string> linesfile = new linkedlist<>();              while(line != null){                 linesfile.add(line);                   line = in.readline();             }              system.out.println(linesfile);         }         catch(ioexception e){             system.out.println(e);         }     }     else{         system.out.println(                 objpath.toabsolutepath() + " doesn't exist");     }      /* create , display form */     java.awt.eventqueue.invokelater(new runnable() {         public void run() {             new a19015_form().setvisible(true);         }     }); } 

reading text file in java straight forward (and there sure other, more creative/efficient ways this):

try (bufferedreader reader = new bufferedreader(new filereader(path))) { //try resources needs jdk 7   int linenum = 0;  string readline; while ((readline = reader.readline()) != null) { //read until end of stream 

skipping arbitrary amount of lines can accomplished this:

   if (linenum == 0) {        linenum++;        continue;    } 

your real problem text split on. had been using csv use string[] nextline = readline.split("\t"); split each line respective cells based on tab separation. not, you'll stuck reading each line, , find split on.

it seems you're in control of text file format. if are, go easier consume format such csv, otherwise you're going designing custom parser format.

a bonus using csv can mirror database effectivly. i.e. csv header column = database column.

as far databases go, using jdbc easy enough, make sure use prepared statements insert data prevent against sql injection:

     public connection connecttodatabase(){           string url = "jdbc:postgresql://url";           return drivermanager.getconnection(url);          }       connection conn = connecttodatabase();      preparedstatement pstinsert = conn.preparestatement(cinsert);      pstinsert.settimestamp(1, fromts1);      pstinsert.setstring(2, nextline[1]);      pstinsert.execute();      pstinsert.close();      conn.close(); 

--edit--

i didn't see pastebin earlier on. doesn't appear you're in charge of file format, you're going need split on spaces ( each word ) , rely on regular expressions determine if question or not. fortunately seems file consistent should able without problem.

--edit 2--

as possible solution can try untested code:

try{         bufferedreader reader = new bufferedreader(new filereader("file.txt")); //try resources needs jdk 7          boolean doregex = false;         string readline;         while ((readline = reader.readline()) != null) { //read until end of stream             if(readline.startswith("~~ end of syllabus")){                                   doregex = true;                 continue;   //immediately goto next iteration             }             if(doregex){                 string[] line = readline.split(" "); //split on spaces                 if(line[0].matches("your regex here")){                       //answer should line[1]                       //do logic answer here                 }                                }         }     } catch (ioexception e) {         e.printstacktrace();  //to change body of catch statement use file | settings | file templates.     } 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -