我需要从指定路径读取文件。例如:我有一个名为abc.txt的文件,它位于/dev/user/assets/data/abc.png中。 如果必须从Zip文件中读取,我们会执行类似
的操作Zipfile zipFile = new ZipFile("test.zip");
ZipEntry entry = zipFile.getEntry(imagePath); // where image path is /dev/user/assets/data/abc.png.
是否有类似于上面的代码从文件夹中读取? 喜欢
File folder = new Folder("Users");
并将路径“/dev/user/assets/data/abc.png”添加到上面并阅读图像。
答案 0 :(得分:2)
是的,File
有两个args构造函数File(File parent, String child)
,它完全符合您的描述(您可能只需抛出子项的前导'/')。看看JavaDoc
答案 1 :(得分:0)
我不确定我是否完全理解你想做什么。但是,如果您想要做的只是阅读具有给定路径的文件的内容,那么您就不应该使用File API。
相反,您应该使用new FileReader(textFilePath);
或new FileInputStream(imageFilePath);
查看以下example,摘自java教程。
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;
public class CopyLines {
public static void main(String[] args) throws IOException {
BufferedReader inputStream = null;
PrintWriter outputStream = null;
try {
inputStream = new BufferedReader(new FileReader("xanadu.txt"));
outputStream = new PrintWriter(new FileWriter("characteroutput.txt"));
String l;
while ((l = inputStream.readLine()) != null) {
outputStream.println(l);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
}