-1

My value object has multiple lines say:

abc
xyz
pqr
mno

I want to convert these lines into upper case and simultaneously write into a csv file. Me below code only writes the last entry i.e. "mno" into the o/p file. Because it is overwriting over existing ones.

private Object transformFieldValue(Object value) throws IOException {
    String upperCase = value.toString().toUpperCase();
    File file = new File("andy.csv");
    FileWriter fr=null;

    try {
        fr = new FileWriter(file);
        fr.write(upperCase);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            fr.flush();
            fr.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    return upperCase;
}

How to achieve the result so that andy.csv reads like:

ABC
XYZ
PQR
MNO
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
earl
  • 738
  • 1
  • 17
  • 38
  • 1
    Possible duplicate of [Java: FileWriter: Create a new line](http://stackoverflow.com/questions/18549704/java-filewriter-create-a-new-line) – OldProgrammer Dec 12 '16 at 18:53
  • What is the actual type of 'value'? Presumably it's not actually Object, but is derived from Object. – jarmod Dec 12 '16 at 18:53
  • Similar to jarmod's question, I'm not seeing where you are passing in the 4 lines. Are you calling 'transformFieldValue' multiple times for each line? | I'd try using `new FileWriter(file, true)`. Per the javadoc, when you pass in the 'true' parameter, "data will be written to the end of the file rather than the beginning." – snowmanjack Dec 12 '16 at 18:57
  • Does the string representation of the object have carriage return (`\r`) characters in it, by any chance? – David Conrad Dec 12 '16 at 19:06

2 Answers2

0

Because it is overwriting over existing ones.

Yes, but it's overriding the file. My guess is you have an loop that calls transformFieldValue, each time you enter that method, you're creating a new file and a new FileWritter:

  1. Convert your File file and your FileWriter fr members of your class (i.e. global variables).

  2. Create 3 methods: createNewFile(), writeToFile(), closeFile().

    a. createNewFile() should contain the following:

    File file = new File("andy.csv");
    FileWriter fr=null;
    try {
        fr = new FileWriter(file);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    

    b. writeToFile() should contain:

    String upperCase = value.toString().toUpperCase();
    fr.write(upperCase);
    

    c. closeFile() should contain:

    fr.flush();
    fr.close();
    

Add try-catch-finally blocks where needed

But if you want more and better help, please post a proper Minimal, Complete, and Verifiable example

Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89
0

The answer to your question is maybe:

Change

fr = new FileWriter(file);

to

fr = new FileWriter(file, true);

"true" will enable appending bytes to the end of the file.

Do not forget to create a line break. This may be different for differrent for different operating systems:

fw.write("\r\n");

... e.g. for windows line breaks.

BUT:

As you mentioned that it should "convert these lines into upper case and simultaneously write into a csv file" it is clearly a Violation of the Single Responsibility Principle. I suggest to put the Code on Trial in the whole on "Stack Exchange Code Review".

oopexpert
  • 767
  • 7
  • 12