0
# File System Creation
1
2
The `Jimfs` class provides static factory methods for creating in-memory file systems with different configuration options.
3
4
## Core Imports
5
6
```java
7
import com.google.common.jimfs.Jimfs;
8
import com.google.common.jimfs.Configuration;
9
import java.nio.file.FileSystem;
10
```
11
12
## Factory Methods
13
14
### Default File System Creation
15
16
Create a file system with platform-appropriate defaults.
17
18
```java { .api }
19
public static FileSystem newFileSystem();
20
```
21
22
Creates a file system using `Configuration.forCurrentPlatform()` which selects Unix, Mac OS X, or Windows configuration based on the current operating system.
23
24
**Usage Example:**
25
```java
26
FileSystem fs = Jimfs.newFileSystem();
27
Path root = fs.getPath("/"); // Unix-style on Linux/Mac, "C:\" style on Windows
28
```
29
30
### Named File System Creation
31
32
Create a file system with a custom name for URI identification.
33
34
```java { .api }
35
public static FileSystem newFileSystem(String name);
36
```
37
38
The file system uses the given name as the host part of its URI. For example, with name `"test-fs"`, the file system URI will be `jimfs://test-fs` and path URIs will be `jimfs://test-fs/path/to/file`.
39
40
**Parameters:**
41
- `name` - String name for the file system (used in URI host part)
42
43
**Usage Example:**
44
```java
45
FileSystem fs = Jimfs.newFileSystem("my-test-filesystem");
46
// File system URI: jimfs://my-test-filesystem
47
// Path URI example: jimfs://my-test-filesystem/work/data.txt
48
```
49
50
### Configured File System Creation
51
52
Create a file system with a specific configuration.
53
54
```java { .api }
55
public static FileSystem newFileSystem(Configuration configuration);
56
```
57
58
**Parameters:**
59
- `configuration` - Configuration object defining file system behavior
60
61
**Usage Example:**
62
```java
63
Configuration config = Configuration.unix()
64
.toBuilder()
65
.setMaxSize(1024 * 1024 * 1024) // 1GB limit
66
.setBlockSize(4096) // 4KB blocks
67
.setSupportedFeatures(Feature.LINKS, Feature.SYMBOLIC_LINKS)
68
.build();
69
70
FileSystem fs = Jimfs.newFileSystem(config);
71
```
72
73
### Named and Configured File System Creation
74
75
Create a file system with both a custom name and configuration.
76
77
```java { .api }
78
public static FileSystem newFileSystem(String name, Configuration configuration);
79
```
80
81
**Parameters:**
82
- `name` - String name for the file system
83
- `configuration` - Configuration object defining file system behavior
84
85
**Usage Example:**
86
```java
87
Configuration winConfig = Configuration.windows()
88
.toBuilder()
89
.setWorkingDirectory("C:\\temp")
90
.setAttributeViews("basic", "dos")
91
.build();
92
93
FileSystem fs = Jimfs.newFileSystem("windows-test", winConfig);
94
```
95
96
## URI Scheme
97
98
All Jimfs file systems use the URI scheme:
99
100
```java { .api }
101
public static final String URI_SCHEME = "jimfs";
102
```
103
104
This constant defines the scheme used in all Jimfs file system URIs.
105
106
## File System Lifecycle
107
108
File systems created by these methods:
109
110
- Are immediately available for use with all `java.nio.file` APIs
111
- Exist only in memory and are destroyed when the JVM terminates
112
- Can be closed explicitly using `fileSystem.close()`
113
- Support concurrent access from multiple threads
114
- Are registered with the system provider for URI-based access via `Paths.get(URI)`
115
116
## Integration with Standard APIs
117
118
Once created, Jimfs file systems work with all standard Java NIO.2 APIs:
119
120
```java
121
FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
122
123
// Standard path operations
124
Path workDir = fs.getPath("/work");
125
Path dataFile = workDir.resolve("data.txt");
126
127
// Standard file operations
128
Files.createDirectories(workDir);
129
Files.write(dataFile, "Hello World".getBytes());
130
List<String> lines = Files.readAllLines(dataFile);
131
132
// Standard stream operations
133
try (InputStream in = Files.newInputStream(dataFile)) {
134
// Process file content
135
}
136
```
137
138
## Error Handling
139
140
File system creation methods may throw:
141
142
- `IllegalArgumentException` - If the name parameter contains invalid characters for URI construction
143
- `AssertionError` - If an unexpected IOException occurs during file system setup (this should not happen in normal operation)