Solved

Reading from a text file

Posted on 1998-04-08
2
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

630 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