How to implement this?

X

xz

I want all the classes write the runtime information into one common
file, let's say, log.
So I define a BufferedWriter log in one of the classes and make it
public static, as follows,

import java.io.BufferedWriter;
import java.io.FileWriter;

public class Tester {

static String path = "/home/xi/Desktop/D2V/
validation/";
static FileWriter fwlog = new FileWriter(path +
"log");
public static BufferedWriter log = new
BufferedWriter(fwlog);
//the rest code
}


However, the constructor of FileWriter throws exception so it does not
compile:
--------------------
Tester.java:11: unreported exception java.io.IOException; must be
caught or declared to be thrown
static FileWriter fwlog = new FileWriter(path +
"log");
^
1 error
--------------------

What can I do to handle this exception?

looks like I cannot either catch it here or put the "throws
IOException...." sentence after "public class Tester".
 
C

Chris

xz said:
I want all the classes write the runtime information into one common
file, let's say, log.
So I define a BufferedWriter log in one of the classes and make it
public static, as follows,

import java.io.BufferedWriter;
import java.io.FileWriter;

public class Tester {

static String path = "/home/xi/Desktop/D2V/
validation/";
static FileWriter fwlog = new FileWriter(path +
"log");
public static BufferedWriter log = new
BufferedWriter(fwlog);
//the rest code
}


However, the constructor of FileWriter throws exception so it does not
compile:
--------------------
Tester.java:11: unreported exception java.io.IOException; must be
caught or declared to be thrown
static FileWriter fwlog = new FileWriter(path +
"log");
^
1 error
--------------------

What can I do to handle this exception?

looks like I cannot either catch it here or put the "throws
IOException...." sentence after "public class Tester".

Try this:

public class Tester {
private static BufferedWriter logger;

public static BufferedWriter getLogger() throws IOException {
if (logger==null) {
logger = new BufferedWriter(whatever...);
}
return logger;
}
}

In your code, call:

Tester.getLogger().write("some message");

Of if you want to avoid the call to if (logger==null) on every log
statement, create a static init() method and call that before the first
call to getLogger().
 
X

xz

Try this:

public class Tester {
private static BufferedWriter logger;

public static BufferedWriter getLogger() throws IOException {
if (logger==null) {
logger = new BufferedWriter(whatever...);
}
return logger;
}

}

In your code, call:

Tester.getLogger().write("some message");

Of if you want to avoid the call to if (logger==null) on every log
statement, create a static init() method and call that before the first
call to getLogger().


Thanks. That works.
 
L

Lew

Chris said:
Try this:

public class Tester {
private static BufferedWriter logger;

public static BufferedWriter getLogger() throws IOException {
if (logger==null) {
logger = new BufferedWriter(whatever...);
}
return logger;
}
}

In your code, call:

Tester.getLogger().write("some message");

Of if you want to avoid the call to if (logger==null) on every log
statement, create a static init() method and call that before the first
call to getLogger().

The latter likely avoids the threading complications of the unsynchronized
test-and-set idiom you presented. In a case like this there is no advantage
to lazy initialization.

If your Logger class is from log4j it is already thread safe, of course.

One more approach is a static initializer:

public class Tester
{
private static final BufferedWriter logger;
static
{
String path = "/home/xi/Desktop/D2V/validation/";
try
{
logger = new BufferedWriter( new FileWriter(
new File( path, "log" )));
}
catch ( IOException exc )
{
String msg = "Cannot open log file \""+ path +"log\"";
throw new IllegalStateException( msg, exc );
}
}
....
}

This is like using the static init() method without naming the method. The
body of the static initializer could be the body of a static initialization
method like initLogger().

public class Tester
{
private static final BufferedWriter logger = initLogger();
....
}
 
X

xz

Chris wrote:
The latter likely avoids the threading complications of the unsynchronized
test-and-set idiom you presented. In a case like this there is no advantage
to lazy initialization.

If your Logger class is from log4j it is already thread safe, of course.

One more approach is a static initializer:

public class Tester
{
private static final BufferedWriter logger;
static
{
String path = "/home/xi/Desktop/D2V/validation/";
try
{
logger = new BufferedWriter( new FileWriter(
new File( path, "log" )));
}
catch ( IOException exc )
{
String msg = "Cannot open log file \""+ path +"log\"";
throw new IllegalStateException( msg, exc );
}
}
...

}

This is like using the static init() method without naming the method. The
body of the static initializer could be the body of a static initialization
method like initLogger().

This is more close to what I wanted originally.
Thank you!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top