Solved

Reading from a text file

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 84
stackato and cloud 4 85
javap bin 2 34
Android development question 2 37
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

770 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