import java.io.*;
public class Main {
public static void main(String[] args) throws IOException{
// c01.csvをShift-JIS形式で開く
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("c01.csv"), "Shift-JIS"));
// 先頭の行を読み込む
String line = br.readLine();
// 先頭の行をカンマで分割する
String[] index = line.split(",");
// 分割したものを順番に表示する
for (int i = 0; i < index.length; i++) {
System.out.println(i + ":" + index[i]);
}
}
}
実行結果
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException{
// c01.csvをShift-JIS形式で開く
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("c01.csv"), "Shift-JIS"));
// 先頭の行を読み込む
String line = br.readLine();
// 先頭の行をカンマで分割する
String[] index = line.split(",");
// 残りの行のデータを読み込んで表示する
while((line = br.readLine()) != null){
// lineに""が含まれている場合は、削除
line = line.replaceAll("\"", "");
// lineをカンマで分割する
String[] data0 = line.split(",");
// data0の各要素を表示する
for(int i = 0; i < data0.length; i++){
System.out.println(index[i] + ":" + data0[i] + " ");
}
System.out.println();
}
// ファイルを閉じる
br.close();
}
}
実行結果
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException{
// データを格納するリスト
List<Data> data = new ArrayList<Data>();
// c01.csvをShift-JIS形式で開く
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("c01.csv"), "Shift-JIS"));
// 先頭の行を読み込む
String line = br.readLine();
// 先頭の行をカンマで分割する
String[] index = line.split(",");
// 残りの行のデータを読み込んで表示する
while((line = br.readLine()) != null){
// lineに""が含まれている場合は、削除
line = line.replaceAll("\"", "");
// lineをカンマで分割する
String[] data0 = line.split(",");
// フォーマットがindexと同じものの場合は形式を変換してdataに追加する
if(data0.length != index.length){
continue;
}
// data0の0番目か6番目が整数のフォーマットでなかったら
if(!data0[0].matches("[0-9]+") || !data0[6].matches("[0-9]+")){
continue;
}
// data0の各要素をDataクラスのコンストラクタに入れられる形に変換
int prefectureCode = Integer.parseInt(data0[0]);
String prefectureName = data0[1];
int year = Integer.parseInt(data0[4]);
int population = Integer.parseInt(data0[6]);
// Dataクラスのインスタンスを作成
Data data1 = new Data(prefectureCode, prefectureName, year, population);
// dataに追加
data.add(data1);
}
// ファイルを閉じる
br.close();
// dataの各要素を表示
for(int i = 0; i < data.size(); i++){
data.get(i).show();
}
}
}
// データクラス
class Data {
int prefectureCode; // 都道府県コード
String prefectureName; // 都道府県名
int year; // 年
int population; // 人口
// コンストラクタ
public Data(int prefectureCode, String prefectureName, int year, int population){
this.prefectureCode = prefectureCode;
this.prefectureName = prefectureName;
this.year = year;
this.population = population;
}
// 情報を表示
public void show(){
System.out.println("都道府県コード:" + prefectureCode);
System.out.println("都道府県名:" + prefectureName);
System.out.println("年:" + year);
System.out.println("人口:" + population);
}
}
実行結果
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException{
// データを格納するリスト
List<Data> data = new ArrayList<Data>();
// c01.csvをShift-JIS形式で開く
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("c01.csv"), "Shift-JIS"));
// 先頭の行を読み込む
String line = br.readLine();
// 先頭の行をカンマで分割する
String[] index = line.split(",");
// 残りの行のデータを読み込んで表示する
while((line = br.readLine()) != null){
// lineに""が含まれている場合は、削除
line = line.replaceAll("\"", "");
// lineをカンマで分割する
String[] data0 = line.split(",");
// フォーマットがindexと同じものの場合は形式を変換してdataに追加する
if(data0.length != index.length){
continue;
}
// data0の0番目か6番目が整数のフォーマットでなかったら
if(!data0[0].matches("[0-9]+") || !data0[6].matches("[0-9]+")){
continue;
}
// data0の各要素をDataクラスのコンストラクタに入れられる形に変換
int prefectureCode = Integer.parseInt(data0[0]);
String prefectureName = data0[1];
int year = Integer.parseInt(data0[4]);
int population = Integer.parseInt(data0[6]);
// Dataクラスのインスタンスを作成
Data data1 = new Data(prefectureCode, prefectureName, year, population);
// dataに追加
data.add(data1);
}
// ファイルを閉じる
br.close();
// 1970年のデータを1970.csvにShift-JIS形式で書き込む
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("1970.csv"), "Shift-JIS"));
for(int i = 0; i < data.size(); i++){
// そのデータが1970年のものであれば
if(data.get(i).year == 1970){
// そのデータを書き込む
bw.write(data.get(i).prefectureCode + "," + data.get(i).prefectureName + "," + data.get(i).population);
bw.newLine();
}
}
// ファイルを閉じる
bw.close();
}
}
// データクラス
class Data {
int prefectureCode; // 都道府県コード
String prefectureName; // 都道府県名
int year; // 年
int population; // 人口
// コンストラクタ
public Data(int prefectureCode, String prefectureName, int year, int population){
this.prefectureCode = prefectureCode;
this.prefectureName = prefectureName;
this.year = year;
this.population = population;
}
// 情報を表示
public void show(){
System.out.println("都道府県コード:" + prefectureCode);
System.out.println("都道府県名:" + prefectureName);
System.out.println("年:" + year);
System.out.println("人口:" + population);
}
}
1970.csv
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException{
// データを格納するリスト
List<Data> data = new ArrayList<Data>();
// c01.csvをShift-JIS形式で開く
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("c01.csv"), "Shift-JIS"));
// 先頭の行を読み込む
String line = br.readLine();
// 先頭の行をカンマで分割する
String[] index = line.split(",");
// 残りの行のデータを読み込んで表示する
while((line = br.readLine()) != null){
// lineに""が含まれている場合は、削除
line = line.replaceAll("\"", "");
// lineをカンマで分割する
String[] data0 = line.split(",");
// フォーマットがindexと同じものの場合は形式を変換してdataに追加する
if(data0.length != index.length){
continue;
}
// data0の0番目か6番目が整数のフォーマットでなかったら
if(!data0[0].matches("[0-9]+") || !data0[6].matches("[0-9]+")){
continue;
}
// data0の各要素をDataクラスのコンストラクタに入れられる形に変換
int prefectureCode = Integer.parseInt(data0[0]);
String prefectureName = data0[1];
int year = Integer.parseInt(data0[4]);
int population = Integer.parseInt(data0[6]);
// Dataクラスのインスタンスを作成
Data data1 = new Data(prefectureCode, prefectureName, year, population);
// dataに追加
data.add(data1);
}
// ファイルを閉じる
br.close();
// 都道府県コードが1の都道府県名を取得
String prefectureName = "";
for(int i = 0; i < data.size(); i++){
if(data.get(i).prefectureCode == 1){
prefectureName = data.get(i).prefectureName;
break;
}
}
// 都道府県名がprerectureNameのデータをprerectureName.csvにShift-JIS形式で書き込む
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(prefectureName + ".csv"), "Shift-JIS"));
for(int i = 0; i < data.size(); i++){
// そのデータがprerectureNameのものであれば
if(data.get(i).prefectureName.equals(prefectureName)){
// そのデータを書き込む
bw.write(data.get(i).year + "," + data.get(i).population);
bw.newLine();
}
}
// ファイルを閉じる
bw.close();
}
}
// データクラス
class Data {
int prefectureCode; // 都道府県コード
String prefectureName; // 都道府県名
int year; // 年
int population; // 人口
// コンストラクタ
public Data(int prefectureCode, String prefectureName, int year, int population){
this.prefectureCode = prefectureCode;
this.prefectureName = prefectureName;
this.year = year;
this.population = population;
}
// 情報を表示
public void show(){
System.out.println("都道府県コード:" + prefectureCode);
System.out.println("都道府県名:" + prefectureName);
System.out.println("年:" + year);
System.out.println("人口:" + population);
}
}
北海道.csv