为什么我在这个循环中收到java.io.IOException?

为什么我在这个循环中收到java.io.IOException?,java,stream,ioexception,writer,Java,Stream,Ioexception,Writer,我已经编写了下面的代码块,它拾取一个纯文本文件,如果文件上的当前修改日期大于循环的最后一次迭代,则将该文件解析为HTML版本,并将其上载到ftp服务器。我相信这个问题与作者有关,但我不明白这里发生了什么。循环的第一次迭代似乎执行得很好,然后休眠5分钟并抛出以下错误: java.io.IOException: Stream closed at java.io.BufferedWriter.ensureOpen(BufferedWriter.java:116) at java.io.Buffered

我已经编写了下面的代码块,它拾取一个纯文本文件,如果文件上的当前修改日期大于循环的最后一次迭代,则将该文件解析为HTML版本,并将其上载到ftp服务器。我相信这个问题与作者有关,但我不明白这里发生了什么。循环的第一次迭代似乎执行得很好,然后休眠5分钟并抛出以下错误:

java.io.IOException: Stream closed
at java.io.BufferedWriter.ensureOpen(BufferedWriter.java:116)
at java.io.BufferedWriter.write(BufferedWriter.java:221)
at java.io.Writer.write(Writer.java:157)
at java.io.Writer.append(Writer.java:227)
at PrintOutConvosFtp3.main(PrintOutConvosFtp3.java:130)
以下是引发错误的代码:

import java.io.*;
import java.util.*;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import java.sql.Timestamp;

