如何使用扫描仪通过char读取输入文件? [英] How to read an input file char by char using a Scanner?
本文介绍了如何使用扫描仪通过char读取输入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须使用 Scanner
,所以是否有 nextChar()
而不是 nextLine()
我可以使用的方法?
I have to use Scanner
, so is there a nextChar()
instead of nextLine()
method that I could use?
谢谢!
推荐答案
您可以转换为字符数组。
You can convert in an array of chars.
import java.io.*;
import java.util.Scanner;
public class ScanXan {
public static void main(String[] args) throws IOException {
Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader("yourFile.txt")));
while (s.hasNext())
{
String str = s.next();
char[] myChar = str.toCharArray();
// do something
}
} finally {
if (s != null) {
s.close();
}
}
}
这篇关于如何使用扫描仪通过char读取输入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文