Solved

Reading from a text file

Posted on 1998-04-08
2
180 Views
Last Modified: 2010-04-16
I'm after a simple routine to read items from a straight text file. I'm new to java, coming from a VisBasic/Perl background, and the stuff I've seen in the API documentation seems to make what I want to do complex.

The file has two string values separated by semi-colons on each line. I want to read the values into variables.

For example, in Perl I would do:
$VarName=<FILEHANDLE>;
($Var1,$Var2)=split(/;/,$VarName;


Is there a simple routine like this in Java?
0
Comment
Question by:ism
2 Comments
 
LVL 2

Accepted Solution

by:
eelko earned 50 total points
ID: 1233964
// First you have to open a stream:
try
{
    in = new FileInputStream( filename );
}
catch( IOException e )
{
    System.err.println( "Can't open inputstream " + filename );
    System.exit(1);
}

// Then read a line from this stream
int inputBytesRead;
char inbuf = new char[80];

try
{
    inputBytesRead = in.read(inbuf);
}
catch( IOException e )
{
    System.err.println( "Can't read form input stream." );
}

// Then parse the line "val1/val2"
String str = new String( inbuf );
int idxSlash = str.indexOf('/')
String val1 = str.substring(0, idxSlash);
String val2 = str.subString(idxSlash+1);

// At the end close the input stream
try
{
    in.close();
}
catch( IOException e )
{
    System.err.println( "Can't close input stream." );
}

===================================
I hope I understand your Perl example right. So you see it isn't that complex, only some more code.

If another expert knows a better alternative I like to hear it.
0
 

Author Comment

by:ism
ID: 1233965
That'll do for me :)

Sorry about the delay to grade your answer...took a bit of a holiday last week.

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

708 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

16 Experts available now in Live!

Get 1:1 Help Now