Solved

Reading a file and collecting data using java

Posted on 2013-05-10
9
607 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
  • 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
 

Author Comment

by:Sreejith22
ID: 39155194
i would appreciate any correct code snippets
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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 500 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

747 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

10 Experts available now in Live!

Get 1:1 Help Now