It may not even be the first step, but I will do it little by little. 2019/04/19 Updated a little.
input_01.csv
"ID","class","name"
"0001","Saber","Altria"
"0002","caster","media"
"0003","Berserker","Hercules"
input_02.csv
"ID","Ship type","name"
"0001","Battleship","Nagato"
"0002","a light cruiser","Abukuma"
"0003","Destroyer","heat haze"
FileInOutFirst.java
package com.otamesi;
import java.io.BufferedReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class FileInOutFirst{
/**
*The first Files class.
*
*/
public static void main(String args[]) throws Exception{
FileInOutFirst fileInOutFirst = new FileInOutFirst();
fileInOutFirst.inOut01();
fileInOutFirst.inOut02();
fileInOutFirst.inOut03();
fileInOutFirst.inOut04();
fileInOutFirst.inOut05();
}
/**
*Part 1.<br>
*For the time being, try to output to standard output.
*
*/
private void inOut01() throws Exception {
//Read SJIS file
List<String> input = Files.readAllLines(Paths.get("D:/", "input_01.csv"), Charset.forName("SJIS"));
System.out.println("Part 1");
input.forEach(line -> System.out.println(line));
}
/**
*Part 2.<br>
*For the time being, try to output to standard output(UTF-8)。
*
*/
private void inOut02() throws Exception {
//UTF-Read 8 files
List<String> input = Files.readAllLines(Paths.get("D:/", "input_02.csv"), StandardCharsets.UTF_8);
System.out.println("Part 2");
input.forEach(line -> System.out.println(line));
}
/**
*Part 3.<br>
*For the time being, try to output to standard output(UTF-8)。
*
*/
private void inOut03() throws Exception {
//UTF-Read 8 files
List<String> input = Files.readAllLines(Paths.get("D:/", "input_03.csv"), StandardCharsets.UTF_8);
System.out.println("Part 3");
input.forEach(System.out::println);
}
/**
*Part 4.<br>
*For the time being, try to output to standard output(UTF-8)。
*
*/
private void inOut04() throws Exception {
//UTF-Read 8 files
List<String> input = Files.readAllLines(Paths.get("D:/", "input_03.csv"), StandardCharsets.UTF_8);
List<String> input2 = new ArrayList<String>();
System.out.println("Part 4");
input.forEach(input2::add);
input2.forEach(System.out::println);
}
/**
*Part 5.<br>
*Use newBufferedReader(For large files)
*
*/
private void inOut05() throws Exception {
System.out.println("Part 5");
Path file = Paths.get("D:/", "input_03.csv");
try (BufferedReader br = Files.newBufferedReader(file, StandardCharsets.UTF_8)) {
String text;
while ((text = br.readLine()) != null) {
System.out.println(text);
}
}
}
}
Part 1 "ID", "class", "name" "0001", "Saber", "Altria" "0002", "Caster", "Media" "0003", "Berserker", "Heracles" Part 2 "ID", "ship type", "name" "0001", "Battleship", "Nagato" "0002", "Light Cruiser", "Abukuma" "0003", "Destroyer", "Kagero"
This is the first forEach method, but you can write a loop very neatly! Also, constants are prepared for encoding, which is convenient (unfortunately there seems to be no SJIS).
If you specify a character code different from the character code of the file as a parameter, The following exception occurs. You may have a chance to encounter it, so let's remember ...
Exception in thread "main" java.nio.charset.MalformedInputException: Input length = 1
at java.nio.charset.CoderResult.throwException(CoderResult.java:281)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:339)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at java.nio.file.Files.readAllLines(Files.java:3205)
Recommended Posts