Avoid extra new line, writing on .txt file
java, nio
Solution
From javadoc for write: "Each line is a char sequence and is written to the file in sequence with each line terminated by the platform's line separator, as defined by the system property line.separator."
Simplest way to do as you wish:
List<String> lineList =Arrays.asList("1. Hello", "2. I am Fine");
String lastLine = "3. What about U ?";
Files.write(filePath, lineList, Charset.forName("UTF-8"));
Files.write(filePath, lastLine.getBytes("UTF-8"), StandardOpenOption.APPEND);
Problem
Currently I am using `java.nio.file.File.write(Path, Iterable, Charset)` to write txt file. Code is here... ``` Path filePath = Paths.get("d:\\myFile.txt"); List<String> lineList =Arrays.asList("1. Hello", "2. I am Fine", "3. What about U ?"); Files.write(filePath, lineList, Charset.forName("UTF-8")); ``` But one more (4th) empty line generated in the text file. How can I avoid 4th empty line ? ``` 1 | 1. Hello 2 | 2. I am Fine 3 | 3. What about U ? 4 | ```