我希望程序的大部分输出都打印到一个文件中,但我仍然希望在命令提示符中显示几行输出。
有办法做到这一点吗?
更具体地说,我有一个LZW compressor
,它使用文件写入器将其压缩输出打印到LZW file
("Algorithms"textbooK中的"Binary Standard Out")
它还解压缩LZW files
并将输出打印回txt file
我希望能够获得系统定时操作,但将信息打印到命令提示符,而不是输出文件。
有人有解决方案吗?
public class LZWMod
{
private static final int startW = 9;
private static final int maxL = 65536;
private static final int R = 256; // number of input chars
private static int L = 512; // number of codewords = 2^W
private static int W = startW; // codeword width
public static void compress()
{
TSTMod<Integer> st = new TSTMod<Integer>(); //set up the symbol table
for (int i = 0; i < R; i++) //fill it with the ascii characters
{
StringBuilder sb = new StringBuilder("" +(char) i);
st.put(sb, i);
}
int code = R+1; // R is codeword for EOF
StringBuilder sb = new StringBuilder(); //set up stringbuilder to do the work
while (!BinaryStdIn.isEmpty()) //loop as long as there is stuff to read
{
sb.append(BinaryStdIn.readChar()); //add a letter to the SB (to get the ball rolling)
while (st.contains(sb) && !BinaryStdIn.isEmpty()) //keep adding letters as long as its a valid prefix
sb.append(BinaryStdIn.readChar());
if (code < maxL) st.put(sb, code ++); //if we arent out of room, add to the table
char lookAhead = sb.charAt(sb.length() -1); //identify the lookahead
sb.delete(sb.length() -1, sb.length()); //chop off the lookahead
BinaryStdOut.write(st.get(sb), W); //add the pattern to our symbol table
if ( L != maxL && code == L) //if necessary, enlarge the symbol table
{
W = W +1;
L = (int)(Math.pow(2.0, (double)W ));
}
sb.delete(0, sb.length()); //erase the stringbuilder
sb.append(lookAhead); //put the look-ahead back in there
}
BinaryStdOut.write(st.get(sb), W); //make sure we get the last character
BinaryStdOut.write(R, W);
BinaryStdOut.close();
//I would like to put some code here printing the system time to
//the command prompt
}
public static void expand()
{
String[] st = new String[L];
int i; // next available codeword value
// initialize symbol table with all 1-character strings
for (i = 0; i < R; i++)
st[i] = "" + (char) i;
st[i++] = ""; // (unused) lookahead for EOF
int codeword = BinaryStdIn.readInt(W);
String val = st[codeword];
while (true)
{
BinaryStdOut.write(val);
codeword = BinaryStdIn.readInt(W);
if (codeword == R) break;
String s = st[codeword];
if (i == codeword) s = val + val.charAt(0); // special case hack
if (i < maxL -1) st[i++] = val + s.charAt(0);//if (i < L)
if (i == L -1 && st.length != maxL)
{
W = W +1;
double z = Math.pow(2.0, (double)W );
L = (int)z;
st = Arrays.copyOf(st, st.length * 2);
}
val = s;
}
BinaryStdOut.close();
}
集成像log4j这样的日志记录工具,并将其用于输出。这些记录器可以通过属性文件轻松配置为写入多个流。在log4j中,这些输出通道被称为Appenders。并行写入文件和控制台基本上是这些Appenders的标准场景。您将需要org.apache.log4j.ConsoleAppender
和org.apache.log4j.RollingFileAppender
。
示例:
log4j.rootLogger=DEBUG,F,C
log4j.appender.F=org.apache.log4j.RollingFileAppender
log4j.appender.F.File=${user.home}/product.log
log4j.appender.F.MaxFileSize=1MB
log4j.appender.F.MaxBackupIndex=5
log4j.appender.F.layout=org.apache.log4j.PatternLayout
log4j.appender.F.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.C=org.apache.log4j.ConsoleAppender
log4j.appender.C.layout=org.apache.log4j.PatternLayout
log4j.appender.C.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
这定义并独立地配置两个附加器CCD_ 7&根(=默认)记录器的C
,该记录器将向其转发日志级别为DEBUG
或以上的所有消息。F和C是自由选择的名称。
您可以在以后轻松地重新配置记录器,即使是在部署之后,也无需重新编译等。