简体   繁体   English

如何从Java的扫描仪中提取单个字符?

[英]How to extract a single character from a scanner in Java?

What all I want to do is extract a single character every time from a text file, no matter it is a whitespace and/or new line character, till the eof. 我要做的就是每次从文本文件中提取单个字符,无论是空格和/或换行符,直到eof。 Would using ""(empty string) as a delimiter work? 使用“”(空字符串)作为分隔符会起作用吗?

scanner.next().charAt[0]

The above code will take the first character of the string returned by 上面的代码将采用由返回的字符串的第一个字符

scanner.next()

and ignore the rest of the part. 并忽略其余部分。 But I want just a single character every time from a file without ignoring any. 但是我每次都只希望文件中有一个字符而不会忽略任何字符。

if you want to read a file character by character, use a Reader, not a Scanner. 如果要逐个字符读取文件,请使用阅读器而不是扫描器。 A Scanner is for "scanning" for specific patterns. 扫描仪用于“扫描”特定图案。

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

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