DosFileAttributeView Hidden And Readonly
Flip Windows hidden and read-only flags on a file through DosFileAttributeView, then prove the read-only attribute rejects rewrites until you clear it.
Code
import java.io.IOException;
import java.nio.file.AccessDeniedException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.DosFileAttributeView;
public class DosFlagGuard {
public static void main(String[] args) throws IOException {
Path config = Files.createTempFile("app-", ".cfg");
Files.writeString(config, "retries=3\n");
// Windows 文件存储支持 DOS 属性视图;不支持的存储返回 null
DosFileAttributeView dos = Files.getFileAttributeView(config, DosFileAttributeView.class);
if (dos == null) {
System.out.println("DOS attributes unsupported on this store");
Files.deleteIfExists(config);
return;
}
dos.setHidden(true);
dos.setReadOnly(true);
System.out.println("hidden=" + Files.isHidden(config) + " writable=" + Files.isWritable(config));
try {
Files.writeString(config, "retries=4\n");
} catch (AccessDeniedException denied) {
System.out.println("read-only blocked the rewrite: " + denied.getFile());
}
dos.setReadOnly(false);
Files.writeString(config, "retries=4\n");
System.out.println("first line after clear: " + Files.readAllLines(config).get(0));
Files.delete(config);
}
}From setup to output — DosFileAttributeView Hidden And Readonly
DosFileAttributeView Hidden And Readonly comes down to the class DosFlagGuard.
Caveats
- No dependencies section needed; the standard library is the whole story.
Using it
The run emits 4 labeled sections — DOS attributes unsupported on this store, hidden=, read-only blocked the rewrite:, first line after clear: — one per step.
Key statements
DosFileAttributeView dos = Files.getFileAttributeView(config, DosFileAttributeView.class);
Path config = Files.createTempFile("app-", ".cfg");
Files.writeString(config, "retries=3\n");
Interactive Tools for This Topic
Argon2id Hash InfoEstimate Argon2id password-hash latency and memory usage, and check your parameters against OWASP 2024 minimum and recommended settings.bcrypt Cost EstimatorEstimate bcrypt cost factor latency and brute-force resistance, and get a recommended cost for your target hash time and attacker throughput.CORS Header Policy DebuggerSimulate browser CORS preflight and credential policies against server response headers to pinpoint blocked origins, methods, or headers and fix them.CSP Header BuilderBuild a Content-Security-Policy header from per-directive sources, skipping empty ones and rejecting a blanket wildcard.
FAQ
- What does DosFileAttributeView Hidden And Readonly print when run?
- Running the file directly is enough — expect labeled output lines such as `DOS attributes unsupported on this store`, `hidden=`, `read-only blocked the rewrite:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.