使用缓冲区写入器在新文件中写入输出(write output in new file using buffer writter)
我希望在新文件中编写此程序的输出。如何生成像xls这样的新文件。 或其他,并将结果写入其中。 我已经读过文件然后应用kmean聚类算法并生成输出现在我想写。
package kmean; //package greenblocks.statistics; import java.io.IOException; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; 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 { 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(); int i=0; for(int clusterNum : assignments) { System.out.printf("Instance %d -> Cluster %d \n", i, clusterNum); i++; } // TODO code application logic here } }Output of this program I want to write in new file.so how can I generate new file like xls. or other and write the results in it. I had already read the file then applied kmean clustering algorithm and generate output now I want to write that.
package kmean; //package greenblocks.statistics; import java.io.IOException; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; 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 { 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(); int i=0; for(int clusterNum : assignments) { System.out.printf("Instance %d -> Cluster %d \n", i, clusterNum); i++; } // TODO code application logic here } }最满意答案
使用此代码在新文本文件中编写输出...这可能会对您有所帮助
包com.mkyong;
import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; public class WriteToFileExample { public static void main(String[] args) { try { String content = "This is the content to write into file"; File file = new File("/filePath/filename.txt"); // if file doesnt exists, then create it if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); bw.write(content); bw.close(); System.out.println("Done"); } catch (IOException e) { e.printStackTrace(); } } }use this code to write your output in new text file... may this will help you
package com.mkyong;
import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; public class WriteToFileExample { public static void main(String[] args) { try { String content = "This is the content to write into file"; File file = new File("/filePath/filename.txt"); // if file doesnt exists, then create it if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); bw.write(content); bw.close(); System.out.println("Done"); } catch (IOException e) { e.printStackTrace(); } } }更多推荐
发布评论