public class PrintOutConvosFtp3
{
public static void main(String[] args) throws IOException, InterruptedException
    {
    boolean infiniteLoop = true;
    long currentLoopModified = 0;
    long lastLoopModified = 0;

    while (infiniteLoop)
    {
    currentLoopModified = new File("C:/Documents and Settings/Cuckoo/Desktop/Syss-convos.LOG").lastModified();
    if (currentLoopModified > lastLoopModified)
    {

    //Read in the conversation log
BufferedReader reader = new BufferedReader(new FileReader("C:/Documents and Settings/Cuckoo/Desktop/Syss-convos.LOG"));
FileWriter output = new FileWriter("C:/Documents and Settings/Cuckoo/Desktop/Conversations.html");
List<String> lines = new ArrayList<String>();
String line = null;
while ((line = reader.readLine()) != null)
    //Remove some unnecessary clutter from the log
 {
 if (!(line.contains("just hung up!!!") || line.contains("just left the Realm.")
        || line.contains("Hurry, I've many esoteric secrets to divulge, and welcome to BaDbOy's realm.")
        || line.contains("For custom MegaMud paths and additional information, check out the website:")
        || line.contains("Syss gossips: Discuss new ideas/issues & see the most up to date information on Facebook!")
        || line.contains("Syss gossips: http://www.facebook.com/groups/EsotericEdits/")
        || line.contains("Syss gossips: MME Dats, Megamud path files and quest walkthroughs are available at my site")
        || line.contains("Syss gossips: www.esoteric-edits.fhero.net")
        || line.contains("telepaths: @")
        || line.contains("I'm a bot.  Try telepathing me with @commands.")
        || line.contains("Syss gossips: Remember, you can telepath me @commands for useful things like adding lives.")
        || line.contains("Syss gossips: Bring a friend, help keep mud alive!")
        || line.contains("You say \"http://esoteric-edits.fhero.net/\"")
        || line.contains("For a list of available commands, you can telepath me with @commands.")))
 {
 //Make the dates american style
     String day = line.substring(0,2);
     String month = line.substring(3,5);
lines.add(month + "/" + day + line.substring(5));
}


 }
//initialize the output file with HTML header
output.write("<html>");
output.write(System.getProperty("line.separator") + "\t<head>");
output.write(System.getProperty("line.separator") + "\t\t<link type=\"text/css\" rel=\"stylesheet\" href=\"stylesheet.css\"/>");
output.write(System.getProperty("line.separator") + "\t\t<title>Esoteric Edits BBS - Conversation Log</title>");
output.write(System.getProperty("line.separator") + "\t</head>");
output.write(System.getProperty("line.separator") + "\t<body>"+ System.getProperty("line.separator") + System.getProperty("line.separator"));
output.write(System.getProperty("line.separator") + "<div id='cssmenu'>");
output.write(System.getProperty("line.separator") + "\t\t<center><img src=\"logo_10_2.png\">");
output.write(System.getProperty("line.separator") + "<ul>");
output.write(System.getProperty("line.separator") + "\t<li> <a href='index.html'><span>Home</span></a></li>");
output.write(System.getProperty("line.separator") + "\t<li> <a href='downloads.html'><span>Downloads</span></a></li>");
output.write(System.getProperty("line.separator") + "\t<li> <a href='Quests.html'><span>Quest Walkthroughs</span></a></li>");
output.write(System.getProperty("line.separator") + "\t<li> <a href='https://www.facebook.com/groups/EsotericEdits/'><span>Facebook</span></a></li>");
output.write(System.getProperty("line.separator") + "\t<li> <a href='captures.html'><span>Captures</span></a></li>");
output.write(System.getProperty("line.separator") + "\t<li class='last'> <a href='FAQs.html'><span>FAQs</span></a></li>");
output.write(System.getProperty("line.separator") + "</ul></center>");
output.write(System.getProperty("line.separator") + "</div><div id='mainpage'>");
output.write(System.getProperty("line.separator") + "<center><img src=\"divider.png\"></center>");

//write out a new file with HTML coloration
for (ListIterator<String> iter = lines.listIterator(); iter.hasNext(); ) 
{
    String currentline = iter.next();
    output.write("<b>"); //make everything bold
    if (currentline.contains("gangpaths: "))
{
output.write(System.getProperty("line.separator") + "<font color=\"#808000\">" + currentline + "<br></font>");
}
    else if (currentline.contains("gossips: ") || currentline.contains("auctions: "))
{
output.write(System.getProperty("line.separator") + "<font color=\"#FF00FF\">" + currentline + "<br></font>");
}
else if (currentline.contains("Broadcast from "))
{
output.write(System.getProperty("line.separator") + "<font color=\"yellow\">" + currentline + "<br></font>");
}
else if (currentline.contains("says \"") || currentline.contains("greets you.") || currentline.contains("bows deeply.")
             || currentline.contains("breaks into a wide grin.") || currentline.contains("You say \"") 
             || currentline.contains("nods affirmatively.") || currentline.contains("grin slyly"))
{
output.write(System.getProperty("line.separator") + "<font color=\"green\">" + currentline + "<br></font>");
}
else
{
output.write(System.getProperty("line.separator") + currentline + "<br>");
}
}
//finalize the HTML footer
output.write(System.getProperty("line.separator") + "</b>");
output.write(System.getProperty("line.separator") + "</div>");
output.write(System.getProperty("line.separator") + "</body>");
output.write(System.getProperty("line.separator") + "\t</html>");
output.close(); //file is finalized locally



//define variables for FTP process
String server = "servername";
int port = 21;
String user = "fake";
String pass = "password";

//begin FTP process to web server
FTPClient ftpClient = new FTPClient();
FileInputStream fis = null;
try {

    File localFile = new File("C:/Documents and Settings/Cuckoo/Desktop/Conversations.html");
    ftpClient.connect(server, port);
    ftpClient.login(user, pass);
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    fis = new FileInputStream(localFile);
    String remoteFile = "/public_html/Conversations.html";
    ftpClient.storeFile(remoteFile, fis);
    java.util.Date currentDate = new java.util.Date();
    Timestamp ftpTimestamp = new Timestamp(currentDate.getTime());
    //Create writer file to log iterations of the loop as successful or skipped.
    Writer writer = new BufferedWriter(new FileWriter("C:/Documents and Settings/Cuckoo/Desktop/Convo-Upload.log", true));
    writer.append("Successfully uploaded file as of " + ftpTimestamp.toString() + System.getProperty("line.separator"));
    writer.close(); //close the iteration logger
    ftpClient.logout();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (fis != null) {
            fis.close();
        }
        ftpClient.disconnect();
    } catch (IOException e) {
        e.printStackTrace();
        }
    }


lastLoopModified = currentLoopModified; //set the timestamp for the lastmodified on the file being read in
Thread.sleep(300000); //5 minutes
}
    else
    {
        java.util.Date currDate = new java.util.Date();
        Timestamp currTimestamp = new Timestamp(currDate.getTime());
        //Create writer file to log iterations of the loop as successful or skipped.
        Writer writer = new BufferedWriter(new FileWriter("C:/Documents and Settings/Cuckoo/Desktop/Convo-Upload.log", true));
        writer.append("Did not detect any new content in file.  Did not upload as of " + currTimestamp.toString() + System.getProperty("line.separator"));
        writer.close(); //close the iteration logger
    }
}

}
}
import java.io.*;
导入java.util.*;
导入org.apache.commons.net.ftp.ftp;
导入org.apache.commons.net.ftp.FTPClient;
导入java.sql.Timestamp;
公共类打印输出SFTP3
{
公共静态void main(字符串[]args)引发IOException、InterruptedException
{
布尔无穷远=真;
长currentLoopModified=0;
长lastLoopModified=0;
while(无穷远)
{
currentLoopModified=新文件(“C:/Documents and Settings/buckoo/Desktop/Syss convers.LOG”).lastModified();
如果(currentLoopModified>lastLoopModified)
{
//阅读对话日志
BufferedReader=新的BufferedReader(新的文件阅读器(“C:/Documents and Settings/Buckoo/Desktop/Syss convers.LOG”);
FileWriter output=newfilewriter(“C:/Documents and Settings/buckoo/Desktop/Conversations.html”);
列表行=新的ArrayList();
字符串行=null;
而((line=reader.readLine())!=null)
//从日志中删除一些不必要的混乱
{
if(!(line.contains(“刚刚挂断!!!”)| | line.contains(“刚刚离开领域”)
||包含(“快点,我有很多深奥的秘密要泄露,欢迎来到坏小子的王国。”)
||包含(“有关自定义MegaMud路径和其他信息,请访问网站:”)
||line.contains(“Syss gossips:讨论新想法/问题&在Facebook上查看最新信息!”)
||行。包含(“Syss gossips:http://www.facebook.com/groups/EsotericEdits/")
||包含(“Syss gossips:MME dat、Megamud路径文件和任务演练可在我的网站上找到”)
||包含(“Syss gossips:www.esoteric-edits.fhero.net”)
||行。包含(“心灵感应:@”)
||contains(“我是一个机器人。试着用@commands与我心灵感应。”)
||contains(“Syss gossips:记住,你可以通过telepath me@命令获得一些有用的东西,比如添加生命。”)
||包含(“Syss gossips:带个朋友来,帮助保持泥浆的活力!”)
||行。包含(“您说\”http://esoteric-edits.fhero.net/\"")
||contains(“对于可用命令的列表,您可以使用@commands与我心灵感应。”))
{
//把日期做成美国式
字符串日期=行子字符串(0,2);
字符串月份=行子字符串(3,5);
行。添加(月+“/”+日+行。子字符串(5));
}
}
//使用HTML头初始化输出文件
输出。写入(“”);
output.write(System.getProperty(“line.separator”)+“\t”);
output.write(System.getProperty(“line.separator”)+“\t\t”);
output.write(System.getProperty(“line.separator”)+“\t\tEsoteric编辑BBS-对话日志”);
output.write(System.getProperty(“line.separator”)+“\t”);
output.write(System.getProperty(“line.separator”)+“\t”+System.getProperty(“line.separator”)+System.getProperty(“line.separator”);
output.write(System.getProperty(“line.separator”)+“”);
output.write(System.getProperty(“line.separator”)+“\t\t”);
output.write(System.getProperty(“line.separator”)+“
    ”; output.write(System.getProperty(“line.separator”)+“\t
  • ”; output.write(System.getProperty(“line.separator”)+“\t
  • ”; output.write(System.getProperty(“line.separator”)+“\t
  • ”; output.write(System.getProperty(“line.separator”)+“\t
  • ”; output.write(System.getProperty(“line.separator”)+“\t
  • ”; output.write(System.getProperty(“line.separator”)+“\t
  • ”; output.write(System.getProperty(“line.separator”)+“
”; output.write(System.getProperty(“line.separator”)+“”); output.write(System.getProperty(“line.separator”)+“”); //用HTML颜色写出一个新文件 for(ListIterator iter=lines.ListIterator();iter.hasNext();) { 字符串currentline=iter.next(); output.write(“”;//将所有内容都加粗 if(currentline.contains(“连接路径:)) { output.write(System.getProperty(“line.separator”)+“”+currentline+“
”; } else if(currentline.contains(“gossips:)| | currentline.contains(“拍卖:)) { output.write(System.getProperty(“line.separator”)+“”+currentline+“
”; } else if(currentline.contains(“广播自”)) { output.write(System.getProperty(“line.separator”)+“”+currentline+“
”; } else if(currentline.contains(“says\”)| currentline.contains(“问候您”)| | currentline.contains(“深深鞠躬”) ||currentline.contains(“咧嘴笑”)| | currentline.contains(“你说\”) ||currentline.contains(“肯定地点头”)| | currentline.contains(“狡猾地咧嘴笑”)) { output.write(System.getProperty(“line.separator”)+“”+currentline+“
”; } 其他的 { output.write(System.getProperty(“line.separator”)+currentline+“
”; } } //完成HTML页脚 output.write(System.getProperty(“line.separator”)+“”); output.write(System.getProperty(“line.separator”)+“”); output.write(System.getProperty(“line.separator”)+“”); output.write(System.getProperty(“line.separator”)+“\t”); output.close();//文件在本地完成 //为FTP进程定义变量 String server=“servername”; int端口=21; 字符串user=“false”; 字符串pass=“password”; //开始到web服务器的FTP进程 FTPClient FTPClient=新的FTPClient(); FileInputStream fis=null; 试一试{ File localFile=新文件(“C:/Documents and Settings/buckoo/Desktop/Conversations.html”); ftpClient.connect(服务器、端口); ftpClient.login(用户,通过); ftpClient.setFileType