EDIT: I guess this thread can be closed, since all my questions have been answered! Thanks to everyone who helped me!
EDIT: I stumbled upon an error at openFileInput("myfilename.txt");
. This is the error: The method openFileInput(String) is undefined for the type Game
. I read an answer here: Android File I/O openFileInput() undefined, but must admit that I don't quite understand it...
I'm trying to read parts of a text file, till the token ";". This is the code I wrote:
InputStream instream = openFileInput("myfilename.txt");
String line=null;
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
while((line=buffreader.readLine())!=null){
String[] parts=line.split(";");
int intOne = Integer.parseInt(parts([0]);
int intTwo = Integer.parseInt(parts([1]);
String strLine = parts([3]);
}
public static void Start(){
while(there is text in the file){
// read first line till ';';
// name that variable intOne;
// read first line after ';' till next ';';
// name that variable intTwo;
// read next line, declare as strLine;
// System.out.println(strLine);
}
}
Beneath it is the idea of what it should do. But I have some questions:
Am I right to say that the
String[] parts
is an array?I want to have a bigger file, but read only 3 lines per loop. Or could I, when I have a file of 100 lines, read that all at once, then recall them from the
parts[]
? Or would that take way too much time?Where should the text file be? When I'm testing in Eclipse, in the project folder? And when I export it inside the jar?
Hope someone can answer my questions!
(My source is: Read specific string from each line of text file using BufferedReader in java, all credits to Gilbert Le Blanc!)
EDIT: When I do this in the file:
Hello,
I am coding;
Will the pars[0]
be Hello,
, because that's one line, or Hello, I am coding
? And will it take the enter with it?
Another EDIT: I wish to create some sort of textbased RPG engine, where you only have to edit the text file, to change the story. For example (in the text file):
30;60; //Score needed for this piece of the story
Hello!; // The text
Hi!;5; // The first possible answer; the score you'll get when you answer this
Shut up!;-5; // The second possible answer; the score you'll get when you answer this