Reputation: 1
Would like to know is there any method to read specific data from a text file? For example:
[January 1, 2014 3:57 AM] (EmailDeliveryOptionsHome.getAddEmailDeliveryOptions [Connection at : MayConnectionPool]) : Exception Encountered | (java.sql.SQLException: ORA-12899: value too large for column "BGADMIN"."TBLSTOPRINT_ACCT"."EMAIL_ADDR"
I would like to read the part where they says
EmailDeliveryOptionsHome.getAddEmailDeliveryOptions
but not the rest,I don't really know how to write program to not read the date and [ Connection at: ...... EMAIL_ADDR. Do I need to use buffered reader? Or can I use other method as well?
public static void main(String[] args) {
try
{ File myFile = new File("system.txt");
FileReader filereader= new FileReader(myFile);
int len=(int)myFile.length();
char [] chars = new char[len];
filereader.read(chars);
for(int i=0; i<len; i++){
System.out.print(chars[i]);
filereader.close();
}
}
catch(IOException e)
{ System.out.print("Error performing file reading");
}
this code reads everything, in the text file, how do I modify the text file so that it read only specific data?
Upvotes: 0
Views: 181
Reputation: 52185
You could take a look at the Pattern
Class since what you are reading seems to be in some defined format (pattern) and you are always looking for something in particular which can be extracted.
This simple code below should do what you are after:
String input = "[January 1, 2014 3:57 AM] (EmailDeliveryOptionsHome.getAddEmailDeliveryOptions [Connection at : MayConnectionPool]) : Exception Encountered | (java.sql.SQLException: ORA-12899: value too large for column \"BGADMIN\".\"TBLSTOPRINT_ACCT\".\"EMAIL_ADDR\"";
Pattern p = Pattern.compile("\\[Connection at\\s*:\\s*([^\\]]+)");
Matcher m =p.matcher(input);
if(m.find())
{
System.out.println(m.group(1));
}
This yields:
MayConnectionPool
A simple explanation of this regex can be found here.
What this does is as follows:
[
character is a special character in regex language, and thus must be escaped with an extra \
in front. As usual, an extra \
is needed to escape it for Java.Connection at
will match exactly those characters\\s
):
will match exactly that\\s
)([^\\]]+)
: In regular expression language, the round brackets ((
and )
respectively) denote a group capture. Anything which is between these brackets and is matched will be captures and made available later to access. These are needed when you want to extract, not just match information within a given string. The []
in regex denote a set of characters which you would like to match, so [abc]
will match a
, b
and c
, in no particular order. However, if you have characters which follow the ^
character, then, the effect is opposite meaning that it will match anything which is not within the square brackets. So in short, what [^\\]]+
is that it will match 1 or more repetitions of (+
) any character which is not a closing square bracket (\\]
). Anything which matches will be placed in a group which I am later accessing.Upvotes: 2
Reputation:
This is a quick but a bit dirty way that I use:
Use this (or use your own method) to get the file content:
Scanner s = new Scanner(new File("textfile.txt"));
String fileContent = "";
if(s.hasNextLine()) { // If multiline
while(s.hasNext()) {
fileContent += s.nextLine() + "\n";
}
}else { // If only one line
while(s.hasNext()) {
fileContent += " " + s.next();
}
// Remove the first space
fileContent = fileContent.trim();
}
To strip the data you wanted out of the file use:
String result = null;
String searchFor = "EmailDeliveryOptionsHome.getAddEmailDeliveryOptions";
if(fileContent.contains(searchFor)) {
result = fileContent.substring(
fileContent.indexOf(searchFor),
fileContent.indexOf(searchFor) + searchFor.length());
}
Now you have the result in a variable called result
or if it wasn't found result
is null.
Upvotes: 0