java流问题

[复制链接]
查看11 | 回复1 | 2009-7-23 08:29:48 | 显示全部楼层 |阅读模式
代码如下:
import java.io.*;
public class Iotest {
static final int lineLength = 81;
public static void main(String[] args) {
FileOutputStream fos ;

byte[] phone = new byte[lineLength];
byte[] name = new byte[lineLength];
try {
fos= new FileOutputStream("D:/word.txt");
while(true){
System.err.println("请输入一个名字:");
readLine(name);
if("done".equalsIgnoreCase(new String(name,0,0,4))){

break;
}
System.err.println("请输入电话号:");
readLine(phone);
for(int i = 0;phone!=0;i++){

fos.write(phone);
}
fos.write(',');
for(int j = 0;name[j]!=0;j++){

fos.write(name[j]);
}
fos.write('\n');
fos.close();
}
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
private static void readLine(byte[] name) throws IOException {
int b = 0, i= 0;
while((i <(lineLength - 1))&& (b= System.in.read())!='\n'){
name[i++] = (byte)b;
}
name = (byte)0;
}
}
为什么当我输入完第二次电话号码时会报错?帮我修改下代码,实现当用户输入姓名和电话号码时,将每一个姓名和密码加在文件里。

回复

使用道具 举报

千问 | 2009-7-23 08:29:48 | 显示全部楼层
看了你写的程序,感觉你先学的c或c++.用java写程序有很多现成的很好用的类.比如我给写个你从键盘输入那部分.我现在用java.util.Scanner类,读取键盘输入的文字.Scanner scan = new Scanner(System.in) ;
String name = null , phone = null ;
while( true )
{
System.out.println("input your name:") ;
name = scan.next() ;
System.out.println("input your phone:") ;
ph...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行