2015-04-07 86 views
1

我有一個包含45311實例的輸入文件。應用我的編程任務後。當我將它寫回新文件時,它實際上只寫入43371實例。它已成功運行,但其餘實例在哪裏。java文件寫入器寫入實例的不完整數據

package kmean; 
//package greenblocks.statistics; 

import java.io.IOException; 



import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.FileWriter; 

import weka.clusterers.SimpleKMeans; 
import weka.core.Instances; 

/** 
* 
* @author admin 
*/ 
public class Kmean { 

     public static BufferedReader readDataFile(String filename) { 
     BufferedReader inputReader = null; 

     try { 
      inputReader = new BufferedReader(new FileReader(filename)); 
     } catch (FileNotFoundException ex) { 
      System.err.println("File not found: " + filename); 
     } 

     return inputReader; 
    } 


    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) throws IOException, Exception { 
     BufferedWriter writer = null; 

     try { 
      writer = new BufferedWriter(new FileWriter("perturbed1.csv")); 
     } 
     catch (IOException e) { 
     } 
     SimpleKMeans kmeans = new SimpleKMeans(); 

     kmeans.setSeed(10); 

     //important parameter to set: preserver order, number of cluster. 
     kmeans.setPreserveInstancesOrder(true); 
     kmeans.setNumClusters(5); 

     BufferedReader datafile = readDataFile("elecNormNew.arff"); 
       // BufferedReader datafile = readDataFile("perturbed.csv"); 
     Instances data = new Instances(datafile); 


     kmeans.buildClusterer(data); 

     // This array returns the cluster number (starting with 0) for each instance 
     // The array has as many elements as the number of instances 
     int[] assignments = kmeans.getAssignments(); 
       StringBuilder sb = new StringBuilder(); 
     int i=0; 
     for(int clusterNum : assignments) { 
      // System.out.printf("Instance %d -> Cluster %d \n", i, clusterNum); 
        sb.append(i); 
        sb.append(";"); 
        sb.append(clusterNum); 
        sb.append("\n"); 
        //System.out.printf("\n"); 
      i++; 
     } 
System.out.println(sb.toString());   
      writer.write(sb.toString()+"\n"); 
     // TODO code application logic here 
    } 
} 

回答

2

你應該寫的所有數據後,調用writer.close()末。

+0

謝謝。是工作 .............. –

0

insted的的writer.write(sb.toString()+"\n");

嘗試writer.write(sb.toString()+writer.newLine());

,並完成你的writig進展情況,

writer.flush(); 
writer.close(); 

有一些問題,我自己爲 「\ n」 莫比那的問題。

2

關於緩衝文件編寫者的整潔事實是,他們將輸入並保存它,直到緩衝區已滿。這減少了I/O操作。至多有一個寫入操作適合一個硬盤寫入緩衝區,因此操作系統將整個緩衝區作爲一個I/O命令。缺點是如果最後如果你沒有flush()緩衝區,其餘的內容將不會被寫入磁盤。如果您致電close()任何待處理的字節將被寫入並釋放資源。

try(Inputstream is = new ...) { 

如果您有任何數據的代碼之後寫的,你可以用.flush(),以確保數據寫的是:在Java 7及以上的,你可以通過只在您的try聲明中打開流使用autoclosing功能。

緩衝區大小默認設置爲8k,但這可能會從jre和版本中警惕。