reading a text document

Posted on 2012-09-11
Last Modified: 2012-09-17

I have following text data in a pipe | delimitted text file

999|5555|6666|PPP|9/9/2009 15:49|1444-AAA_111|FINISH
998|5554|6666|PPP|9/9/2009 15:49|1444-AAA_111|FINISH
997|5553|6666|PPP|9/9/2009 15:49|1444-AAA_111|FINISH
996|5552|6666|PPP|9/9/2009 15:49|1444-AAA_111|FINISH
I am reading above data into String array called rows.

Then i am calling following method like below from main method

The implementation code of checkDatabases(rows) is as below

private static void checkDatabases(String[] rows) {
System.out.println("rows.length : "+rows.length);
//above line printing 4 correctly as there are four lines in the text file
                  String username="";//this is first cell value of each line of the text
                    String password="";//this is second cell value of each line of the text
            PreparedStatement pstmt = null;      
            List<HashMap<String,String>> missingList = new ArrayList<HashMap<String,String>>();

// i need to manipulate array of 4*6 I believe here to read the values specifically first and second value to pass to below query(as username and password) to check againest database which I am not sure how to do it. ???

                        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
                        Statement stmt = con.createStatement();

                        String usernameColumn = genericImporterProp.getString("usernameColumn");
                        String passwordColumn = genericImporterProp.getString("passwordColumn");
                        ResultSet rs1 = stmt.executeQuery("select * from login where "  + usernameColumn + "='"+username+"' and " + passwordColumn + "='"+Double.parseDouble(stringCellValue)+"'");

                        boolean isExist = false;
                        if (rs1!=null) {

                              while ({
                                    String rec1 = rs1.getString(1);
                                    String rec2 = rs1.getString(2);
                                    System.out.println("rec1 is--"+rec1+"---rec2 is---"+rec2);

                        if (!isExist) {
                              HashMap<String,String> mapDetail =new HashMap<String,String>();

                              for(HashMap<String,String> newMapDetail : missingList) {
                                    String myUseridToPass = newMapDetail.get("username");
                                    String myPasswordToPass = newMapDetail.get("password");



                  catch(Exception e){


            System.out.println("Missing List : "+missingList);

            generateCsvFile("c://MissingList.csv", missingList);


Preferably without using java 1.5 generics is much good as server is running on java 1.4.
Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance
Question by:gudii9
    LVL 86

    Expert Comment

    I'd probably handle the file something like:

    import java.util.*;
    public class ReadIt {
        public static void main(String[] args) throws Exception {
            Scanner s = new Scanner(new File("data.txt"));
            while (s.hasNextLine()) {
                String[] creds = s.nextLine().split("\\|", 3);
        static void process(String[] row) {
            System.out.printf("username=%s,password=%s\n", row[0], row[1]);

    Open in new window

    LVL 7

    Author Comment

    i cannot use scanner as server running on java 1.4 not java 1.5. please advise
    LVL 86

    Accepted Solution

        public static void main(String[] args) throws Exception {
            BufferedReader in = null;
            try {
                in = new BufferedReader(new FileReader(new File("data.txt")));
                String line = null;
                while ((line = in.readLine()) != null) {
                    String[] creds = line.split("\\|", 3);
            } finally {
                if (in != null) {
                    try {
                    } catch (IOException e) { /* ignore */

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    Java 1603 Error 2 26
    json example 39 76
    Understanding websocket example in spring 1 20
    mapShare challenge 13 40
    I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now