你好!我正在尝试使用该方法Files.readAllLines
,但出现异常AccessDeniedException
public class Test {
public static void main(String[] args) throws IOException {
Path directory = Paths.get("D:\\it\\A");
boolean isDirectory = Files.isDirectory(directory);
boolean isWritable = Files.isWritable(directory);
System.out.println(isDirectory);
System.out.println(isWritable);
List<String>list = new ArrayList<>();
list=Files.readAllLines(directory);
}
}
安慰:
true
true
Exception in thread "main" java.nio.file.AccessDeniedException: D:\it\A
at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:89)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
at java.base/sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:236)
at java.base/java.nio.file.Files.newByteChannel(Files.java:380)
at java.base/java.nio.file.Files.newByteChannel(Files.java:432)
at java.base/java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:422)
at java.base/java.nio.file.Files.newInputStream(Files.java:160)
at java.base/java.nio.file.Files.newBufferedReader(Files.java:2923)
at java.base/java.nio.file.Files.readAllLines(Files.java:3413)
at java.base/java.nio.file.Files.readAllLines(Files.java:3454)
at Test.main(Test.java:20)
Process finished with exit code 1
我试图在不同的本地驱动器上创建一个文件夹,该文件夹确实存在于该地址,该文件夹和所有子文件夹的“只读”模式均已关闭。防病毒被禁用,另一个应用程序不使用该文件夹。我搜索了互联网,但没有找到答案。操作系统 Windows 10 PRO,开发环境:
IntelliJ IDEA 2022.1(社区版)Build #IC-221.5080.210,2022年4月12日构建运行时版本:11.0.14.1+1-b2043.25 amd64
我知道这可能是一个愚蠢的错误,但我找不到答案。在此先感谢您的帮助!
InputStream
:
public class Test {
public static void main(String[] args) throws IOException {
String path = "D:\\it\\A\\A2\\TEXT1.txt";
InputStream is = new FileInputStream(path);
int a=0;
while ((a=is.read())!=-1) {
System.out.println((char)a);
}
is.close();
}
}
也有效Files.readAllBytes
:
public class Test {
public static void main(String[] args) throws IOException {
String path = "D:\\it\\A\\A2\\TEXT1.txt";
String content = new String(Files.readAllBytes(Paths.get(path)));
System.out.println(content);
}
}
UPD。检查通过cmd
此文件夹设置的读写权限。
取自https://stackoverflow.com/questions/1844688/how-to-read-all-files-in-a-folder-from-java
或(Java 8+)