0

I have input CSV. One of the columns has no value in the cell. While writing the data to another CSV I want to write NULL in that empty cell. I tried to replace "" with NULL but then it prints NULL in all cells. Also, I want to exclude adding NULL text in hard-coded columns of the code i.e. UniqueId, IsDelete, Rootpid. I want them to be empty.

Input Expected Output

Here is my code so far:

public static void main(String[] args) throws FileNotFoundException, IOException
    {


        String sourceFilePath = "C://MyData/Emp_Input.csv";
        String newFilePath = "C://MyData/Emp_Output.csv";
        File sourceCsvFile = new File(sourceFilePath);
        File finalCsvFile = new File(newFilePath);
        final Charset Encoding = Charset.forName("UTF-8"); 
        String cvsSplitBy = ",";


        String line = "";

        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(finalCsvFile), Encoding));


        try (BufferedReader br = new BufferedReader(new FileReader(sourceCsvFile))) 
        {
            line = br.readLine(); 
            String newFileLine = "UniqueID" + cvsSplitBy +line + cvsSplitBy  + "IsDelete" + cvsSplitBy + "Rootpid";
            writer.write(newFileLine);   
            writer.newLine();


            while ((line = br.readLine()) != null)  
            {   
            /*  if (line.contains("") )
                {

                    line = line.replace("", "NULL"); // missing code to replace empty cell of a csv with text as NULL
                } */


                else if (line.contains("TRUE") || line.contains("FALSE"))
                {
                    line = line.replace("TRUE", "1");
                    line = line.replace("FALSE", "0");

                } 


                    newFileLine =  cvsSplitBy + line + cvsSplitBy + cvsSplitBy;
                    writer.write(newFileLine);  
                    writer.newLine();


            }

            writer.close();

        }


    }

1 Answer 1

1

It is impossible, there is no difference between empty and null strings in csv.

Sign up to request clarification or add additional context in comments.

3 Comments

Agreed! But I have a requirement for further process of the application I want NULL as a text to be written in those cells which are empty.
You can configure excel to show NULL for empty strings in your csv, but if you write null in cvs, it's the value "null" which is of course different from empty and null object
Yes I want value null to be written in CSV.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.