2014-12-12 5 views
1

모든 행이 vertice를 나타내는 파일이 있습니다. (예 - 1.0 0.0 정점 A에 대한 형식) 내 작업은 X와 정점의 Y 값과 "정점 A"의 라벨을 절약 할 방법Java를 사용하여 파일에서 데이터 읽기

public void read(InputStream is) throws IOException 

을 만드는 것입니다.

public void read(InputStream is) throws IOException { 

     try { 
      Reader r = new InputStreamReader(is); 
      BufferedReader br = new BufferedReader(r); 
      while(br.readLine()!=null){ 
       //something 
      } 

     } catch(IOException ex){ 
       ex.printStackTrace(); 
      } 
    } 

도 내가 정확히 만드는 방법

public void read(File file) throws IOException 

을 만들 필요가 동일하지만 파일 대신 스트림 : 나는 그것을 제대로 구문 분석하는 방법을 모른다. 이 두 가지 방법의 차이점을 말해 줄 수 있습니까?

답변

0

나는 다음을 수행하고 코드를 통해 설명 할 것 :)

public void read(InputStream is) throws IOException { 
    //You create a reader hold the input stream (sequence of data) 
    //You create a BufferedReader which will wrap the input stream and give you methods to read your information 
    BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
    handleVerticeValues(reader); 
    reader.close(); 
} 

public void read(File file) throws IOException { 
    //You create a buffered reader to manipulate the data obtained from the file representation 
    BufferedReader reader = new BufferedReader(new FileReader(file)); 
    handleVerticeValues(reader); 
    reader.close(); 
} 

private void handleVerticeValues(BufferedReader reader) throws IOException { 
    //Then you can read your file like this: 
    String lineCursor = null;//Will hold the value of the line being read 

    //Assuming your line has this format: 1.0 0.0 verticeA 
    //Your separator between values is a whitespace character  
    while ((lineCursor = reader.readLine()) != null) { 
     String[] lineElements = lineCursor.split(" ");//I use split and indicates that we will separate each element of your line based on a whitespace 
     double valX = Double.parseDouble(lineElements[0]);//You get the first element before an whitespace: 1.0 
     double valY = Double.parseDouble(lineElements[1]);//You get the second element before and after an whitespace: 0.0 
     String label = lineElements[2];//You get the third element after the last whitespace 
     //You do something with your data 
    } 
} 

당신은 그 :) 또 다른 접근 방식뿐만 아니라 StringTokenizer를 사용하여 분할을 사용하여 피할 수 있습니다.

다른 대답에서 언급했듯이 파일은 파일 시스템의 노드 표현 일 뿐이며 파일 시스템에있는 요소를 가리키고 있지만 내부적으로이 시점에서는 데이터 나 정보를 보유하지 않습니다 파일, 내 말은, 단지 파일 (정보가 파일, 디렉토리 또는 이와 유사한 것) 인 경우 (존재하지 않으면 FileNotFoundException을 수신함).

InputStream은 일련의 데이터입니다.이 시점에서해야 할 일에 따라 BufferedReader, ObjectInputStream 또는 다른 구성 요소에서 처리하거나 읽어야하는 정보가 필요합니다.

더 많은 정보를 위해, 당신은 또한 당신의 친절한 API 문서로 요청할 수 있습니다 :

https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html

https://docs.oracle.com/javase/7/docs/api/java/io/File.html

감사와 ... 행복 코딩 : 당신이 경우

2

파일은 파일 시스템의 노드를 나타내며, 스트림은 읽기 헤드가있는 데이터 시퀀스를 나타냅니다. 입력 스트림에서 결과를 읽기위한 파일 열기. System.In은 파일을 제공하지 않은 입력 스트림의 예이며 stdin의 스트림입니다.

public void read(File file) throws IOException 
{ 
//using your input stream method, read the passed file 
//Create an input stream from the given file, and then call what you've already implemented. 
read(new FileInputStream(file)); 
//I assume your read function closes the stream when it's done 
} 
+0

그것은 좋은 것입니다 읽기 조작 후에 스트림을 닫습니다. –

관련 문제