package com.shijian15_9_3;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Test {
public static void main(String arg[]){
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader bir = new BufferedReader(ir);
while(true){
try {
FileOutputStream fo = new FileOutputStream("User.txt");
BufferedOutputStream bo = new BufferedOutputStream(fo);
String name;
byte bname[] = new byte[30];
byte bpassword[] = new byte[30];
System.out.print("用户名:");
name = bir.readLine();
bname = name.getBytes();
if(name.equalsIgnoreCase("done"))
break;
fo.write(bname);
fo.flush();
String password;
System.out.print("密码:");
password = bir.readLine();
bpassword = password.getBytes();
fo.write(bpassword);
fo.flush();
fo.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
}
为什么不能写到文件里面????
------解决思路----------------------
FileOutputStream fo = new FileOutputStream("User.txt");
这句代码放到while(true)之前执行
------解决思路----------------------
if(name.equalsIgnoreCase("done"))
break;
break直接跳出了,后面都没执行。
package test;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
public class Test {
public static void main(String arg[]) {
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader bir = new BufferedReader(ir);
try (FileOutputStream fo = new FileOutputStream("User.txt")) {
System.out.print("用户名:");
fo.write(bir.readLine().getBytes());
System.out.print("密码:");
fo.write(bir.readLine().getBytes());
fo.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}