62,614
社区成员
发帖
与我相关
我的任务
分享
package test;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
public class Test {
public static void main(String arg[]) throws FileNotFoundException {
BufferedReader bir = new BufferedReader(new InputStreamReader(System.in));
FileOutputStream fo = new FileOutputStream("Userx.txt", true);
try {
while (true) {
System.out.print("用户名:");
String name = bir.readLine();
if (name.equalsIgnoreCase("done"))
break;
fo.write(name.getBytes());
System.out.print("密码:");
String password = bir.readLine();
fo.write(password.getBytes());
fo.flush();
}
} catch (Exception e) {
} finally {
try {
if (fo != null)
fo.close();
} catch (Exception e2) {
}
}
}
}
这是改好的,你按这个再改为你需的。
重启下你的机器,错误的程序把user1.txt文件锁定 了。
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();
}
}
}