Created
October 17, 2017 05:24
-
-
Save inherithandle/cddddc2f45e03393504f90ad79f66c55 to your computer and use it in GitHub Desktop.
how to read file in java.
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
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.nio.charset.StandardCharsets; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.Arrays; | |
import java.util.List; | |
import org.junit.Test; | |
/* | |
* This Java source file was auto generated by running 'gradle init --type java-library' | |
* by 'nhn_enter' at '17. 10. 17 오후 2:07' with Gradle 2.14.1 | |
* | |
* @author nhn_enter, @date 17. 10. 17 오후 2:07 | |
*/ | |
public class LibraryTest { | |
private final String fileName = "data.txt"; | |
@Test | |
public void readFileLineByLine() throws IOException { | |
File file = new File(fileName); | |
BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); | |
String line; | |
while ((line = bufferedReader.readLine()) != null) { | |
System.out.println(line); | |
} | |
} | |
@Test | |
public void readFileWordByWord() throws IOException { | |
byte[] bytesFromFile = Files.readAllBytes(Paths.get(fileName)); | |
String stringFromFile = new String(bytesFromFile, StandardCharsets.UTF_8); | |
List<String> words = Arrays.asList(stringFromFile.split(" ")); | |
for (String word : words) { | |
System.out.println(word); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment