Last active
June 21, 2016 07:44
-
-
Save tomoTaka01/f26de51f948b4355a6e5feedc6022b7c to your computer and use it in GitHub Desktop.
Creating two files and check if they are equal. The two files are different charset and line separator.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.test; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.Writer; | |
import java.nio.charset.Charset; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.nio.file.StandardOpenOption; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
import java.util.stream.Stream; | |
public class TestFile { | |
public static void main(String... args){ | |
new TestFile().createFiles(); | |
new TestFile().readFiles(); | |
new TestFile().readFiles2(); | |
} | |
private void readFiles2() { | |
List<List<String>> lines = Stream.of("sjis", "utf8").map(charset -> { | |
Path path = Paths.get("/Users/tomo/file" + charset + ".txt"); | |
try { | |
return Files.readAllLines(path, Charset.forName(charset)); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return null; | |
}).collect(Collectors.toList()); | |
if (lines.get(0).equals(lines.get(1))) { | |
System.out.println("OK"); | |
} else { | |
System.out.println("NG"); | |
} | |
} | |
private void readFiles() { | |
List<String> sjisLines = readFile("sjis"); | |
List<String> utfLines = readFile("utf8"); | |
System.out.println(sjisLines); | |
System.out.println(utfLines); | |
if (sjisLines.equals(utfLines)) { | |
System.out.println("OK"); | |
} else { | |
System.out.println("NG"); | |
} | |
} | |
private List<String> readFile(String charset) { | |
Path path = Paths.get("/Users/tomo/file" + charset + ".txt"); | |
List<String> lines = new ArrayList<>(); | |
try (BufferedReader reader = Files.newBufferedReader(path, Charset.forName(charset))) { | |
String line = null; | |
while ((line = reader.readLine()) != null) { | |
lines.add(line); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return lines; | |
} | |
private void createFiles() { | |
String crlf = "\r\n"; | |
String lf = System.getProperty("line.separator"); | |
List<String> contents = Arrays.asList("1行め", "2行め"); | |
createFile(contents, crlf, "sjis"); | |
createFile(contents, lf, "utf8"); | |
} | |
private void createFile(List<String> contents, String linesep, String charset) { | |
Path path = Paths.get("/Users/tomo/file" + charset + ".txt"); | |
try (Writer writer = Files.newBufferedWriter(path, Charset.forName(charset), StandardOpenOption.CREATE, | |
StandardOpenOption.TRUNCATE_EXISTING)) { | |
String lines = contents.stream().collect(Collectors.joining(linesep)); | |
writer.write(lines); | |
} catch (IOException e){ | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment