Last active
July 19, 2016 01:28
-
-
Save takaki/88c2718d8650655e46e1daa40910cf20 to your computer and use it in GitHub Desktop.
オンメモリのファイルシステムJimfs ref: http://qiita.com/takaki@github/items/a9b270993719699c415a
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
package example.misc; | |
import com.google.common.jimfs.Configuration; | |
import com.google.common.jimfs.Jimfs; | |
import java.nio.charset.StandardCharsets; | |
import java.nio.file.FileSystem; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
public class JimFsDemo { | |
public static void main(final String[] args) throws Exception { | |
try (final FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) { | |
final Path tmp = fs.getPath("/tmp"); | |
Files.createDirectories(tmp); | |
final Path tmpFile = Files.createTempFile(tmp, "jimfs", "tmp"); | |
Files.write(tmpFile, "abcdef".getBytes(StandardCharsets.UTF_8)); | |
// System.out.println(tmpFile.toFile().getAbsolutePath()); // toFileで例外が出るのでダメ | |
System.out.println(tmpFile.getParent().getFileName()); | |
System.out.println(tmpFile.getFileName()); | |
final byte[] read = Files.readAllBytes(tmpFile); | |
System.out.println(new String(read, StandardCharsets.UTF_8)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment