命令提示符和输出文件之间的Java分割输出

时间:2015-06-25 07:44:04

标签: java io command-prompt

我希望将程序的大部分输出打印到文件中,但我仍然希望在命令提示符中显示几行输出。

有没有办法做到这一点?

更具体地说,我有LZW compressor使用文件编写器将其压缩输出打印到LZW file(“Algorithims”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();

}

1 个答案:

答案 0 :(得分:0)

集成log4j之类的日志工具,并将其用于输出。可以通过属性文件轻松配置这些记录器以写入多个流。在log4j中,这些输出通道称为Appenders。并行写入文件和控制台基本上是这些Appender的 标准场景。您需要org.apache.log4j.ConsoleAppenderorg.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

这定义并独立配置两个appender F&amp; C用于root(= default)记录程序,它将日志级别DEBUG或更高级别的所有消息转发给它们。 F和C是自由选择的名称。

您可以稍后轻松地重新配置记录器,即使在部署之后,也无需重新编译等。

相关问题