M
marcwentink
Dear group,
I have written the following class below, which is a class to write
something in a log file. Now I want to make vars file, out and method
write(pStr) static, so I do not have to initialize an object when I use
this class.
Nevertheless when I make file static variable I encounter a problem. If
I try to initialize file in the code following "private BufferedWriter
out;" javac complains it wants to see a try, catch block. But when I
use the catch block, it gives me an error too. If I initialize the
static vars in the constructor I still have to make an object (once) to
use the static method since I have to be sure the static vars are
initialized, if I do not file for example is null. I am a but stucked
here now.
The latest thing I tried is copied, this gives a compile error below:
compile:
[javac] Compiling 2 source files to E:\cvs\PROJE~9B\java\pd\build
[javac] E:\cvs\PROJE~9B\java\pd\s\n\a\pd\LogIt.java:11: i
llegal start of type
[javac] try
[javac] ^
[javac] E:\cvs\PROJE~9B\java\pd\s\n\a\pd\LogIt.java:49: <
identifier> expected
[javac] }
[javac] ^
[javac] 2 errors
import java.io.*;
public class LogIt
{
private static File file;
private static BufferedWriter out;
try
{
file = new File("logs/PGLogFile.txt");
out = new BufferedWriter(new FileWriter("logs/PGLogFile.txt",
true));
}
catch (IOException ex)
{
System.out.println("EventLog: failed opening event log with " + ex);
System.exit(1);
}
public LogIt()
{
/* try
{
file = new File("logs/PGLogFile.txt");
out = new BufferedWriter(new FileWriter("logs/PdfGenLogFile.txt",
true));
}
catch (IOException ex)
{
System.out.println("EventLog: failed opening event log with " + ex);
System.exit(1);
}
*/ }
public static void write(String pStr)
{
try
{
out.write(pStr);
out.close();
}
catch (IOException ex)
{
System.out.println("EventLog: failed opening event log with " + ex);
System.exit(1);
}
}
}
Appreciation in Advance, Marcus Wentink
I have written the following class below, which is a class to write
something in a log file. Now I want to make vars file, out and method
write(pStr) static, so I do not have to initialize an object when I use
this class.
Nevertheless when I make file static variable I encounter a problem. If
I try to initialize file in the code following "private BufferedWriter
out;" javac complains it wants to see a try, catch block. But when I
use the catch block, it gives me an error too. If I initialize the
static vars in the constructor I still have to make an object (once) to
use the static method since I have to be sure the static vars are
initialized, if I do not file for example is null. I am a but stucked
here now.
The latest thing I tried is copied, this gives a compile error below:
compile:
[javac] Compiling 2 source files to E:\cvs\PROJE~9B\java\pd\build
[javac] E:\cvs\PROJE~9B\java\pd\s\n\a\pd\LogIt.java:11: i
llegal start of type
[javac] try
[javac] ^
[javac] E:\cvs\PROJE~9B\java\pd\s\n\a\pd\LogIt.java:49: <
identifier> expected
[javac] }
[javac] ^
[javac] 2 errors
import java.io.*;
public class LogIt
{
private static File file;
private static BufferedWriter out;
try
{
file = new File("logs/PGLogFile.txt");
out = new BufferedWriter(new FileWriter("logs/PGLogFile.txt",
true));
}
catch (IOException ex)
{
System.out.println("EventLog: failed opening event log with " + ex);
System.exit(1);
}
public LogIt()
{
/* try
{
file = new File("logs/PGLogFile.txt");
out = new BufferedWriter(new FileWriter("logs/PdfGenLogFile.txt",
true));
}
catch (IOException ex)
{
System.out.println("EventLog: failed opening event log with " + ex);
System.exit(1);
}
*/ }
public static void write(String pStr)
{
try
{
out.write(pStr);
out.close();
}
catch (IOException ex)
{
System.out.println("EventLog: failed opening event log with " + ex);
System.exit(1);
}
}
}
Appreciation in Advance, Marcus Wentink