user2859994
user2859994

Reputation: 49

Java FileWriter Windows Issues

I'm writing a Java program that prints information to a text file as it runs. When testing my program on my Mac in Eclipse, it works properly--both creating the text file and writing to it. I exported my program to a .jar file to test on different computers, and when I run the .jar file on other Mac computers, everything works as it should. However, when I run the program on a Windows computer, a file is created but nothing is written to it. I have searched for Filewriter issues on different operating systems, but haven't come up with anything so far--thoughts?

Below is the code I use to write to the file. It's within a timer task and writes an angle value and a time stamp to the file on each execution. Everything else within the timer task works properly on all operating systems, so that isn't the issue. If it helps, here is an example of a file might look like:

1    270
2    30
3    26
4    29

etc. with the first column containing the time count and the second column containing an angle value.

The code:

public AnAngleTimerTask(Model newModel, int newCounter, int newEndAnalysis,
        int newSampleEvery) {
    model = newModel;
    counter = newCounter;
    endAnalysis = newEndAnalysis;
    sampleEvery = newSampleEvery;
    angles = new int[(int) Math
            .floor((endAnalysis - counter) / sampleEvery)];
    times = new int[(int) Math.floor((endAnalysis - counter) / sampleEvery)];
    file = newFile();
}

@Override
public void run() {
    PrintWriter out;
     System.out.println(counter);
    // get angle and write to file
    if (!model.getTimer().getPaused()) {
        int usbAngle = retrieveAngle();
        times[i] = counter;
        angles[i] = usbAngle;
        try {
            out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
            out.printf("%-10d %-10d\n", counter, usbAngle);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        model.setAngle(usbAngle);
        i++;
        counter = counter + sampleEvery;
    }
    if (counter == endAnalysis) {
        model.setMeanAngle(angles);
        model.setR(angles);
        System.out.println("End Analysis");
        cancel();
    }
}

public File newFile() {
    String nameString;
    Date myDate = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd:HH-mm-ss");
    String myDateString = sdf.format(myDate);
    if (model.getArenaName() == null) {
        nameString = "Arena" + model.getName() + "Tracker";
    } else {
        nameString = model.getArenaName();
    }
    File file = new File(model.getPath() + "/" + nameString + "_"
            + model.getName() + "_" + myDateString + ".txt");
    model.setLastDataRecordedToFile(file.getAbsolutePath());
    return file;
}

Please let me know if you need any other information or code for context.

Upvotes: 0

Views: 404

Answers (3)

user2859994
user2859994

Reputation: 49

After some trial and tribulation, I discovered that the issue was actually that I was using a file name with a colon in it (generated by the SimpleDateFormat), which is permitted on Macs but not on Windows. Got rid of the colon and everything works as it should!

Upvotes: 0

user207421
user207421

Reputation: 311023

PrintWriter swallows exceptions, and you aren't calling checkError() to see if one happened. Try it with a BufferedWriter. You'll have to use something other than printf() of course.

Upvotes: 0

ac3
ac3

Reputation: 196

Is the file empty? Or is the output not as expected. Depending on that, changing from

out.printf("%-10d %-10d\n", counter, usbAngle);

to

out.printf("%-10d %-10d%n", counter, usbAngle);

might help. With the first one it sends a raw line feed. Second one does the platform specific conversion and generate a \r\n on windows

Upvotes: 0

Related Questions