Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Reading in Text File for Java?
I'm doing an assignment where I have to read in a text file using Java.
I use Eclipse.
The text file will be called something like "command.txt"
It will contain information for commands such as:
START 2
GET 3000
The first word will be a command with the second being some kind of process/integer name.
How do I read in these commands for my code to use in the program? For example, how can I read in "START" and process the information to check for the command so that the program can initialize the START action?
Thanks for the help. Hope my question was clear enough to answer!
I notice that the first answer reads in line by line. How can I make it so that it reads in command by command. For example, rather than reading in just "START 2"
How can I read in "START" then "2" separately?
I notice that the first answer reads in line by line. How can I make it so that it reads in command by command. For example, rather than reading in just "START 2"
How can I read in "START" then "2" separately?
4 Answers
- deonejuanLv 71 decade agoFavorite Answer
I started using Scanner over BufferedReader to read files. First a note about the text file:
the command.txt has to reside at the same location of your MainProgram.class file.
From there you do read line per line and utilize methods of String (see the java API for String)
while( src.hasNext() ) {
input = src.nextLine();
if( input.toLowerCase().startsWith("start")
// here, I would branch to a method( input )
private int method( String in ) {
int lValue = in.indexOf( " " ) + 1;
int rValue = inindexOf( " ", lValue);
int value = Integer.parseInt( in.substring( lValue, rValue );
return value;
}
Source(s): http://www.java2s.com/Code/Java/Language-Basics/Us... http://download-llnw.oracle.com/javase/6/docs/api/... - 1 decade ago
import java.io.*;
class FileRead
{
public static void main(String args[])
{
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("textfile.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
- binaryFusionLv 51 decade ago
"I notice that the first answer reads in line by line. How can I make it so that it reads in command by command. For example, rather than reading in just "START 2"
How can I read in "START" then "2" separately?"
Read that here
http://download.oracle.com/javase/tutorial/java/da...
And