Paste only the source with your own memo.
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.stream.Stream;
/**
*Studying file reading using Stream
* @author komikcomik
*
*/
public class FilesLinesTest {
public static void main(String[] args) {
FileSystem fs = FileSystems.getDefault();
Path inputPath = fs.getPath("resouces/test.txt");
/*Read file*/
try (Stream<String> stream = Files.lines(inputPath, StandardCharsets.UTF_8)) {
stream.forEach(System.out::println);
} catch (IOException e) {
//TODO auto-generated catch block
e.printStackTrace();
}
/*Writing a file*/
Path outputPath = fs.getPath("resouces/output.txt");
try (BufferedWriter bw = Files.newBufferedWriter(outputPath, StandardCharsets.UTF_8, StandardOpenOption.CREATE)) {
bw.write("Ahhhh\n");
bw.write("Good\n");
bw.write("Uuuuuuu\n");
} catch (IOException e) {
//TODO auto-generated catch block
e.printStackTrace();
}
}
}
Recommended Posts