博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java_I/O输入输出_实现当用户输入姓名和密码时,将每一个姓名和密码加在文件中,如果用户输入done,就结束程序。...
阅读量:4583 次
发布时间:2019-06-09

本文共 969 字,大约阅读时间需要 3 分钟。

import java.io.*;

public class Example {

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("word.txt");
while (true) {
System.err.println("请输入一个名字:");
if ("done".equalsIgnoreCase(new String(name, 0, 0, 4))) {
System.out.println("录入完毕");
break;
}
System.err.println("请输入电话号:");
readLine(phone);
for (int i = 0; phone[i] != 0; i++) {
fos.write(phone[i]);
}
fos.write(',');
for (int j = 0; name[j] != 0; j++) {
fos.write(name[j]);
}
fos.write('\n');
System.out.println("信息已经写入文件");
}
fos.close();
} catch (Exception 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[i] = (byte) 0;
}
}

 

转载于:https://www.cnblogs.com/bby2014210552/p/5942698.html

你可能感兴趣的文章
串并行数据结构实验--MAC下SML环境安装1
查看>>
java取整和java四舍五入方法
查看>>
学习linux-基础-操作系统结构
查看>>
卸载Linux内置的AMP软件
查看>>
关于js的几道经典题(作用域、原型链等)自己做的
查看>>
如何判断js是否加载完全
查看>>
【菜鸟学Python】函数的定义及调用
查看>>
宜信微服务任务执行器
查看>>
realsense blog 国外某人
查看>>
点击按钮将内容赋值到粘贴板
查看>>
DevExpress12.2.6 安装顺序记录
查看>>
.Net基础篇_学习笔记_第四天_switch-case02
查看>>
linux之基本命令讲解
查看>>
DAG上dp思想
查看>>
写文件
查看>>
iOS获取APP的版本号和名称
查看>>
如何用keytool导入证书
查看>>
第一周周六DailyReporting——PM(李忠)
查看>>
CF235C Cyclical Quest
查看>>
如何通过预加载器提升网页加载速度
查看>>