使用缓冲区写入器在新文件中写入输出(write output in new file using buffer writter)

编程入门 行业动态 更新时间:2024-10-28 10:27:55
使用缓冲区写入器在新文件中写入输出(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(); } } }

更多推荐

本文发布于:2023-07-17 07:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141027.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓冲区   新文件   write   output   buffer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!