Solved

Reading from a text file

Posted on 1998-04-08
2
183 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Requested array size exceeds VM limit 3 84
Problem to start Neon 20 85
Java Jpanels and Jframe 8 28
tomcat not starting 6 29
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
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.

911 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

20 Experts available now in Live!

Get 1:1 Help Now