Reputation: 135
I need to read a file from a specified path. E.g: I have a file called abc.txt and it resides inside /dev/user/assets/data/abc.png. If one has to read from a Zip file we do something like
Zipfile zipFile = new ZipFile("test.zip");
ZipEntry entry = zipFile.getEntry(imagePath); // where image path is /dev/user/assets/data/abc.png.
Is there a code something similar to the above for reading from a folder? Like
File folder = new Folder("Users");
and give the path "/dev/user/assets/data/abc.png" to the above and read the image.
Upvotes: 0
Views: 1288
Reputation: 3859
I'm not sure if I understand exactly what you want to do. But if what you want to do is simply to read the contents of a file with a given path, then you should'nt use the File API.
Instead you should use a new FileReader(textFilePath);
or a new FileInputStream(imageFilePath);
Look at the following example, taken from the java tutorials.
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();
}
}
}
}
Upvotes: 0
Reputation: 47608
Yes, File
has two-args constructor File(File parent, String child)
that does exactly what you describe (you may just have to throw the leading '/' of the child). Take a look at the JavaDoc
Upvotes: 2