如何在 Java 中逐字符读取输入?
我习惯了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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
使用 Reader.read()。 返回值-1表示流结束; 否则,转换为 char。
此代码从文件参数列表中读取字符数据:
上述代码的缺点是它使用系统的默认字符集。 只要有可能,最好选择已知的编码(如果可以选择,最好选择 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:
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.)
结合其他人关于指定字符编码和缓冲输入的建议,我认为这是一个非常完整的答案。
假设您有一个代表要读取的文件的
File
对象: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:另一种选择是不逐个字符地读取内容——将整个文件读入内存。 如果您需要多次查看角色,这非常有用。 一种简单的方法是:
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 方法一次读取一个字节,直到流结束。
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.
如果我是你,我只会使用扫描仪并使用“.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.
如果您使用 BufferedReader,您有多种选择。 这个缓冲阅读器比 Reader 更快,因此您可以包装它。
这将行读入字符数组。 你有类似的选择。 查看文档。
You have several options if you use
BufferedReader
. This buffered reader is faster than Reader so you can wrap it.this reads line into char array. You have similar options. Look at documentation.
将您的阅读器包装在 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.
在java 5中添加的新功能是Scanner方法,它提供了在java中逐字符读取输入的机会。
例如;
使用 Scanner 方法 import java.util.Scanner;
在主方法之后:define
Scanner myScanner = new Scanner(System.in);
//用于读取字符
char everything=myScanner.findInLine(".").charAt(0);
你可以存储单个字符,如果你想要更多读取更多字符,则声明更多对象,例如anything1,anything2 ...
更多答案示例请检查您的手(复制/粘贴)
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)
这将从文件中每行打印 1 个字符。
This will print 1 character per line from the file.