简体   繁体   中英

How to append text in a csv file at the end of each line Java?

I have a csv file with testdata:

31-September-2017 01:52:57 02:11:25
31-September-2017 01:52:57 02:11:25

I want to write the test result(PASS/FAIL) for every line of data at the end of each line, like this:

31-September-2017 01:52:57 02:11:25 PASSED
31-September-2017 01:52:57 02:11:25 FAILED

I am using openCSV api to read the file content. When I open the same file using CSVWriter, it is deleting all the contents of the file. Used BufferedWriter as well, same problem.

Please suggest me how I can achieve this with the original contents of file remaining same, and appending the test result at the end of each line. Thanks.

Use BufferedReader and BufferedWriter for this:

  • Read the file line by line
  • Write another file line by line
  • Add the PASSED / FAILED to each line before writing
  • Delete the old file and rename the new file

I'll try to explain why this will be the best way:

  • Its efficient ( O(n) while n is the size of the file)
  • Easy to implement (Just about 20-30 lines of code for the reading and writing part)
  • Will be readable and understandable (Readability is a very important point.)

Something simillar like below should work with opencsv.

import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Example {    
    public static void main(String[] args) {
        try {
            String fileName = "C:\\Users\\eritrean\\Desktop\\yourfile.csv";
            List<String[]> myEntries =  readFile(fileName);
            List<String[]> testedEntries = new ArrayList<>();
            for(String[] row : myEntries){
                String[] withTestResult = addTestResult(row,randomResult());
                testedEntries.add(withTestResult);
            }
            writeToFile(fileName,testedEntries);
        } catch (IOException ex) {

        }
    }
    public static List<String[]> readFile(String fileName) throws IOException {
        CSVReader reader = new CSVReader(new FileReader(fileName),'\t','\"',0);
        List<String[]> myEntries = reader.readAll();
        return myEntries;
    }

    public static String[] addTestResult(String[] row, String result){        
        return (String.join("\t", row)+"\t"+result).split("\t");
    }
    public static String randomResult(){  
        Random rand = new Random();
        return rand.nextBoolean()?"PASSED":"FAILED";
    }
    public static void writeToFile(String fileName, List<String[]> myEntries) throws IOException {
        try (CSVWriter writer = new CSVWriter(new FileWriter(fileName), '\t',CSVWriter.NO_QUOTE_CHARACTER)) {
            for(String[] row: myEntries){
                writer.writeNext(row);
            }
        }
    }    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM