Created
December 14, 2018 14:05
-
-
Save pluto-atom-4/8140672e288734e08e93821ea185ff3b to your computer and use it in GitHub Desktop.
Exercising Java NIO2
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 org.junit.Test; | |
import java.io.IOException; | |
import java.nio.charset.Charset; | |
import java.nio.charset.StandardCharsets; | |
import java.nio.file.Files; | |
import java.nio.file.OpenOption; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.nio.file.attribute.FileAttribute; | |
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING; | |
import static java.nio.file.StandardOpenOption.WRITE; | |
public class NioTest { | |
@Test | |
public void verifyGetPath() { | |
String filename = "text.txt"; | |
Path path = Paths.get(filename); | |
FileAttribute[] attributes = new FileAttribute[] {}; | |
OpenOption[] options = new OpenOption[] { WRITE, TRUNCATE_EXISTING }; | |
try { | |
Path tempFilePath = Files.createTempFile("tag-", ".csv", attributes); | |
Charset charset = StandardCharsets.UTF_8; | |
String content = new String(Files.readAllBytes(tempFilePath), charset); | |
content = content.replaceAll("$featureId$", "bar"); | |
Files.write(tempFilePath, content.getBytes(charset),options); | |
Files.delete(tempFilePath); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
// http://www.java2s.com/Tutorials/Java/Java_io/1000__Java_nio_File_Attributes.htm | |
// http://www.java2s.com/Tutorials/Java/Java_io/1000__Java_nio_File_Attributes.htm | |
// http://tutorials.jenkov.com/java-nio/path.html | |
// https://stackoverflow.com/questions/20663841/quickest-way-to-use-common-openoption-combinations | |
// https://waman.hatenablog.com/entry/20120515/1337044411 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment