how to read in an input file.

hi, i am having trouble trying to figure out how to read in an input file and assign the input file to a set of varibles. Example: input file contains: cat,dog,mouse. And I want my program to read the input file and then assign it to varibles in my program such as: c=cat, d=dog, m=mouse. Thank you very much.

[316 byte] By [coffeeshopa] at [2007-11-26 17:15:30]
# 1

You should know the way that your file is organized.

If those values for example are splitted with just space

for example

****START OF FILE*****

cats mice dogs

****END OF FILE******

import java.io.*;

import java.util.*;

public class read_From_file{

public static void main(String[] argz) {

try{

String linefeed , varA , varB , varC;

File fin = new File("YOURFILENAME");

FileInputStream fis = new FileInputStream(fin);

BufferedReader br = new BufferedReader(new InputStreamReader(fis));

if((linefeed=br.readLine())!=null) {

line = linefeed.split(" ");//<--This is the splitting delimiter could be , or : or | or whatever

varA=line[0];

varB=line[1];

varC=line[2];

}

}catch( IOException ioe){

System.out.println(ioe);

}

}

this would only read the 1st line .

by replacing if with while or whatever applies to you , you can get your results

Cheers!

georousa at 2007-7-8 23:43:30 > top of Java-index,Java Essentials,Java Programming...
# 2
thanks. i will try this out asap
coffeeshopa at 2007-7-8 23:43:30 > top of Java-index,Java Essentials,Java Programming...
# 3
georous, how should I define line in my program. because when I compile it I am getting an error? thanks so much.
coffeeshopa at 2007-7-8 23:43:30 > top of Java-index,Java Essentials,Java Programming...
# 4
Lookup split in the API: http://java.sun.com/j2se/1.5/docs/api/java/lang/String.html#split(java.lang.String)
Rodney_McKaya at 2007-7-8 23:43:30 > top of Java-index,Java Essentials,Java Programming...
# 5
if you mean what type String of course , if you post the error msg maybe i could help more.. (and some part of your code that is prone to that error)
georousa at 2007-7-8 23:43:30 > top of Java-index,Java Essentials,Java Programming...