簡體   English   中英

麻煩從文本文件中讀取單個字符

[英]Troubles reading a single character from a text file

我正在編寫一個“應用程序”,它接受用戶輸入的時間,並將每天的小時和分鍾分別存儲在文本文件中(給出的結果如下:

第一天: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 +"'");
    }
  }
}

我不斷收到字符串超出范圍的異常,但我不明白為什么

在進行char操作之前,您需要檢查空字符串。

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

Buffered Writer還保存了輸入之間的回車鍵。 因此,為了消除回車readLine在每行讀取之間添加虛擬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();
            }

看一下最后一個循環。

放置一個斷點,看看會發生什么。 也許發生ArrayIndexOutBoundExceptions是因為您試圖讀取更多的一個字符,而這是不可能的。 看一下此URL,以了解如何使用bufferedReader讀取txt。

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

希望能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM