不知道你说的是不是这个意思,下面一个简单的程序 能够在文件大于100字节时换到另一个文件名。只是示范用。代码很不严格,不知道是不是满足你的要求了。哈哈 import java.io.*; public class Redirect { public static void main(String args[]) { int i = 0; PrintStream pstream = null; try{ File f = new File("c:\\a" + i +".txt"); System.out.println(f.length()); if (f.length() > 100) { ++i; f = new File("c:\\a" + i +".txt"); //把这儿换成你的 resetPrintStream() 类似的函数吧 } pstream = new PrintStream(new FileOutputStream(f,true)); } catch(Exception e) { e.printStackTrace(); } System.setOut(pstream); System.out.println("hhhhhhhhhhhhhhheihei 你看看这个呀"); pstream.close(); }}
会不会是多线程是资源竞争引发的问题?resetPrintStream()-->将a.txt改成其他名字也许是这里出的问题。
楼主将输出文件改成按当前时间决定输出文件名称看看。
import java.io.*;
public class Redirect
{
public static void main(String args[])
{
int i = 0;
PrintStream pstream = null;
try{
File f = new File("c:\\a" + i +".txt");
System.out.println(f.length());
if (f.length() > 100)
{
++i;
f = new File("c:\\a" + i +".txt"); //把这儿换成你的 resetPrintStream() 类似的函数吧
}
pstream = new PrintStream(new FileOutputStream(f,true));
}
catch(Exception e)
{
e.printStackTrace();
}
System.setOut(pstream);
System.out.println("hhhhhhhhhhhhhhheihei 你看看这个呀");
pstream.close();
}}
我调用redirectToFile的时候,先调用resetPrintStream()将流关掉了
也不是每次运行的时候都会有问题
好多次执行操作以后才偶然出现一次
谢谢各位