-2
static void goOut(String in) {
                  //instance variables
    String fileCopy = currentLine +     in;
    try {
        FileWriter writer = new FileWriter(output,true);


        writer.write(line1 + System.getProperty("line.separator", "\r\n"));
        writer.write(fileCopy + System.getProperty("line.separator", "\r\n"));


    }   catch(IOException ex) {
        ex.printStackTrace();
    }
}

Edited code to the correct standard as pointed out by other users.

Adz
  • 2,809
  • 10
  • 43
  • 61
  • 9
    Why would you create two `FileWriter` objects writing to the same file at the same time? That sounds like a recipe for disaster. – Jon Skeet Jul 09 '14 at 19:21
  • Thanks for the reply. I tried to solve the problem that way but it didn't work. With one FileWriter it also doesn't work. – Adz Jul 09 '14 at 19:24
  • you can call writer again right? `writer.write(fileCopy + System.get....)` and then call `writer.close();` – Rahul Shardha Jul 09 '14 at 19:25
  • Have you tried writing what you want written once outside the method, before calling the method a number of times? – Matt Shank Jul 09 '14 at 19:25
  • Why did you think it would help? And what is `currentLine`? – Jon Skeet Jul 09 '14 at 19:26
  • I'm honestly not sure why the .close(); is in the try block and not in a finally block. – EpicPandaForce Jul 09 '14 at 19:26
  • what are: `line1`, `copyFile`, `in`, `currentLine` ? how do you use this method ? and yes, you definitely don't want to open two writers to the same file... – Nir Alfasi Jul 09 '14 at 19:26
  • currentLine is an instance String variable which gets part of a sentence from another text file. in is a String passed on from another method, – Adz Jul 09 '14 at 19:30

3 Answers3

0

I believe you want to separate the jobs the "goOut" is responsible for.

You should make "goOut" only write the numbers (in your example).

The writing of the y's (in your example) should not be apart of the method and called once, at the start of writing to the file.

Also, @Jon Skeet is right about the multiple FileWriters. Use one, since its the same file.

Greg Hilston
  • 2,397
  • 2
  • 24
  • 35
0

of course because thats what you r telling it to do. every time is called it writes both x and the number. a quick fix: you can keep a flag if it is the first run set it flag = true. and check within ur method, sth like this:

public class YourClass{

private boolean didRun = false;

static void goOut(String in) {

    ...... init ur file and writer
    if(!didRun)
       writer.write(Y);

     writer.write(in);
     writer.close();
     didRun = true;
    }
}

I dont know the rest of the code but i think thats what u need

eldjon
  • 2,800
  • 2
  • 20
  • 23
  • Hmm I did what you asked by the y is never printed – Adz Jul 09 '14 at 19:39
  • It worked but I get an java.io.IOException: Stream closed error. It still creates the correct file type though :/ – Adz Jul 09 '14 at 19:57
  • just double check ur code if u r properly opening/closing one single writer. i dont know what u r trying to achieve entirely but i think if u state ur requirements it might be more helpful. and dont forget to mark my answer if it helped – eldjon Jul 09 '14 at 20:02
0

Agree, sounds like a disaster.

When you use multiple writers to access the file, I would expect to get unpredictable results.

I dont think there is any guarantee that FileWriter1 would complete the task before FileWriter2.

In addition, the method is not synchronized.

Jama Djafarov
  • 358
  • 3
  • 11