Skip to content

Commit

Permalink
correcting file writer to not leave trailing newline
Browse files Browse the repository at this point in the history
  • Loading branch information
hansenjn committed Jul 31, 2024
1 parent 7021122 commit 12dc29d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
10 changes: 5 additions & 5 deletions 2D Analyzer/src/main/java/motiQ2D/MotiQ_2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,7 @@ public void windowClosing(WindowEvent winEvt) {
//Generate legends

//Save results text files
outputTextFile tw1 = new outputTextFile(dir[task] + filePrefix + ".txt");
OutputTextFile tw1 = new OutputTextFile(dir[task] + filePrefix + ".txt");
tw1.append("Saving date: " + FullDateFormatter.format(currentDate) + " Analysis started: " + FullDateFormatter.format(startDate));
tw1.append("Image");
tw1.append(" name: " + name [task]);
Expand Down Expand Up @@ -1535,7 +1535,7 @@ public void windowClosing(WindowEvent winEvt) {
}

void saveLegendST(int frames, String savePath){
outputTextFile tw =new outputTextFile(savePath);
OutputTextFile tw =new OutputTextFile(savePath);

String legendTw2 = " particle nr";
String legendTw2l2 = " ";
Expand Down Expand Up @@ -1635,7 +1635,7 @@ void saveLegendLT(int frames, String savePath){
String spacer = "";
for(int g = 0; g < nrOfGroups; g++)spacer += " ";

outputTextFile tp = new outputTextFile (savePath);
OutputTextFile tp = new OutputTextFile (savePath);
String legendLTheader = " ";
legendLTheader += " " + "Long-term scanning behaviour" + spacer;
for(int s = 0; s < 5; s++){
Expand Down Expand Up @@ -1771,7 +1771,7 @@ void saveLegendLT(int frames, String savePath){
}

void saveOneRowResultsST(ArrayList<TimelapseParticle2D> particles, int frames, double xCorr, double yCorr, String directory, String name, String savePath){
outputTextFile tw3 =new outputTextFile(savePath);
OutputTextFile tw3 =new OutputTextFile(savePath);
for(int p = 0; p < particles.size(); p++){
TimelapseParticle2D particle = particles.get(p);
String appendTxtTw3 = "" + directory;
Expand Down Expand Up @@ -2330,7 +2330,7 @@ void saveOneRowResultsST(ArrayList<TimelapseParticle2D> particles, int frames, d
}

void saveOneRowResultsLT(ArrayList<TimelapseParticle2D> particles, int frames, double xCorr, double yCorr, String directory, String name, String savePath){
outputTextFile tw3 =new outputTextFile(savePath);
OutputTextFile tw3 =new OutputTextFile(savePath);
for(int p = 0; p < particles.size(); p++){
TimelapseParticle2D particle = particles.get(p);
String appendTxtTw3 = "" + directory;
Expand Down
34 changes: 20 additions & 14 deletions 2D Analyzer/src/main/java/motiQ2D/outputTextFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,39 @@
import java.io.FileWriter;
import java.io.IOException;

class outputTextFile {
class OutputTextFile {
String path;
FileWriter writer = null;
String output;

public outputTextFile(String outputPath) {
public OutputTextFile(String outputPath) {
output = "";
path = outputPath;
try {
writer = new FileWriter(new File(path));
} catch (IOException e) {
e.printStackTrace();
}

}

public void append(String text) {
try {
writer.write(text + "\n");
} catch (IOException e) {
e.printStackTrace();
}
output += (text + "\n");
}

public void finish() {
public boolean finish() {
if(output.length() == 0) return false;
if(path.length() == 0) return false;

output = output.replaceAll("\n$", "");

try {
writer = new FileWriter(new File(path));
writer.write(output);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
output = "";
path = "";
return true;
}

public void changePath(String newOutputPath) {
path = newOutputPath;
}
}

0 comments on commit 12dc29d

Please sign in to comment.