Code to read a character in the file
hi,
i need a code that can read a character in the file. The example file is like this:
Quote:
The quick brown fox,,,
field 1, 344
request: 12344 fjsdlfkj
bit 1
bit 2
fields2
response fkljsdfljewir
bit 1
bit 2
bit 3
successfull
request
bit 1
bit 2
response
failed
Then, i will read that file(above) and then get the request word until before the response. Then get the response until before the word successful.
But my code below only shows per line basis not per word or character. :( please help me.
Sample output is like this:
Quote:
request: 12344 fjsdlfkj
bit 1
bit 2
fields2
response fkljsdfljewir
bit 1
bit 2
bit 3
=====
Code :
import java.awt.image.DataBuffer;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
public class ReadFile
{
private String fileName = null;
private int bufferSize = 1000;
public ReadFile ()
{}
public ReadFile (String fileName, int bufferSize)
{
this.setFileName (fileName);
this.setBufferSize (bufferSize);
}
public ReadFile (String fileName)
{
this(fileName, 1000);
}
public void setFileName (String fileName)
{
this.fileName = fileName;
}
public String getFileName ()
{
return this.fileName;
}
public void setBufferSize (int bufferSize)
{
this.bufferSize = bufferSize;
}
public int getBufferSize ()
{
return this.bufferSize;
}
public ArrayList read () throws java.io.FileNotFoundException, java.io.IOException
{
FileReader fr = new FileReader (this.getFileName());
BufferedReader br = new BufferedReader (fr);
ArrayList aList = new ArrayList (this.getBufferSize());
String line = null;
while ( (line = br.readLine()) != null)
{
aList.add(line);
}
br.close();
fr.close();
return aList;
}
public static void main (String args[]) //include main for testing purposes
{
try
{
ReadFile rf = new ReadFile("C:\\txn.log");
ArrayList a = rf.read();
if (a.size() > 0)
{
int i, x;
for (i=0; i<a.size(); i++)
{
if(a.contains("request")) {
String test = (String) a.get(i+1);
System.out.println (test);
}
}
System.out.println ("EOF");
}
}
catch (Exception e)
{
System.out.println (e.getMessage());
}
}
}
Re: File Reader... help please
Hello Truffy, welcome to the forums :)
So given this input file:
Quote:
The quick brown fox,,,
field 1, 344
request: 12344 fjsdlfkj
bit 1
bit 2
fields2
response fkljsdfljewir
bit 1
bit 2
bit 3
successfull
request
bit 1
bit 2
response
failed
What is your expected output? Is this it?
Quote:
request: 12344 fjsdlfkj
bit 1
bit 2
fields2
response fkljsdfljewir
bit 1
bit 2
bit 3
request
bit 1
bit 2
response
failed
Re: File Reader... help please
Quote:
Originally Posted by
JavaPF
Hello Truffy, welcome to the forums :)
So given this input file:
What is your expected output? Is this it?
hi. thank you for welcoming me.
yup, thats the output.
i just need to get the request data and its response.
Re: File Reader... help please
Hello Truffy,
Does this application need to be generic? Will it support other text files in the same request/response format or will you only be dealing with the example above?
Re: File Reader... help please
Quote:
Originally Posted by
JavaPF
Hello Truffy,
Does this application need to be generic? Will it support other text files in the same request/response format or will you only be dealing with the example above?
it will support other extension files. that program is reading server logs, meaning it is in tail, i just listen
to the file and if there is another transaction, i will get it again and parse it. do you think that file reader is the
best approached to that project? the server logs is on the back end, and i will make a front end logs. c++ linux is our
back end. one of my friend told me that i can used java using ssh but i cant figure out how to do it.