日期:2014-05-18  浏览次数:20786 次

写入文件,求助!!!!
为什么文件生成了,但是什么内容也写不进去啊??
代码如下:
Java code

String spna=(String)session.getAttribute("spname");
String spfor=(String)session.getAttribute("spformat");
spna=spna+spfor;

String msg = "";
String local = request.getRequestURL().toString();
//out.println(local);
//out.flush();
//local="http://localhost:8090/myjsp/spbf.jsp"
String url0;

        url0 ="/myjsp/spwj/"+spna;
        url0 = url0.replaceAll(" ","%20");
        msg += "mkList(\"";
        msg += local.substring(0,local.substring(0,local.lastIndexOf("/")).lastIndexOf("/"))+url0;
        //out.println(msg);
        //out.flush();
        msg += "\",\"";
        msg += spna;        
        msg += "\");\n";
        //out.println(msg);
        //out.flush();
        
//下面开始生成播放列表的操作
String msghead="<asx version=\"3\">\n<title>JSPBOOK</title>\n<repeat>\n";
String msgtail="</repeat>\n</asx>";
msg=new StringBuffer(msghead).append(msg).append(msgtail).toString();
//out.print(msg);
//out.flush();
    String s=(String)session.getAttribute("loginuser");
    if (s==null);{
     s="";
    }
 String filename = request.getRealPath("mylist/ulist"+s+".asx" );  
 //out.print(filename);
 //out.flush(); 
java.io.File f = new java.io.File(filename);
if(!f.exists())//如果文件不存,则建立
{
  f.createNewFile();
}
try
{
  PrintWriter pw = new PrintWriter(new FileOutputStream(filename));
  pw.println(msg);//写内容
  pw.close();
}
catch(IOException e)
{
  out.println(e.getMessage());
}
%>



------解决方案--------------------
Java code

if(!f.exists())//如果文件不存,则建立
{
  f.mkdirs();
}
try
{
  PrintWriter pw = new PrintWriter(new FileOutputStream(filename));
  pw.println(msg);//写内容
  pw.flush();
  pw.close();
}

------解决方案--------------------
FileWriter writer = new FileWriter(file , true);
writer.write("----------------------------");
writer.flush();
writer.close();
------解决方案--------------------
有注释的,可以参考下。。。
http://hi.baidu.com/snowgod/blog/item/919d797e0aa81e0028388ab8.html