Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reading a file and collecting data using java

Posted on 2013-05-10
9
Medium Priority
?
635 Views
Last Modified: 2013-06-06
From the given sql file, how can i read and store all the insert statements in an ArrayList<String> using java code? Any help with code snippet is much appreciated with points.
temp.sql
0
Comment
Question by:Sreejith22
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 16

Expert Comment

by:krakatoa
ID: 39155176
You mean trawling a textfile for such keywords?
0
 
LVL 16

Expert Comment

by:Valeri
ID: 39155189
read the file line by line. If the line startsWith "INSERT INTO" concatenate the next lines until you reach the line that endsWith ";"
0
 

Author Comment

by:Sreejith22
ID: 39155192
start should be "INSERT INTO" and end should be ");"  .  When it first hits ");" , after an "INSERT INTO" that entire start to end should be an entry of array list.

Basically, I need to get all the insert into sql statements contained in this attached "temp.sql" file
0
Give Your Engineering Team a Productivity Boost

Learn why container technology is so powerful and how it can provide your team with productivity gains and other benefits.

 

Author Comment

by:Sreejith22
ID: 39155194
i would appreciate any correct code snippets
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39155280
Why would you need to separate the INSERT statements from the rest of the ddl file?
0
 
LVL 16

Expert Comment

by:Valeri
ID: 39155281
Here is the code. After the test you can remove the lines that contain "System.out.println".
==========================================================================

import java.io.FileInputStream;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.List;
import java.util.ArrayList;

public class ReadInserts {

    private static List<String> insertsInto = new ArrayList<String>();

    public static void main(String[] args) {
        try {
            // Open the file
            FileInputStream fstream = new FileInputStream("c:\\yourFolder\\temp.sql");
            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
           
            String strLine; boolean concatenate = false; String statement = "";
            //Read File Line By Line
            while ((strLine = br.readLine()) != null)   {
                // print the content on the console
                System.out.println(strLine);

                if (strLine.startsWith("INSERT INTO")) {
                    concatenate = true;
                }

                if (concatenate) statement += (strLine + "\n");

                if (strLine.endsWith(";")) {
                    concatenate = false;
                    if (statement.length() > 0) System.out.println(">>> : " + statement);
                    if (statement.length() > 0) insertsInto.add(statement);
                    statement = "";
                }
            }
            //Close the input stream
            in.close();
        } catch (Exception e){//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
    }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39155354
DataInputStream in = new DataInputStream(fstream);

Open in new window


DataInputStream is unnecessarily specialized for reading text. All you need is
BufferedReader br = new BufferedReader(new FileReader("temp.sql")); // (without the literal)

Open in new window

if (concatenate) statement += (strLine + "\n");

Open in new window

String concatenation should be avoided. Use StringBuilder instead
0
 
LVL 16

Accepted Solution

by:
Valeri earned 2000 total points
ID: 39160598
Yes, CEHJ is right. DataInputStream is not needed in this case. please change the code in this way:
==================================================================
import java.io.*;
import java.util.List;
import java.util.ArrayList;

public class ReadInserts {

    private static List<String> insertsInto = new ArrayList<String>();

    public static void main(String[] args) {
        try {
            // Open the file
            FileReader fileReader = new FileReader("c:\\yourFolder\\temp.sql");
            BufferedReader buffReader = new BufferedReader(fileReader);

            String strLine; boolean concatenate = false; String statement = "";
            //Read File Line By Line
            while ((strLine = buffReader.readLine()) != null)   {
                // print the content on the console
                System.out.println(strLine);

                if (strLine.startsWith("INSERT INTO")) {
                    concatenate = true;
                }

                if (concatenate) statement += (strLine + "\n");

                if (strLine.endsWith(";")) {
                    concatenate = false;
                    if (statement.length() > 0) System.out.println(">>> : " + statement);
                    if (statement.length() > 0) insertsInto.add(statement);
                    statement = "";
                }
            }
            buffReader.close();
        } catch (Exception e){//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
    }
}

=======================================
String concatenation is not harmful in this case.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39160722
A potentially more serious problem is when the lines do not begin with 'INSERT INTO' or end with ';' (they've no need to)
0

Featured Post

Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

730 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