Search code examples
filestreamfilestreamstreamwriterfilewriter

write output in new file using buffer writter


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
        }
    }

Solution

  • 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();
        }
       }
       }