How to parse a text, in this case a file from work, but can be applied to any text file.

import java.io.*; import java.util.ArrayList; import java.util.StringTokenizer; public class ReadWriteTextFile { //aFile: ruta completa con nombre de archivo //sluVar: solo ruta de ubicacion del archivo public ArrayList getContents(File aFile, String sluVar) { StringBuffer contents = new StringBuffer(); ArrayList salida = new ArrayList(); BufferedReader input = null; try { input = new BufferedReader(new FileReader(aFile)); String line = null; String group = ""; boolean flag = false; boolean flag1 = false; String slu = sluVar; //System.out.println(slu); while ((line = input.readLine()) != null) { //System.out.println(line); StringTokenizer st = new StringTokenizer(line, "/"); //System.out.println("++++++++++++++" + st.toString()); if (st.countTokens() == 5) { st.nextToken(); String tipo = st.nextToken(); String period = ""; String hour1 = ""; String hour2 = ""; //System.out.println("++++++++++++++" + tipo); if (tipo.contains("CE")) { StringTokenizer stName = new StringTokenizer(tipo); stName.nextToken(); slu = stName.nextToken(); } if (tipo.equals("DTCAP_PER_SSN")) { //slu = stName.nextToken(); //System.out.println("--------------------------"); if(flag){ salida.add(""+contents.toString()); } flag = true; period = st.nextToken(); hour1 = st.nextToken(); hour2 = st.nextToken(); //System.out.println(slu + " " + tipo + " " + period + " " + hour1 + " " + hour2 + " " + "5555555555555"); contents = new StringBuffer(); contents.append(slu + "|" + period + "-" + hour1 + "-" + hour2 + "-" + tipo); } //else { //flag = false; //} } else if ((st.countTokens() == 2) && flag) { String name = st.nextToken(); String count = st.nextToken(); //System.out.println(name + " " + "222222222" + " " + count); contents.append("|" + name + " " + count); //System.out.println("///////////////////////////////"+contents.toString()); //salida.add("" + contents.toString()); } } } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } finally { try { if (input != null) { input.close(); } } catch (IOException ex) { ex.printStackTrace(); } } salida.add(contents.toString()); //System.out.println(contents.toString()); return salida; } //a.run(salida, sgu, fecha); //salida=ruta completa con nombre de archivo //sgu= solo ruta public void run(String fileEntrada, String sguFinal, String fechaFinal) { PrepaidDAO dao = new PrepaidDAO( "oracle.jdbc.OracleDriver", // identificacion del driver "DB NAME", // acceso a una cierta BD "IP", // que se encuentra en un cierto servidor 1521, // bajo un cierto puerto de acceso "USER", // autentificacion del usuario a la BD "PASS" ); File testFile = new File(fileEntrada); ArrayList result = getContents(testFile, sguFinal);//AQUI CONTINUAR //System.out.println("CECECECECECECEC//////////////////////////////////////////////////////"); //System.out.println(result.toString()); String fecha = fechaFinal; while (!result.isEmpty()) { String entrada = "" + result.remove(0); //System.out.println("*********************" + entrada); StringTokenizer st = new StringTokenizer(entrada, "|"); //VERIFICA SI NO HAY BASURA EN LA LINEA if (st.hasMoreTokens()) { String sgu = st.nextToken(); //agarra el nombre de SGU //System.out.println("++++++++++++++++++++++++++++++++++++++++++" + slu); String data = st.nextToken();//30-00:30-01:00-DTCAP_PER_SSN StringTokenizer stData = new StringTokenizer(data, "-"); String period = stData.nextToken(); //30 String hour1 = stData.nextToken(); // 00:00 String hour2 = stData.nextToken(); // 00:30 String dataType = stData.nextToken();//DTCAP_PER_SSN //System.out.println("8888888888888888888" + dataType); if (dataType.equals("DTCAP_PER_SSN")) { int BEGIN_IN_146, CONTINUE_IN_146, END_IN_146, ABORT_IN_146, BEGIN_OUT_146, CONT_OUT_146, END_OUT_146, ABORT_OUT_146, IBEGIN_ABRT_146, IBEGIN_IGN_146, ICONT_ABRT_146, IEND_IGN_146, IABORT_IGN_146, SLU_SFAIL_146, TCAP_TO_SLU_146, TOT_SSN_SEL_146, TOT_PDUS, LOST_PDUS, OOS_PDUS, DUP_PDUS; BEGIN_IN_146= CONTINUE_IN_146= END_IN_146= ABORT_IN_146= BEGIN_OUT_146= CONT_OUT_146= END_OUT_146= ABORT_OUT_146= IBEGIN_ABRT_146= IBEGIN_IGN_146= ICONT_ABRT_146= IEND_IGN_146= IABORT_IGN_146= SLU_SFAIL_146= TCAP_TO_SLU_146= TOT_SSN_SEL_146= TOT_PDUS= LOST_PDUS= OOS_PDUS= DUP_PDUS = 0; //System.out.println(dataType); while (st.hasMoreTokens()) { String s = st.nextToken(); //BEGIN_IN_146 2811075 StringTokenizer stCE = new StringTokenizer(s); //BEGIN_IN_146 2811075 - divide en espacios vacios String tipoDato = stCE.nextToken(); //BEGIN_IN_146 String cantidad = stCE.nextToken(); // 2811075 if (tipoDato.equals("BEGIN_IN_146")) { //System.out.println(s); BEGIN_IN_146 = Integer.parseInt(cantidad); } else if (tipoDato.equals("CONTINUE_IN_146")) { //System.out.println(s); CONTINUE_IN_146 = Integer.parseInt(cantidad); } else if (tipoDato.equals("END_IN_146")) { //System.out.println(s); END_IN_146 = Integer.parseInt(cantidad); } else if (tipoDato.equals("ABORT_IN_146")) { //System.out.println(s); ABORT_IN_146 = Integer.parseInt(cantidad); } else if (tipoDato.equals("BEGIN_OUT_146")) { //System.out.println(s); BEGIN_OUT_146 = Integer.parseInt(cantidad); } else if (tipoDato.equals("CONT_OUT_146")) { //System.out.println(s); CONT_OUT_146 = Integer.parseInt(cantidad); } else if (tipoDato.equals("END_OUT_146")) { //System.out.println(s); END_OUT_146 = Integer.parseInt(cantidad); } else if (tipoDato.equals("ABORT_OUT_146")) { //System.out.println(s); ABORT_OUT_146 = Integer.parseInt(cantidad); } else if (tipoDato.equals("IBEGIN_ABRT_146")) { //System.out.println(s); IBEGIN_ABRT_146 = Integer.parseInt(cantidad); } else if (tipoDato.equals("IBEGIN_IGN_146")) { //System.out.println(s); IBEGIN_IGN_146 = Integer.parseInt(cantidad); } else if (tipoDato.equals("ICONT_ABRT_146")) { //System.out.println(s); ICONT_ABRT_146 = Integer.parseInt(cantidad); } else if (tipoDato.equals("IEND_IGN_146")) { //System.out.println(s); IEND_IGN_146 = Integer.parseInt(cantidad); } else if (tipoDato.equals("IABORT_IGN_146")) { //System.out.println(s); IABORT_IGN_146 = Integer.parseInt(cantidad); } else if (tipoDato.equals("SLU_SFAIL_146")) { //System.out.println(s); SLU_SFAIL_146 = Integer.parseInt(cantidad); } else if (tipoDato.equals("TCAP_TO_SLU_146")) { //System.out.println(s); TCAP_TO_SLU_146 = Integer.parseInt(cantidad); } else if (tipoDato.equals("TOT_SSN_SEL_146")) { //System.out.println(s); TOT_SSN_SEL_146 = Integer.parseInt(cantidad); } else if (tipoDato.equals("TOT_PDUS")) { //System.out.println(s); TOT_PDUS = Integer.parseInt(cantidad); } else if (tipoDato.equals("LOST_PDUS")) { //System.out.println(s); LOST_PDUS = Integer.parseInt(cantidad); } else if (tipoDato.equals("OOS_PDUS")) { //System.out.println(s); OOS_PDUS = Integer.parseInt(cantidad); } else if (tipoDato.equals("DUP_PDUS")) { //System.out.println(s); DUP_PDUS = Integer.parseInt(cantidad); } } dao.insertDTCAP_PER_SSN(fecha + hour1, fecha + hour2, period, sgu, BEGIN_IN_146, CONTINUE_IN_146, END_IN_146, ABORT_IN_146, BEGIN_OUT_146, CONT_OUT_146, END_OUT_146, ABORT_OUT_146, IBEGIN_ABRT_146, IBEGIN_IGN_146, ICONT_ABRT_146, IEND_IGN_146, IABORT_IGN_146, SLU_SFAIL_146, TCAP_TO_SLU_146, TOT_SSN_SEL_146, TOT_PDUS, LOST_PDUS, OOS_PDUS, DUP_PDUS); } } } } public static void main(String[] aArguments) throws IOException { String slu = "test"; } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.