Basic Java Programming Question

RedArmy

Platinum Member
Mar 1, 2005
2,648
0
0
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:

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!
 

Mr Burns

Junior Member
Sep 5, 2008
14
2
66
If you need to write the System.out.println statement to the file, one way in your case is to make Datab.read() return the String from the println ( public String read (int num) ... return System.out.println("DB Value read....) )

then in main

Datab db = new Datab();
String writeThisToTheFile = db.read(some int here that you need);
writeToFile("output.txt",writeThisToFile);

And then change writeToFile(String filename) to writeToFile(String filename, String textToWrite) {
....
out = new BufferedWriter(new FileWriter(filename));
out.write(textToWrit);
....
}

Look at BufferedWriter in the java api docs for more write options.
 

RedArmy

Platinum Member
Mar 1, 2005
2,648
0
0
Thanks for the quick reply Mr Burns. Could you possibly enlighten me as to what (some int here that you need) implies? The public String read (int num) line that you posted isn't being well received by the compiler, so I was wondering if maybe figuring out one part might help with the other. Thanks again.
 

SJP0tato

Senior member
Aug 19, 2004
267
0
76
Looks like the "some int here" part is referring to the internal "private int readers" variable in the Datab class. Basically, create an instance of the Datab class and pass in the int necessary for recording purposes (looks like it's the reader number?).

To sum up what Mr Burns is saying:
Modify the datab "read" method to return a string value, and instead of printing the output set it to a string and return it.

Then in your main class instantiate the datab class, call the method & pass in the reader number, and then save the returned string value to a file.
 

Mr Burns

Junior Member
Sep 5, 2008
14
2
66
Yes, the int refers to a number, pass a number to the Datab.read(int num) method. I don't know where you get this number, but that is unrelated to printing it out.

Code:
public class Lab3v3 {
    public static Date DB = new Date(); //ignore this
    public void writeToFile(String filename, String textToWrite) {
              BufferedWriter out = null;
              try {
                       //Construct the BufferedWriter object
                      out = new BufferedWriter(new FileWriter(filename),textToWrite);
                      out.write(textToWrite);
                 } catch (Exception e){   //Catch exception if any
                      System.err.println("Error: " + e.getMessage());
		 } finally {
                     if (out != null){
                         out.close();
                     }
                 } 
     	  }
}

Later on in another class:

class Datab{
    private int readers;
    public Datab()  {
       this.readers = 0;
    }
 
     public String read(int num) {
          synchronized(this) {
               this.readers++;
               DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
               return System.out.println("DB value read =: " +dateFormat.format(Lab3v3.DB)+ " by reader num: " +  num);
         }
    }


public static void main(String[] args){
    Datab() dataB = new Datab();

// somehow you get a int number to pass to dataB.read(int num) here, I just put something in for example
    String writeThisToTheFile = dataB.read(300);  
  
     // this will write "DB value read =: theDate by reader num: 300" to file output.txt
    new Lab3v3().writeToFile("output.txt",writeThisToTheFile);
}

This is really just ok for writing something once to a file. If you want to write a million lines you need to have that BufferedWriter outside of the method writeToFile, and not open/close it every time you do a write on the file, but open it once at the beginning, do the writing and when all done do a
out.close();

But the idea is once you have a BufferedWriter, you can pass Strings to it to write to the file.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |