I haven't used Java in about 4 years since I've mainly done everything I've needed in C/C++. However, due to various reasons I have to fall back on Java for something. My basic question is as follows:
I need to setup a text output file that stores the values that I get from another method (which gets called a lot).
I have a basic setup of something like this right now:
Therefore, basically all I need to know is how to take the same println statement in that method and turn it into something that can be stored in output.txt
Thanks for the help!
I need to setup a text output file that stores the values that I get from another method (which gets called a lot).
I have a basic setup of something like this right now:
Code:
public class Lab3v3 {
public static Date DB = new Date(); //ignore this
public void writeToFile(String filename) {
BufferedWriter out = null;
try {
//Construct the BufferedWriter object
out = new BufferedWriter(new FileWriter(filename));
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
Later on in another class:
class Datab
{
private int readers;
public Datab()
{
this.readers = 0;
}
public void read(int num)
{
synchronized(this)
{
this.readers++;
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
System.out.println("DB value read =: " +dateFormat.format(Lab3v3.DB)+ " by reader num: " + num);
//I need to capture the same output as what I have for the System.out.println statement right above and store it into my output file.
}
public static void main(String[] args) contains
new Lab3v3().writeToFile("output.txt");
Therefore, basically all I need to know is how to take the same println statement in that method and turn it into something that can be stored in output.txt
Thanks for the help!