2015-02-03 13 views
0

나는 약 46000 인스턴스와 17 특성을 가진 데이터 세트 csv 파일이 있습니다 .. 그럼 내가 열 값을 추출하고 1로 증가하여 그 값을 지금 수정 이미 String[] values의 값의 문자열 배열을 가지고 ....어떻게 CSV 파일을 수정 후 특정 숫자 열 값을 사용하여 자바

 import java.io.File; 
    import java.io.FileNotFoundException; 
    import java.util.Scanner; 
    import java.util.logging.Level; 
    import java.util.logging.Logger; 
    import java.io.FileWriter; 
    //import com.csvreader.CsvWriter; 
    //import com.opencsv.CSVReader; 
    //import com.opencsv.CSVWriter; 
    //import au.com.bytecode.opencsv.CSVWriter; 
    public class Main { 

     public static void main(String[] args) { 
      // TODO code application logic here   
      String filename ="bank-full.csv"; 

    File file= new File(filename);   
      try {   
       Scanner inputStream = new Scanner(file); 
       inputStream.next();   
       while(inputStream.hasNext()) 
       { 
        double abc; 
        String data= inputStream.next();     
        String[] values = data.split(";"); 
        double balance= Double.parseDouble(values[11]); 
        balance=balance+1;      

          System.out.println(balance);     
       }   
        inputStream.close(); 
      } catch (FileNotFoundException ex) { 
       Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); 
      }   
       } 

답변

0

를 내가 얻을 수있는 수정 values.how와 csv 파일을 원한다. 그렇다면 수정 된 balancevalues[11]에 할당 한 다음 값을 결합하고 세미콜론을 붙이면 어떨까요?

당신은 System.out.println(balance); 후이 추가 시도 할 수 있습니다 :

System.out.println(balance); 

// replace original value with the modified balance 
values[11] = String.valueOf(balance); 

// iterate through the values and build a string out of them 
StringBuilder sb = new StringBuilder(); 
for (int i = 0; i < values.length; i++) { 
    sb.append(values[i]); 
    if (i < values.length - 1) { 
     // if it is not the last value, put a semicolon in between 
     sb.append(";"); 
    } 
} 
// get the new string 
String newData = sb.toString(); 

System.out.println(newData); 

새로운 값은 현재 문자열 newData에 저장됩니다. 새 .csv 파일에 newData을 작성하기 만하면됩니다.