zoukankan      html  css  js  c++  java
  • Java_I/O输入输出_实现当用户输入姓名和密码时,将每一个姓名和密码加在文件中,如果用户输入done,就结束程序。

    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;
    }

    }

  • 相关阅读:
    MIPAV
    SPM12manual,统计部分(8-10)笔记
    Django中ORM介绍和字段及字段参数
    Django的路由系统
    django 连接mysql报错
    django启动创建用户失败
    django ORM操作
    Django创建App报错
    Web框架
    Bootstrap框架(组件)
  • 原文地址:https://www.cnblogs.com/bby2014210552/p/5942698.html
Copyright © 2011-2022 走看看