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.
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!