2012-03-21 2 views
0

지정된 경로에서 파일을 읽어야합니다. 예 : 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"를 부여하고 읽는다.

+2

.png 이미지 파일 내에있는 텍스트 파일입니까? : S – Alderath

답변

2

예, File에는 정확히 설명하는 것을 수행하는 두 개의 args 생성자 File(File parent, String child)이 있습니다 (아이의 '/'를 앞에 붙여야 할 수도 있음). 살펴보기 JavaDoc

+0

고마워 .. .. 일했다 :) – nishMaria

0

정확히 무엇을하고 싶은지 잘 모르겠다. 그러나 원하는 작업이 주어진 경로의 파일 내용을 읽는 것 뿐이라면 File API를 사용하면 안됩니다.

대신 당신은 자바 튜토리얼에서 가져온 다음 example에서 new FileReader(textFilePath); 또는 new FileInputStream(imageFilePath);

봐를 사용해야합니다.

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(); 
      } 
     } 
    } 
} 
관련 문제