Hi I have 3 classes first and second class are computations and 3rd is where I save data from the first 2 classes to a text file.
I am attempting to reuse my method that writes calculated values from first and 2nd class into text file using FileWriter and BufferedWriter.
So i have something like this:
1) first class do calculations
2) then use 3rd class writeDetected to save it in text file
3) 2nd class do calculations
4) then use 3rd class again using writeDetected
Everything is working except when I wanted to save the calculations I’ve done in 2nd class. The contents in text file contains the previous (first class calculations) + the second class calculation. What I wanted to have is a file that has the calculation of first class and another file with 2nd class calculations. Any ideas on how I should do this?
edited:
Just to illustrate this:
it should look something like this
class 1 {1,2,3,4}
class 2 {5,6,7,8}
What I have at the moment is
class 1 {1,2,3,4}
class 2 {1,2,3,4,5,6,7,8}
this is my code:
public void writeDetected(){
Scanner getFileName = new Scanner(System.in);
//Enter file name here for first class then after calculation of
// 2nd class enter another file name
String getInp = getFileName.nextLine();
try{
FileWriter theFile = new FileWriter(getInp);
BufferedWriter readBuf = new BufferedWriter (theFile);
String convDoubleToString;
for (Double getVal: detected){
convDoubleToString = getVal.toString();
readBuf.write(convDoubleToString);
readBuf.newLine();
}
readBuf.close();
}
catch (IOException e){
System.out.println(e);
}
}
Try this,
Sent the
pathfrom thecalling method as Argumentto theParameters of the calledmethod, in this way you can write your file content to whatever file you want (Herethe called method will bewriteMyFile(String path))Eg: