プログラミング初心者の奮闘記

プログラミング初心者の成長と過程。

ファイル操作2

ポイント:

  • ファイルの内容を読み込めるか

問題

java-musashiurawa.hatenablog.com

解答

package rensyuu;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class failsousa2 {

    public static void main(String[] args) {
        // TODO 自動生成されたメソッド・スタブ
        Scanner scan = new Scanner(System.in);
        String fileName = "schedule.csv";

        //ディレクトリ名を取得
        System.out.print("名前を入力してください:");
        String userName = scan.nextLine();
        System.out.println();

        System.out.println("ようこそ" + userName + "さん。");
        System.out.println("登録してあるスケジュールを表示します。");
        System.out.println();

        //ファイルを読み込む
        String path = "c:\\JavaTest\\" + userName;

        try {
            File file = new File(path + "\\" + fileName);
            if(checkBeforReadfile(file)) {
                BufferedReader br = new BufferedReader(new FileReader(file));

                String str;
                while((str = br.readLine()) != null) {
                    System.out.println(str);
                }
                br.close();
            }else {
                System.out.println(userName + "さんのファイルが存在しません。");
                System.out.println("スケジュールを登録してから実行してください。");
            }
            System.out.println();
            System.out.println("スケジュールの表示を終了します。");
        }catch(FileNotFoundException e) {
            System.out.println(e);
        }catch(IOException e) {
            System.out.println(e);
        }

    }


    private static boolean checkBeforReadfile(File file) {
        if(file.exists()) {
            if(file.isFile() && file.canRead()) {
                return true;
            }
        }
        return false;
    }
}

解答、正解

package rensyuu;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class failsousa2 {

    public static void main(String[] args) {
        // TODO 自動生成されたメソッド・スタブ
        Scanner scan = new Scanner(System.in);
        String fileName = "schedule.csv";

        //ディレクトリ名を取得
        System.out.print("名前を入力してください:");
        String userName = scan.nextLine();
        System.out.println();

        System.out.println("ようこそ" + userName + "さん。");
        System.out.println("登録してあるスケジュールを表示します。");


        //ファイルを読み込む
        String path = "c:\\JavaTest\\" + userName;

        try {
            File file = new File(path + "\\" + fileName);
            if(checkBeforReadfile(file)) {
                BufferedReader br = new BufferedReader(new FileReader(file));


                String str;
                str = br.readLine();
                String koumoku[] = str.split(",",-1);

                while((str = br.readLine()) != null) {
                    System.out.println();
                    String schedule[] = str.split(",",-1);
                    for(int i = 0; i < koumoku.length; i++) {
                        System.out.print(koumoku[i]);
                        System.out.println(schedule[i]);
                    }

                }
                br.close();

            }else {
                System.out.println(userName + "さんのファイルが存在しません。");
                System.out.println("スケジュールを登録してから実行してください。");
            }
//         System.out.println();
//         System.out.println("スケジュールの表示を終了します。");
        }catch(FileNotFoundException e) {
            System.out.println(e);
        }catch(IOException e) {
            System.out.println(e);
        }catch(NullPointerException e) {
            System.out.println(e);
        }
        finally {
            System.out.println();
            System.out.println("スケジュールの表示を終了します。");
        }
    }


    private static boolean checkBeforReadfile(File file) {
        if(file.exists()) {
            if(file.isFile() && file.canRead()) {
                return true;
            }
        }
        return false;
    }
}