如何在不使用java.io.*的情况下列出当前目录中的文件和目录?
最佳答案
这实际上是可能的,无需编写任何 JNI 或进行任何运行时调用。
import java.net.URL;
import sun.net.www.content.text.PlainTextInputStream;
public class NoIO {
public static void main(String args[]) {
NoIO n = new NoIO();
n.doT();
}
public void doT() {
try {
//Create a URL from the user.dir (run directory)
//Prefix with the protocol file:/
//Users java.net
URL u = new URL("file:/"+System.getProperty("user.dir"));
//Get the contents of the URL (this basically prints out the directory
//list. Uses sun.net.www.content.text
PlainTextInputStream in = (PlainTextInputStream)u.getContent();
//Iterate over the InputStream and print it out.
int c;
while ((c = in.read()) != -1) {
System.out.print((char) c);
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
一点点思考和无聊就会产生令人惊奇的效果(并且无法仓促得出结论(有志者事竟成))。
您也可以使用 ClassLoader 来完成此操作,通过覆盖它,在某些时候 Java 必须迭代类路径中的所有文件,通过在该点 Hook ,您可以打印出它尝试的所有文件不使用任何类型的 java.io.* 来加载。
经过一番调查,我认为这不太容易实现,尤其是对于家庭作业而言,除非是某种 RE'ing 作业或取证作业。
关于java - 在 Java 中列出文件而不使用 java.io,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1571840/