Link to home
Start Free TrialLog in
Avatar of Sreejith22
Sreejith22Flag for India

asked on

Reading a file and collecting data using java

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
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

You mean trawling a textfile for such keywords?
read the file line by line. If the line startsWith "INSERT INTO" concatenate the next lines until you reach the line that endsWith ";"
Avatar of Sreejith22

ASKER

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
i would appreciate any correct code snippets
Why would you need to separate the INSERT statements from the rest of the ddl file?
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());
        }
    }
}
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
ASKER CERTIFIED SOLUTION
Avatar of Valeri
Valeri
Flag of Bulgaria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
A potentially more serious problem is when the lines do not begin with 'INSERT INTO' or end with ';' (they've no need to)