0
# File System Access
1
2
File system information, mount points, disk usage statistics, and file store details with comprehensive storage metrics.
3
4
## FileSystem Interface
5
6
Interface providing access to file system information and mounted file stores.
7
8
```java { .api }
9
interface FileSystem {
10
List<OSFileStore> getFileStores();
11
List<OSFileStore> getFileStores(boolean localOnly);
12
long getOpenFileDescriptors();
13
long getMaxFileDescriptors();
14
}
15
```
16
17
### Methods
18
19
- **`getFileStores()`** - Gets all mounted file stores
20
- **`getFileStores(boolean localOnly)`** - Gets file stores, optionally filtering to local only
21
- **`getOpenFileDescriptors()`** - Gets the current number of open file descriptors
22
- **`getMaxFileDescriptors()`** - Gets the maximum number of file descriptors allowed
23
24
## OSFileStore Interface
25
26
Represents a mounted file system or file store with detailed storage information.
27
28
```java { .api }
29
interface OSFileStore {
30
String getName();
31
String getVolume();
32
String getLabel();
33
String getLogicalVolume();
34
String getMount();
35
String getDescription();
36
String getType();
37
String getOptions();
38
String getUUID();
39
long getFreeSpace();
40
long getUsableSpace();
41
long getTotalSpace();
42
long getFreeInodes();
43
long getTotalInodes();
44
boolean updateAttributes();
45
}
46
```
47
48
### Properties
49
50
- **`getName()`** - Gets the file store name
51
- **`getVolume()`** - Gets the volume name
52
- **`getLabel()`** - Gets the volume label
53
- **`getLogicalVolume()`** - Gets the logical volume name
54
- **`getMount()`** - Gets the mount point path
55
- **`getDescription()`** - Gets a description of the file store
56
- **`getType()`** - Gets the file system type (e.g., "NTFS", "ext4", "HFS+")
57
- **`getOptions()`** - Gets mount options as a string
58
- **`getUUID()`** - Gets the unique identifier for the file store
59
60
### Space Information
61
62
- **`getFreeSpace()`** - Gets free space in bytes
63
- **`getUsableSpace()`** - Gets usable space in bytes (may be less than free space due to reserved space)
64
- **`getTotalSpace()`** - Gets total space in bytes
65
- **`getFreeInodes()`** - Gets the number of free inodes
66
- **`getTotalInodes()`** - Gets the total number of inodes
67
68
### Methods
69
70
- **`updateAttributes()`** - Updates the file store information and returns true if successful
71
72
## Usage Examples
73
74
### List All File Systems
75
76
```java
77
import oshi.SystemInfo;
78
import oshi.software.os.FileSystem;
79
import oshi.software.os.OSFileStore;
80
81
SystemInfo si = new SystemInfo();
82
FileSystem fileSystem = si.getOperatingSystem().getFileSystem();
83
84
List<OSFileStore> fileStores = fileSystem.getFileStores();
85
86
System.out.println("=== File Systems ===");
87
for (OSFileStore store : fileStores) {
88
System.out.printf("Name: %s%n", store.getName());
89
System.out.printf(" Mount: %s%n", store.getMount());
90
System.out.printf(" Type: %s%n", store.getType());
91
System.out.printf(" Total: %s%n", formatBytes(store.getTotalSpace()));
92
System.out.printf(" Free: %s%n", formatBytes(store.getFreeSpace()));
93
System.out.printf(" Usable: %s%n", formatBytes(store.getUsableSpace()));
94
System.out.printf(" Usage: %.1f%%%n%n",
95
(store.getTotalSpace() - store.getFreeSpace()) * 100.0 / store.getTotalSpace());
96
}
97
98
private static String formatBytes(long bytes) {
99
if (bytes < 1024) return bytes + " B";
100
if (bytes < 1024 * 1024) return String.format("%.1f KB", bytes / 1024.0);
101
if (bytes < 1024 * 1024 * 1024) return String.format("%.1f MB", bytes / 1024.0 / 1024.0);
102
return String.format("%.1f GB", bytes / 1024.0 / 1024.0 / 1024.0);
103
}
104
```
105
106
### Local File Systems Only
107
108
```java
109
import oshi.SystemInfo;
110
import oshi.software.os.FileSystem;
111
import oshi.software.os.OSFileStore;
112
113
SystemInfo si = new SystemInfo();
114
FileSystem fileSystem = si.getOperatingSystem().getFileSystem();
115
116
// Get only local file systems (excludes network mounts)
117
List<OSFileStore> localStores = fileSystem.getFileStores(true);
118
119
System.out.println("=== Local File Systems ===");
120
for (OSFileStore store : localStores) {
121
System.out.printf("%-20s %-10s %s%n",
122
store.getName(),
123
store.getType(),
124
store.getMount()
125
);
126
}
127
```
128
129
### Disk Usage Summary
130
131
```java
132
import oshi.SystemInfo;
133
import oshi.software.os.FileSystem;
134
import oshi.software.os.OSFileStore;
135
136
SystemInfo si = new SystemInfo();
137
FileSystem fileSystem = si.getOperatingSystem().getFileSystem();
138
139
List<OSFileStore> fileStores = fileSystem.getFileStores();
140
141
long totalSpace = 0;
142
long freeSpace = 0;
143
long usableSpace = 0;
144
145
System.out.println("=== Disk Usage Summary ===");
146
System.out.printf("%-30s %10s %10s %10s %8s%n",
147
"Mount Point", "Total", "Free", "Usable", "Used %");
148
System.out.println("-".repeat(70));
149
150
for (OSFileStore store : fileStores) {
151
totalSpace += store.getTotalSpace();
152
freeSpace += store.getFreeSpace();
153
usableSpace += store.getUsableSpace();
154
155
double usedPercent = (store.getTotalSpace() - store.getFreeSpace()) * 100.0 / store.getTotalSpace();
156
157
System.out.printf("%-30s %10s %10s %10s %7.1f%%%n",
158
store.getMount(),
159
formatSize(store.getTotalSpace()),
160
formatSize(store.getFreeSpace()),
161
formatSize(store.getUsableSpace()),
162
usedPercent
163
);
164
}
165
166
System.out.println("-".repeat(70));
167
System.out.printf("%-30s %10s %10s %10s %7.1f%%%n",
168
"TOTAL",
169
formatSize(totalSpace),
170
formatSize(freeSpace),
171
formatSize(usableSpace),
172
(totalSpace - freeSpace) * 100.0 / totalSpace
173
);
174
175
private static String formatSize(long bytes) {
176
if (bytes < 1024L * 1024L * 1024L) {
177
return String.format("%.1f MB", bytes / 1024.0 / 1024.0);
178
} else if (bytes < 1024L * 1024L * 1024L * 1024L) {
179
return String.format("%.1f GB", bytes / 1024.0 / 1024.0 / 1024.0);
180
} else {
181
return String.format("%.1f TB", bytes / 1024.0 / 1024.0 / 1024.0 / 1024.0);
182
}
183
}
184
```
185
186
### Detailed File Store Information
187
188
```java
189
import oshi.SystemInfo;
190
import oshi.software.os.FileSystem;
191
import oshi.software.os.OSFileStore;
192
193
SystemInfo si = new SystemInfo();
194
FileSystem fileSystem = si.getOperatingSystem().getFileSystem();
195
196
List<OSFileStore> fileStores = fileSystem.getFileStores();
197
198
for (OSFileStore store : fileStores) {
199
System.out.println("=== File Store Details ===");
200
System.out.println("Name: " + store.getName());
201
System.out.println("Volume: " + store.getVolume());
202
System.out.println("Label: " + store.getLabel());
203
System.out.println("Logical Volume: " + store.getLogicalVolume());
204
System.out.println("Mount Point: " + store.getMount());
205
System.out.println("Description: " + store.getDescription());
206
System.out.println("File System Type: " + store.getType());
207
System.out.println("Mount Options: " + store.getOptions());
208
System.out.println("UUID: " + store.getUUID());
209
210
System.out.println("\n--- Space Information ---");
211
System.out.println("Total Space: " + formatBytes(store.getTotalSpace()));
212
System.out.println("Free Space: " + formatBytes(store.getFreeSpace()));
213
System.out.println("Usable Space: " + formatBytes(store.getUsableSpace()));
214
System.out.println("Used Space: " + formatBytes(store.getTotalSpace() - store.getFreeSpace()));
215
216
if (store.getTotalInodes() > 0) {
217
System.out.println("\n--- Inode Information ---");
218
System.out.println("Total Inodes: " + store.getTotalInodes());
219
System.out.println("Free Inodes: " + store.getFreeInodes());
220
System.out.println("Used Inodes: " + (store.getTotalInodes() - store.getFreeInodes()));
221
System.out.printf("Inode Usage: %.1f%%%n",
222
(store.getTotalInodes() - store.getFreeInodes()) * 100.0 / store.getTotalInodes());
223
}
224
225
System.out.println();
226
}
227
```
228
229
### File Descriptor Information
230
231
```java
232
import oshi.SystemInfo;
233
import oshi.software.os.FileSystem;
234
235
SystemInfo si = new SystemInfo();
236
FileSystem fileSystem = si.getOperatingSystem().getFileSystem();
237
238
long openFDs = fileSystem.getOpenFileDescriptors();
239
long maxFDs = fileSystem.getMaxFileDescriptors();
240
241
System.out.println("=== File Descriptor Usage ===");
242
System.out.println("Open File Descriptors: " + openFDs);
243
System.out.println("Maximum File Descriptors: " + maxFDs);
244
245
if (maxFDs > 0) {
246
double usage = openFDs * 100.0 / maxFDs;
247
System.out.printf("File Descriptor Usage: %.1f%%%n", usage);
248
249
if (usage > 80) {
250
System.out.println("WARNING: High file descriptor usage!");
251
}
252
}
253
```
254
255
### Monitor File System Changes
256
257
```java
258
import oshi.SystemInfo;
259
import oshi.software.os.FileSystem;
260
import oshi.software.os.OSFileStore;
261
262
SystemInfo si = new SystemInfo();
263
FileSystem fileSystem = si.getOperatingSystem().getFileSystem();
264
265
// Monitor specific file system
266
String targetMount = "/"; // or "C:\\" on Windows
267
268
OSFileStore targetStore = null;
269
for (OSFileStore store : fileSystem.getFileStores()) {
270
if (store.getMount().equals(targetMount)) {
271
targetStore = store;
272
break;
273
}
274
}
275
276
if (targetStore != null) {
277
System.out.println("Monitoring file system: " + targetStore.getMount());
278
279
long initialFree = targetStore.getFreeSpace();
280
System.out.println("Initial free space: " + formatBytes(initialFree));
281
282
try {
283
Thread.sleep(5000); // Wait 5 seconds
284
} catch (InterruptedException e) {
285
Thread.currentThread().interrupt();
286
}
287
288
// Update and check again
289
if (targetStore.updateAttributes()) {
290
long currentFree = targetStore.getFreeSpace();
291
long change = currentFree - initialFree;
292
293
System.out.println("Current free space: " + formatBytes(currentFree));
294
if (change != 0) {
295
System.out.printf("Change: %s (%+d bytes)%n",
296
formatBytes(Math.abs(change)), change);
297
} else {
298
System.out.println("No change detected");
299
}
300
} else {
301
System.out.println("Failed to update file store attributes");
302
}
303
}
304
```
305
306
### Find File Systems by Type
307
308
```java
309
import oshi.SystemInfo;
310
import oshi.software.os.FileSystem;
311
import oshi.software.os.OSFileStore;
312
313
SystemInfo si = new SystemInfo();
314
FileSystem fileSystem = si.getOperatingSystem().getFileSystem();
315
316
List<OSFileStore> fileStores = fileSystem.getFileStores();
317
318
// Group by file system type
319
Map<String, List<OSFileStore>> storesByType = fileStores.stream()
320
.collect(Collectors.groupingBy(OSFileStore::getType));
321
322
System.out.println("=== File Systems by Type ===");
323
for (Map.Entry<String, List<OSFileStore>> entry : storesByType.entrySet()) {
324
String type = entry.getKey();
325
List<OSFileStore> stores = entry.getValue();
326
327
System.out.printf("%s (%d mount%s):%n",
328
type, stores.size(), stores.size() == 1 ? "" : "s");
329
330
for (OSFileStore store : stores) {
331
System.out.printf(" %-30s %s%n", store.getMount(), formatBytes(store.getTotalSpace()));
332
}
333
System.out.println();
334
}
335
```