从文本文件中读取单个字符的麻烦

时间:2015-06-18 10:45:05

标签: java file-io

我正在写一个" app"从用户那里获取时间输入并将每天的小时和分钟分别存储在文本文件中(给出如下结果:

第1天:8小时45分钟

第2天:8小时43分钟

...)

背后的想法是将这些数据用于多种内容,例如计算平均时间,或者只是在任何一天访问时间,但我还没到达那个阶段,我遇到了麻烦做最简单的事情,比如读小时和打印它。

这里是代码

import java.util.Scanner;
import java.io.*;

public class TimeInput {

  public static void main(String[] args) {

    write();
    read();

  }

  static void write() {

    int dayOfMonth = 1;
    String fileName = "time.txt";

    int[] time = new int[2];
    String timeDisplay;

    Scanner s = new Scanner(System.in);

    try {

        FileWriter fileWriter = new FileWriter(fileName);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);


        while (dayOfMonth <=31) {

            System.out.println("Day " + dayOfMonth);
            System.out.print("Enter hour: " + "__" + "h\r");
            System.out.print("Enter hour: ");
            time[0] = s.nextInt();

            System.out.print("Enter minutes: " + "__" + "min\r");
            System.out.print("Enter minutes: ");
            time[1] = s.nextInt();


            timeDisplay = ("\n"+ "day " + dayOfMonth + ": " + time[0] + "h " + time[1] + "min");

            bufferedWriter.write(timeDisplay);
            bufferedWriter.newLine();

            dayOfMonth++;

            if (time[0] == 0 && time[1] == 0) {
                bufferedWriter.close();
                dayOfMonth = 32; // break
            }
        }
    }

    catch (IOException ex) {
        System.out.println(
        "Error writing to file '" + fileName + "'");
    }
}

static void read() {

    String fileName = "time.txt";
    String line = null;

    try {

        FileReader fileReader = new FileReader(fileName);

        BufferedReader bufferedReader = new BufferedReader(fileReader);

        while ((line = bufferedReader.readLine()) != null) {
            char h = line.charAt(7);
            System.out.println(h);
        }

        bufferedReader.close();
    }
    catch (FileNotFoundException ex) {
        System.out.println(
        "Unable to open file '" + fileName + "'" );
    }
    catch (IOException ex) {
        System.out.println(
        "Error reading file '" + fileName +"'");
    }
  }
}

我不断获得String out of bounds异常,我不明白为什么

3 个答案:

答案 0 :(得分:1)

您需要在char操作之前检查空字符串。

        while ((line = bufferedReader.readLine()) != null) {
            if("".equals(line)){
                continue;
            }
            char h = line.charAt(7);
            System.out.println(h);
        }

答案 1 :(得分:0)

Buffered Writer还在输入之间保存了输入键。因此,要消除输入,请在每行读取之间添加虚拟readLine语句。

            line=bufferedReader.readLine();
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);

                char h = line.charAt(7);
                System.out.println(h);
                line=bufferedReader.readLine();
            }

答案 2 :(得分:0)

看看最后一个循环。

设置一个断点,看看会发生什么。也许ArrayIndexOutBoundExceptions发生,因为你试图阅读更多的那一个字符,这是不可能的。看一下这个url,看看如何用bufferedReader读取txt。

http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/

希望得到这个帮助。