import java.io.*;
import java.nio.*;
import java.util.*;
public class read1 { /**
* @param args
* @throws IOException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws IOException{
int No[]=new int[100];
BufferedReader readGuNo = new BufferedReader(new FileReader("guestNo.txt"));
for(int i=0;i<No.length;i++){
No[i]=reader.read();
if(No[i]==0){
break;
}
}
System.out.println("会员编号");
for(int i=0;i<No.length;i++){
if(No[i]!=0){
System.out.println(No[i]);
}else{
break;
}
}
readGuNo.close();
}}
///////guestNo.txt/////////
1234
5648
1453
import java.nio.*;
import java.util.*;
public class read1 { /**
* @param args
* @throws IOException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws IOException{
int No[]=new int[100];
BufferedReader readGuNo = new BufferedReader(new FileReader("guestNo.txt"));
for(int i=0;i<No.length;i++){
No[i]=reader.read();
if(No[i]==0){
break;
}
}
System.out.println("会员编号");
for(int i=0;i<No.length;i++){
if(No[i]!=0){
System.out.println(No[i]);
}else{
break;
}
}
readGuNo.close();
}}
///////guestNo.txt/////////
1234
5648
1453
int no[]=new int[100]; try {
Scanner sc = new Scanner(new File("D:\\guestNo.txt"));
int k = 0;
while(sc.hasNextInt()){
no[k++] = sc.nextInt();
}
for(int i = 0; i<k; i++){
System.out.println(no[i]);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这里删错了.
No[i]=readGuNo.read();
把读出来的数字,放到,整型 数组里面去
int No[] = new int[100];BufferedReader readGuNo = new BufferedReader(new FileReader(
"guestNo.txt"));for (int i = 0; i < No.length; i++) {
No[i] = Character.getNumericValue(readGuNo.read());//这里取得是字符编号,而非数字本身,需要转换
if (No[i] == 0) {
break;
}
}
System.out.println("会员编号");
for (int i = 0; i < No.length; i++) {
if (No[i] != -1) { //这里改成-1
System.out.println(No[i]);
} else {
break;
}
}
readGuNo.close();
for(int i=0;i<strNo.length;i++){
String strIn = readGuNo.readLine(); //循环读取
if(strIn==null) break; //读取为空时跳出
else strNo[i] = Integer.parseInt(strIn.trim()); //转换成整型
if(strNo[i]==0) break;
}
当然我们需要与时俱进
在1.4时代,这样的事情是很麻烦的...首先是修饰类的层层套装,然后是整行整行的读取,然后是Integer的parse
好了,现在1.5以上的版本都提供了Scanner了,我们都在享受这一变化带来的轻松