Solved

Reading from a text file

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java example issue 5 42
Eclipse Help Java EE 5,6,7 Documentation, why not Java EE 8 8 41
swing controls 2 21
program arguments vs VM arguments 4 26
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

763 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