简体   繁体   English

如何在Java中按字符读取输入的字符?

[英]How do I read input character-by-character in Java?

I am used to the c-style getchar() , but it seems like there is nothing comparable for java. 我已经习惯了c风格的getchar() ,但是似乎没有可比的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? 有没有一种方法可以从Java中的输入缓冲区中获取下一个字符,还是应该随身携带Scanner类?

The input is a file, not the keyboard. 输入是文件,而不是键盘。

Use Reader.read() . 使用Reader.read() A return value of -1 means end of stream; 返回值-1表示流的结尾; else, cast to char . 否则,强制转换为char

This code reads character data from a list of file arguments: 此代码从文件参数列表中读取字符数据:

public class CharacterHandler {
    //Java 7 source level
    public static void main(String[] args) throws IOException {
        // replace this with a known encoding if possible
        Charset encoding = Charset.defaultCharset();
        for (String filename : args) {
            File file = new File(filename);
            handleFile(file, encoding);
        }
    }

    private static void handleFile(File file, Charset encoding)
            throws IOException {
        try (InputStream in = new FileInputStream(file);
             Reader reader = new InputStreamReader(in, encoding);
             // buffer for efficiency
             Reader buffer = new BufferedReader(reader)) {
            handleCharacters(buffer);
        }
    }

    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). 尽可能使用已知的编码(如果可以选择,最好使用Unicode编码)。 See the Charset class for more. 有关更多信息,请参见Charset类。 (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.) (您可能要注意的一件事是补充Unicode字符-需要存储两个char值的Unicode字符。有关更多详细信息,请参见Character类;这是一种边缘情况,可能不适用于家庭作业。)

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: 假设您有一个File对象代表您要读取的文件:

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
}

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. 将输入流包装在缓冲的读取器中,然后使用read方法一次读取一个字节,直到流结束。

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);          
        }       
    }   
}

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();
      }
    }
}

If I were you I'd just use a scanner and use ".nextByte()". 如果您是我,则只需使用扫描仪并使用“ .nextByte()”即可。 You can cast that to a char and you're good. 您可以将其转换为字符,并且您很好。

You have several options if you use BufferedReader . 如果使用BufferedReader则有几种选择。 This buffered reader is faster than Reader so you can wrap it. 此缓冲的阅读器比Reader快,因此可以包装它。

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

this reads line into char array. 这会将行读入char数组。 You have similar options. 您有类似的选择。 Look at documentation. 查看文档。

Wrap your reader in a BufferedReader , which maintains a buffer allowing for much faster reads overall. 将阅读器包装在BufferedReader中 ,该BufferedReader维护一个缓冲区,使整体读取更快。 You can then use read() to read a single character (which you'll need to cast). 然后,您可以使用read()来读取单个字符(需要转换)。 You can also use readLine() to fetch an entire line and then break that into individual characters. 您还可以使用readLine()提取整行,然后将其分成单个字符。 The BufferedReader also supports marking and returning, so if you need to, you can read a line multiple times. BufferedReader还支持标记和返回,因此,如果需要,您可以读取一行。

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. 一般来说,您要在实际使用的任何流之上使用BufferedReader或BufferedInputStream,因为它们维护的缓冲区将使多次读取快得多。

In java 5 new feature added that is Scanner method who gives the chance to read input character by character in java. 在Java 5中增加了一个新功能,即Scanner方法,它使您有机会逐个读取Java中的字符。

for instance; 例如; for use Scanner method import java.util.Scanner; 使用Scanner方法import java.util.Scanner; after in main method:define 在主要方法之后:定义

Scanner myScanner = new Scanner(System.in); 扫描仪myScanner =新的Scanner(System.in); //for read character //用于读取字符

char anything=myScanner.findInLine(".").charAt(0); char everything = myScanner.findInLine(“。”)。charAt(0);

you anything store single character, if you want more read more character declare more object like anything1,anything2... more example for your answer please check in your hand(copy/paste) 您将任何内容存储为单个字符,如果您想更多地阅读更多字符,请声明更多对象,如anything1,anything2 ...更多示例供您回答,请检入(复制/粘贴)

     import java.util.Scanner;
     class ReverseWord  {

    public static void main(String args[]){
    Scanner myScanner=new Scanner(System.in);
    char c1,c2,c3,c4;

    c1 = myScanner.findInLine(".").charAt(0);
        c2 = myScanner.findInLine(".").charAt(0);
    c3 = myScanner.findInLine(".").charAt(0);
    c4 = myScanner.findInLine(".").charAt(0);

    System.out.print(c4);
    System.out.print(c3);
    System.out.print(c2);
    System.out.print(c1);
    System.out.println();

   }
  }

This will print 1 character per line from the file. 这将从文件中每行打印1个字符。

    try {

        FileInputStream inputStream = new FileInputStream(theFile);
        while (inputStream.available() > 0) {
            inputData = inputStream.read();
            System.out.println((char) inputData);

        }
        inputStream.close();
    } catch (IOException ioe) {
        System.out.println("Trouble reading from the file: " + ioe.getMessage());
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM