Sreejith22
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
temp.sql
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 ";"
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
Basically, I need to get all the insert into sql statements contained in this attached "temp.sql" file
ASKER
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:\\yourF older\\tem p.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("INSER T 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());
}
}
}
==========================
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:\\yourF
// 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("INSER
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:
}
}
}
DataInputStream in = new DataInputStream(fstream);
DataInputStream is unnecessarily specialized for reading text. All you need is
BufferedReader br = new BufferedReader(new FileReader("temp.sql")); // (without the literal)
String concatenation should be avoided. Use StringBuilder insteadif (concatenate) statement += (strLine + "\n");
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
A potentially more serious problem is when the lines do not begin with 'INSERT INTO' or end with ';' (they've no need to)