2016-06-29 1 views
0

텍스트 파일 (txt-Format)이 포함 된 큰 데이터 세트가 있습니다. textfiles이 형식의 데이터가 포함됩니다데이터 세트에서 일부 열을 제거하는 방법은 무엇입니까?

Name, Number, Timestamp, Sensordata1, Sensordata2, ... , Sensordata40 
Name, Number, Timestamp, Sensordata1, Sensordata2, ... , Sensordata40 
Name, Number, Timestamp, Sensordata1, Sensordata2, ... , Sensordata40 

지금 나는 번호 및 모든 라인에서 타임 스탬프를 제거해야합니다.

순간에 내 코드 :

try{ 
      // Open the file that is the first 
      // command line parameter 

      FileInputStream fstream = new FileInputStream("file.txt"); 

      // Get the object of DataInputStream 
      DataInputStream in = new DataInputStream(fstream); 
      BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
      String strLine; 

      //Read File Line By Line 
      while ((strLine = br.readLine()) != null) { 

       // Print the content on the console 
       System.out.println (strLine); 
      } 

      //Close the input stream 
      in.close(); 
     }catch (Exception e){//Catch exception if any 
      System.err.println("Error: " + e.getMessage()); 
     } 

이 어떻게 자바에서이 작업을 수행 할 수 있습니다?

+0

Somethin g는'String.split (',')'처럼 배열을 가져와 필요한 것을 제거합니다. – Furtiro

답변

1

몇 가지 방법을 수행 할 수 있습니다. 예를 들어 열과 같은 항목을 검색하는 데 걸리는 시간에 따라 배열을 기준으로 예제 1과 2에서 제거하려는 열을 정적으로 입력하는 것이 가장 간단합니다. 이이처럼 예를 수행 할 수 있습니다

package stackquestions; 

import java.io.BufferedReader; 
import java.io.DataInputStream; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

public class StackQuestions { 


    public static void main(String[] args) { 
     try{ 
      // Open the file that is the first 
      // command line parameter 

      FileInputStream fstream = new FileInputStream("file.txt"); 

      // Get the object of DataInputStream 
      DataInputStream in = new DataInputStream(fstream); 
      BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
      String strLine; 

      //Read File Line By Line 
      while ((strLine = br.readLine()) != null) { 
       String[] data=strLine.split(","); 


       for(int i=0;i<data.length;i++){ 
        if(i!=1 && i!=2){ 
         System.out.println (data[i]); 
        } 

       } 
       // Print the content on the console 

      } 

      //Close the input stream 
      in.close(); 
     }catch (Exception e){//Catch exception if any 
      System.err.println("Error: " + e.getMessage()); 
     } 
} 
} 

또 다른 방법은 읽고있는 선이 첫 번째 줄인지 여부에 따라 열을 감지하는 것입니다, 첫 번째 줄을 가정 할 첫 번째 줄 (포함 읽을 분할 각 반복에서 색인을 확인하여 데이터가 속한 열을 확인하십시오.

+0

쉼표로 구분 된 파일을 사용하기 때문에 더 많은 파일을 공식적으로 읽을 수있는 다른 방법이 있습니다. 보다 안정적인 프로그램 : [link] (http://www.mkyong.com/java/how-to-read-and-parse-csv-file-in-java/). 이 방법은 1 & 2와 같이 정적으로 입력 된 숫자를 참조하지 않고 나중에 데이터에서 제거 할 열을 변경하는 데있어 더 많은 유연성을 허용합니다. 어느 쪽이든 도움이 되었으면 좋겠다. 행운을 빌어 요. – D3181

+0

그레이트 참조! 고마워! – basti12354

0

동일한 값이 항상 같은 열에 나타나면 모든 값을 ArrayList에 추가하고 필요하지 않은 값의 제거를 반복 한 다음 쓰기 만하면됩니다. 다시 파일로.