Reading data from Text file

I

imran

Hello,

I have a problem on reading data from Text file(notepad) and save as a
variable.Data that i want to get from text file are shown below (
sample1, 3, add).Below are java code just to get "sample1" put in
classString. How can i modify the code to get "3" and "add". Can
anyone help me and i also welcome with the java code.

Thank you...



------------Text file (injector.txt)------------
classname = sample1
paramtypes = 3
methodname = add
-------------------------------------------------



-------------Java source code---------------------
static String classString
 
R

Ryan Dillon

imran said:
Hello,

I have a problem on reading data from Text file(notepad) and save as
a

It looks like a Properties object is what you need. You could do
something like:

FileInputStream fis = new FileInputStream("injector.txt");
Properties props = new Properties();
props.load(fis);

String classString = props.getProperty("classString");
....
 
M

Malte

imran said:
Hello,

I have a problem on reading data from Text file(notepad) and save as a
variable.Data that i want to get from text file are shown below (
sample1, 3, add).Below are java code just to get "sample1" put in
classString. How can i modify the code to get "3" and "add". Can
anyone help me and i also welcome with the java code.

Thank you...



------------Text file (injector.txt)------------
classname = sample1
paramtypes = 3
methodname = add
-------------------------------------------------



-------------Java source code---------------------
static String classString
.
.
.

public void operateOnFile()throws IOException
{
RandomAccessFile f = new
RandomAccessFile("D:\\injector.txt","rw");
long length = f.length();
long position = 0;
long old_position = 0;
String content;



f.seek(0);

while (position < length)
{
old_position = position;
content = f.readLine();

StringTokenizer s = new StringTokenizer (content,"=");
while (s.hasMoreTokens())
{
String string_val = s.nextToken();
if (string_val.trim().equals("classname".trim()))
{
classString=s.nextToken();
}
}

position = f.getFilePointer();
}
---------------------------------------------------

Why not just read the line, then call split() on the String?
 
D

Doni Ocena

Your problem is that you are only reading one line. You need a loop to
iterate through the rest of the lines.

while ((line = f.readLine()) != null) {
//do your thing here
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top