0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

read file

Posted at

Mapに格納

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class CsvToMap {
    public Map<String, String> readCsv(String filePath) {
        Map<String, String> dataMap = new HashMap<>();

        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            // ヘッダー行をスキップ
            br.readLine();

            while ((line = br.readLine()) != null) {
                // カンマで行を分割
                String[] values = line.split(",");

                // CSVのカラム数が正しいか確認
                if (values.length == 2) {
                    String key = values[0];
                    String value = values[1];
                    dataMap.put(key, value);
                } else {
                    System.err.println("Invalid CSV format: " + line);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return dataMap;
    }

    // 読み込んだデータを表示する
    public static void main(String[] args) {
        CsvToMap reader = new CsvToMap();
        String filePath = "[ファイルパスを記載]";
        Map<String, String> dataMap = reader.readCsv(filePath);

        // 読み込んだデータを表示
        for (Map.Entry<String, String> entry : dataMap.entrySet()) {
            System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
        }
    }
}

普通の読み込み(格納)

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ReadCsv {
    public List<CsvRow> readCsv(String filePath) {
        List<CsvRow> rows = new ArrayList<>();

        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            // ヘッダー行をスキップ
            br.readLine();

            while ((line = br.readLine()) != null) {
                // カンマで行を分割
                String[] values = line.split(",");

                // CSVのカラム数が正しいか確認
                if (values.length == 3) {
                    CsvRow row = new CsvRow(values[0], values[1], values[2]);
                    rows.add(row);
                } else {
                    System.err.println("Invalid CSV format: " + line);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return rows;
    }

    public static void main(String[] args) {
    	ReadCsv reader = new ReadCsv();
        String filePath = "[ファイルパスを記載]";
        List<CsvRow> rows = reader.readCsv(filePath);

        // 読み込んだデータを表示
        for (CsvRow row : rows) {
            System.out.println(row);
        }
    }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?