试着写一个public final class test { public static void main(String[] args) { try { FileReader fr = new FileReader("c:/test.txt"); Scanner scanner = new Scanner(fr); while (true){ if (scanner.hasNext()){ System.out.println(scanner.next()); }else if (scanner.hasNextInt()){ System.out.println(scanner.nextInt()); }else{ break; } } } catch (FileNotFoundException e) { e.printStackTrace(); } } }
/**
* <pre>
* [機 能] 文件读取。
* [説 明] 按照指定路径下的文件读取,并输出
* </pre>
* @param strFileName 文件名
* @param strDirectory 文件路经
* @throws Exception
*/
public static void read(final String strDirectory,
final String strFileName) throws Exception {
File infile = null;
BufferedReader reader = null;
String line = null;
try {
infile = new File(strDirectory, strFileName);
reader = new BufferedReader((new InputStreamReader(
new FileInputStream(infile))));
while ((line = reader.readLine()) != null) { // 读取一行
String[] datas = line.split(" "); // 这里假设你的数据是用空格分开的
for (String str : datas) {
System.out.println(str); // 输出每个数据,数字i 字母c 数字j 字符串S...
}
}
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (Exception e) {
throw e;
}
}
}
try {
FileReader fr = new FileReader("c:/test.txt");
Scanner scanner = new Scanner(fr);
while (true){
if (scanner.hasNext()){
System.out.println(scanner.next());
}else if (scanner.hasNextInt()){
System.out.println(scanner.nextInt());
}else{
break;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}