0

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.

1
  • 2
    A text file which resides inside a .png image file? :S Commented Mar 21, 2012 at 14:09

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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(); } } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.