jdk7 file write
Language/JAVA 2013. 11. 14. 13:21How to write file using Files.newBufferedWriter?
package org.kodejava.example.nio;
import java.io.BufferedWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class FilesNewBufferedWriter {
public static void main(String[] args) {
Path logFile = Paths.get("D:\\Temp\\logs\\app.log");
try (BufferedWriter writer = Files.newBufferedWriter(logFile,
StandardCharsets.UTF_8, StandardOpenOption.WRITE)) {
for (int i = 0; i < 10; i++) {
writer.write(String.format("Message %s%n", i));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
출처 - http://kodejava.org/how-to-write-file-using-files-newbufferedwriter/
demoFilesOperations.groovy
- #!/usr/bin/env groovy
- /**
- * demoFilesOperations.groovy
- *
- * Demonstrate some of the operations on files and directories provided by Java
- * SE 7 and its NIO.2 implementation. Specific focus is applied to the methods
- * of the java.nio.file.Files class and the java.nio.file.Path interface.
- */
- import java.nio.file.Files
- import java.nio.file.Paths
- // 1. Acquire 'working directory' name to use as current directory.
- def currentDirectoryName = System.getProperty("user.dir")
- // 2. Convert 'working directory' name plus a subdirectory named 'playarea' into
- // an instance of Path
- def playAreaPath = Paths.get(currentDirectoryName, "playarea")
- def playAreaStr = playAreaPath.toString()
- // 3. Create new subdirectory with name 'playarea'
- def playAreaDirPath = Files.createDirectory(playAreaPath)
- // 4. Create a temporary directory with prefix "dustin_"
- def tempDirPath = Files.createTempDirectory("dustin_")
- // 5. Create temporary files, one in the temporary directory just created and
- // one in the "root" temporary directory. Create them with slightly different
- // prefixes, but the same '.tmp' suffix.
- def tempFileInTempDirPath = Files.createTempFile(tempDirPath, "Dustin1-", ".tmp")
- def tempFilePath = Files.createTempFile("Dustin2-", ".tmp")
- // 6. Create a regular file.
- def regularFilePath = Files.createFile(Paths.get(playAreaStr, "Dustin.txt"))
- // 7. Write text to newly created File.
- import java.nio.charset.Charset
- import java.nio.file.StandardOpenOption
- Files.write(regularFilePath,
- ["To Be or Not to Be", "That is the Question"],
- Charset.defaultCharset(),
- StandardOpenOption.APPEND, StandardOpenOption.WRITE)
- // 8. Make a copy of the file using the overloaded version of Files.copy
- // that expects two Paths.
- def copiedFilePath =
- Files.copy(regularFilePath, Paths.get(playAreaStr, "DustinCopied.txt"))
- // 9. Move (rename) the copied file.
- import java.nio.file.StandardCopyOption
- def renamedFilePath = Files.move(copiedFilePath,
- Paths.get(playAreaStr, "DustinMoved.txt"),
- StandardCopyOption.REPLACE_EXISTING)
- // 10. Create symbolic link in 'current directory' to file in 'playarea'
- def symbolicLinkPath = Files.createSymbolicLink(Paths.get("SomeoneMoved.txt"), renamedFilePath)
- // 11. Create (hard) link in 'current directory' to file in 'playarea'
- def linkPath = Files.createLink(Paths.get("TheFile.txt"), regularFilePath)
- // 12. Clean up after myself: cannot delete 'playarea' directory until its
- // contents have first been deleted.
- Files.delete(symbolicLinkPath)
- Files.delete(linkPath)
- Files.delete(regularFilePath)
- Files.delete(renamedFilePath)
- Files.delete(playAreaDirPath)
'Language > JAVA' 카테고리의 다른 글
Java에서 System.getProperty() (2) | 2014.01.02 |
---|---|
tomcat7 cach filter (0) | 2013.12.03 |
double 지수 표현 제거 (0) | 2013.11.13 |
POI를 이용한 Excel ( *.xls, *.xlsx ) 읽기 (0) | 2013.11.13 |
Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable. (0) | 2013.09.17 |