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

发布于 2024-07-19 04:00:24 字数 216 浏览 8 评论 0原文

我习惯了c风格的getchar(),但对于java来说似乎没有什么可比的。 我正在构建一个词法分析器,我需要逐个字符地读入输入。

我知道我可以使用扫描仪扫描令牌或行并逐个字符地解析令牌,但这对于跨越多行的字符串似乎很笨拙。 有没有办法从 Java 的输入缓冲区中获取下一个字符,或者我应该直接使用 Scanner 类?

输入是文件,而不是键盘。

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?

The input is a file, not the keyboard.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

影子是时光的心 2024-07-26 04:00:24

使用 Reader.read()。 返回值-1表示流结束; 否则,转换为 char

此代码从文件参数列表中读取字符数据:

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

上述代码的缺点是它使用系统的默认字符集。 只要有可能,最好选择已知的编码(如果可以选择,最好选择 Unicode 编码)。 有关详细信息,请参阅 Charset 类。 (如果你觉得受虐狂,你可以阅读 本字符编码指南。)

(您可能需要注意的一件事是补充 Unicode 字符 - 需要两个 char 值来存储的字符。请参阅 Character 类了解更多详细信息;这是一个可能获胜的边缘情况不适用于家庭作业。)

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 {
    //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). 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.)

守护在此方 2024-07-26 04:00:24

结合其他人关于指定字符编码和缓冲输入的建议,我认为这是一个非常完整的答案。

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

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
}
满意归宿 2024-07-26 04:00:24

另一种选择是不逐个字符地读取内容——将整个文件读入内存。 如果您需要多次查看角色,这非常有用。 一种简单的方法是:

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

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();
      }
    }
}
一曲爱恨情仇 2024-07-26 04:00:24

将输入流包装在缓冲读取器中,然后使用 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);          
        }       
    }   
}

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);          
        }       
    }   
}
困倦 2024-07-26 04:00:24

如果我是你,我只会使用扫描仪并使用“.nextByte()”。 你可以将其转换为 char 就可以了。

If I were you I'd just use a scanner and use ".nextByte()". You can cast that to a char and you're good.

诗化ㄋ丶相逢 2024-07-26 04:00:24

如果您使用 BufferedReader,您有多种选择。 这个缓冲阅读器比 Reader 更快,因此您可以包装它。

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

这将行读入字符数组。 你有类似的选择。 查看文档。

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.

冰火雁神 2024-07-26 04:00:24

将您的阅读器包装在 BufferedReader 中,维护一个缓冲区,总体上可以更快地读取。 然后,您可以使用 read() 读取单个字符(您需要转换该字符)。 您还可以使用 readLine() 获取整行,然后将其分解为单个字符。 BufferedReader 还支持标记和返回,因此如果需要,您可以多次读取一行。

一般来说,你想使用 BufferedReader 或 BufferedInputStream
在您实际使用的任何流之上,因为它们维护的缓冲区将使多次读取速度更快。

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.

晒暮凉 2024-07-26 04:00:24

在java 5中添加的新功能是Scanner方法,它提供了在java中逐字符读取输入的机会。

例如;
使用 Scanner 方法 import java.util.Scanner;
在主方法之后:define

Scanner myScanner = new Scanner(System.in);
//用于读取字符

char everything=myScanner.findInLine(".").charAt(0);

你可以存储单个字符,如果你想要更多读取更多字符,则声明更多对象,例如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();

   }
  }

In java 5 new feature added that is Scanner method who gives the chance to read input character by character in java.

for instance;
for use Scanner method import java.util.Scanner;
after in main method:define

Scanner myScanner = new Scanner(System.in);
//for read character

char anything=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)

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

   }
  }
彼岸花ソ最美的依靠 2024-07-26 04:00:24

这将从文件中每行打印 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());
    }

This will print 1 character per line from the file.

    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());
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文