views:

11006

answers:

7

I am used to the c-style getchar(), but it seems like there is nothing comparable for java. I am building a lexical analyzer, and I need to read in the input character by character.

I know I can use the scanner to scan in a token or line and parse through the token char-by-char, but that seems unwieldy for strings spanning multiple lines. Is there a way to just get the next character from the input buffer in Java, or should I just plug away with the Scanner class?

Edit: forgot to say where the input is coming from. The input is a file, not the keyboard.

+9  A: 

Use Reader.read(). A return value of -1 means end of stream; else, cast to char.

This code reads character data from a list of file arguments:

public class CharacterHandler {

  public static void main(String[] args) {
    // replace this with a known charset if possible
    Charset charset = Charset.defaultCharset();

    try {
      for (String filename : args) {
        File file = new File(filename);
        parseFile(file, charset);
      }
    } catch (Exception e) {
      System.err.println("Error");
      e.printStackTrace();
    }
  }

  private static void parseFile(File file, Charset charset)
      throws IOException {
    InputStream in = new FileInputStream(file);
    try {
      Reader reader = new InputStreamReader(in, charset);
      // buffer input because you're reading one char at a time
      Reader buffer = new BufferedReader(reader);
      handleCharacters(buffer);
    } finally {
      in.close();
    }
  }

  private static void handleCharacters(Reader reader)
      throws IOException {
    int r;
    while ((r = reader.read()) != -1) {
      char ch = (char) r;
      System.out.println("Do something with " + ch);
    }
  }
}

The bad thing about the above code is that it uses the system's default character set. Wherever possible, prefer a known encoding (ideally, a Unicode encoding if you have a choice). See the Charset class for more. (If you feel masochistic, you can read this guide to character encoding.)

(One thing you might want to look out for are supplementary Unicode characters - those that require two char values to store. See the Character class for more details; this is an edge case that probably won't apply to homework.)

McDowell
Can I use reader with a file, or just the keyboard?
Jergason
Typically you open a FileInputStream and wrap that in an InputStreamReader, specifying the character encoding. (FileReader unfortunately doesn't let you specify the encoding.)
Jon Skeet
A: 

You have several options if you use BufferedReader. This buffered reader is faster than Reader so you can wrap it.

BufferedReader reader = new BufferedReader(new FileReader(path));
reader.read(char[] buffer);

this reads line into char array. You have similar options. Look at documentation.

shake
A: 

Wrap your reader in a BufferedReader, which maintains a buffer allowing for much faster reads overall. You can then use read() to read a single character (which you'll need to cast). You can also use readLine() to fetch an entire line and then break that into individual characters. The BufferedReader also supports marking and returning, so if you need to, you can read a line multiple times.

Generally speaking, you want to use a BufferedReader or BufferedInputStream on top of whatever stream you are actually using since the buffer they maintain will make multiple reads much faster.

James
+1  A: 

Wrap your input stream in a buffered reader then use the read method to read one byte at a time until the end of stream.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Reader {

public static void main(String[] args) throws IOException {

 BufferedReader buffer = new BufferedReader(
                 new InputStreamReader(System.in));
 int c = 0;
 while((c = buffer.read()) != -1) {
  char character = (char) c;

  System.out.println(character);

 }

}

}
pfranza
+1  A: 

Combining the recommendations from others for specifying a character encoding and buffering the input, here's what I think is a pretty complete answer.

Assuming you have a File object representing the file you want to read:

BufferedReader reader = new BufferedReader(
    new InputStreamReader(
        new FileInputStream(file),
        Charset.forName("UTF-8")));
int c;
while((c = reader.read()) != -1) {
  char character = (char) c;
  // Do something with your character
}
roryparle
A: 

Another option is to not read things in character by character -- read the entire file into memory. This is useful if you need to look at the characters more than once. One trivial way to do that is:

  /** Read the contents of a file into a string buffer      */
    public static void readFile(File file, StringBuffer buf)
        throws IOException
    {
    FileReader fr = null;
    try {
      fr = new FileReader(file);
      BufferedReader br = new BufferedReader(fr);
      char[] cbuf = new char[(int) file.length()];
      br.read(cbuf);  
      buf.append(cbuf);
      br.close();
    }
    finally {
      if (fr != null) {
        fr.close();
      }
    }
}
David
A: 

stuff. and things. Oh and stuff about things. I really don't have much of an answer for this. I want it to do a similar thing, but rather than have to press enter after inputting the character, I want it to send to a server. I looked at keyListeners and Changelisteners, but I don't quite get it.

thatGuy