2015-02-04 11 views
0

원본 csv를 수정했습니다. 지금은 그것이 수정 된 CSV 파일을 생성할지 여부를 새로운 CSV 파일을 생성하거나 그것에 업데이 트 ... 어떻게 가능할 수 있을까? 저는 csv의 단일 열 값을 추출하여 1 씩 증가 시켰습니다. 이제 수정 된 데이터가있는 CSV 파일을 원합니다. 어떻게 파일을 쓸 수 있습니까? 당신이 위의 절차를 수행하면자바에서 문자열을 사용하여 줄 단위로 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 java.io.*; 
    public class Main { 

    public static void main(String[] args) throws IOException { 

     String filename ="bank-full.csv"; 

     File file= new File(filename);   
     try {   
      Scanner inputStream = new Scanner(file); 
      inputStream.next();   
      while(inputStream.hasNext()) 
      { 

       String data= inputStream.next();     
       String[] values = data.split(";"); 
       double balance= Double.parseDouble(values[11]); 
       balance=balance+1;      

       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) { 
         sb.append(";"); 
        } 
       } 
       // get the new string 
       String newData = sb.toString(); 

        BufferedWriter writer = null; 
try 
{ 
    writer = new BufferedWriter(new FileWriter("bank-full.csv")); 
    writer.write(newData); 

} 
catch (IOException e) 
{ 
} 
finally 
{ 
    try 
    { 
     if (writer != null) 
     writer.close(); 
    } 
    catch (IOException e) 
    { 
    } 
} 
     System.out.println(newData); 
      }   
       inputStream.close(); 
     } catch (FileNotFoundException ex) { 
      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); 
     }   
      } 
+0

, 당신은 SRC를 무시하지 새로운 CSV 파일을 작성해야합니다 .. – Naren

답변

0

새로운 업데이트 된 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 java.io.*; 

public class Main { 

    public static void main(String[] args) throws IOException { 

     String filename = "src//bank-full.csv"; 

     File file = new File(filename); 

     BufferedWriter writer = null; 

     try { 
      writer = new BufferedWriter(new FileWriter("bank-full_updated.csv")); 


     } catch (IOException e) { 
     } 
      try { 
      Scanner inputStream = new Scanner(file); 
     // inputStream.next(); 
      while (inputStream.hasNext()) { 

       String data = inputStream.next(); 
       String[] values = data.split(";"); 
       double balance = Double.parseDouble(values[2]); 
       balance = balance + 1; 



       values[2] = String.valueOf(balance); 

       // iterate through the values and build a string out of them 
       StringBuilder sb = new StringBuilder(); 

      // String newData = sb.toString(); 
       for (int i = 0; i < values.length; i++) { 
        sb.append(values[i]); 
        if (i < values.length - 1) { 
         sb.append(";"); 


        } 

       } 
       // get the new string 

       System.out.println(sb.toString()); 
       writer.write(sb.toString()+"\n"); 
       } 
      writer.close(); 
      inputStream.close(); 
     } catch (FileNotFoundException ex) { 
      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); 
     } 


    } 
}