Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read by both char and line from text file

Tags:

java

file-io

For a VERY basic RPG (Well, top-down walking around game at present,) I want to make a method that reads a "save" file and stores it in memory (Saving comes later.) I'd like this save file to be user-friendly by having a way to make certain lines of it not be read by the method, so that I can put instructions on modifying it and such. Currently, the plan is to have it not read lines with a % in front, but that can change if % causes a problem.

The save format itself I'd like to have in one compressed block - one or two alphanumeric characters denote the contents of a single "tile." I don't want to simply have 169 lines for every screen of area, I'd rather have them in a compressed block of 13*13 (again, to be user-friendly.)

Essentially, how can I both a) Detect if a line starts with % b) If so, skip to next line c) If not, read two characters at a time copying to string array d) Skip blank lines

I understand BASIC IO, but I haven't been able to find a way of reading both by character and by line.

like image 822
JTTCOTE Avatar asked Feb 14 '13 13:02

JTTCOTE


2 Answers

Personally, I would use a BufferedReader to read the file line by line into individual String objects, and inspect the contents. You can check if a line starts with a comment character by using the cunningly named String.startsWith("%") method, and get at individual characters in the line using String.charAt(index), or double characters using String.substring(start, start+2).

like image 162
SimonC Avatar answered Sep 16 '22 11:09

SimonC


Have a look at the scanner class http://docs.oracle.com/javase/tutorial/essential/io/scanning.html it should be able to do most of the work.

like image 32
Christophe Roussy Avatar answered Sep 16 '22 11:09

Christophe Roussy