-2

im using a large csv file having large dataset about 17 attribute and approx 40000 rows. i want to write ith position of each rows value in reverse order(1st row ith ","separated value is replace with 40000th ith"," separated...2nd is replace with39999...likewise all)(here i extracted 12th value= ith ).how can i do this pls help me.!!

package demo;
 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.*;
/**
 *
/**
 *
 * @author admin
 */
public class Demo {

    /**
     * @param args the command line arguments
     */
      public static void main(String[] args)throws IOException {
        String filename = "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();
            double Tuple;
            int count=0;
            Tuple = 0; 


     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);   
    count=count+1;

            // 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(Demo.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
  • The code you have posted is only vaguely related to the problem statement. Please clarify whether you need to reverse the lines of the file (40,000th swapped with 1st, 39,999th swappend with 2nd,...) or whether you actually need to modify lines (as your code is indicating). You never say that a value should be incremented - but your code increments the 12th item (not the 11th!) in a row. – laune Feb 24 '15 at 15:07
  • sry for that...now i had made changes ..actually i want to write those value in reverse order. – bharatbhai patel Feb 24 '15 at 15:25
  • Which "values"? The lines of the file or the field of each line? (If the latter: why do you post code that doesn't come even close?) If you need to reverse the lines: Read and store lines in a `List`, then iterate from last to first, as @OferLando suggests. – laune Feb 24 '15 at 16:00

1 Answers1

0

Not sure why you'd want to do this, but it sounds like you need to read the file into a Collection (e.g. ArrayList), adding each line as a record, and then loop over the list from the end to the begining - and print everything back into a (new) file...

Ofer Lando
  • 814
  • 7
  • 12