各位老大帮帮忙啊:
小菜最近接到一个任务,从数据库中select一些数据,然后写入指定的文件。文件要求是每5分钟自动生成一个,文件名格式如:XXXX.20051105.0001其中:XXXX是常量,20051105是日期,最后的序列号在0001到9999之间循环,为此,我定义了个方法用来生成文件
public void createFile(int fileType,int j)
{
String s1,s2,s3,s4;
s3=getDate()+".";
s1="WXSMG.";
if (fileType==1) //fileType=1,为SMT,否则为SMO
{
s2="SMT.";
}
else
{
s2="SMO.";
}
DecimalFormat df = new DecimalFormat("0000");
s4 = df.format(j);
String s=s1+s2+s3+s4;
try
{
tofile=new FileWriter(".\\backup\\"+s+".txt");
out=new BufferedWriter(tofile);
}
catch(IOException e)
{
System.out.println("write to file fail!");
System.out.println("The error is:"+e);
}
}
然后又在另一个方法中使用(那个操作数据库的方法)
public String getDate()
{
String fileDate;
java.util.Date nowDate=new java.util.Date();
SimpleDateFormat matter=new SimpleDateFormat("yyyyMMdd");
fileDate=matter.format(nowDate);
return fileDate;
}
public long getTime()
{
return System.currentTimeMillis();
}public void run()
{
Statement st=null;
ResultSet rs1=null;
PreparedStatement pstat1=null;
String result1;
starttime=getTime();
startdate=getDate();
int i=1;
createFile(0,i);
createFile(1,i);
while(true)
{
try
{
//取数据
st=smgwpool.createStatement();
rs1=st.executeQuery("select * from smpp_submit");
while(rs1.next())
{
int id=rs1.getInt("ID");
String MessageID=rs1.getString("MSG_ID");
int MsgType=rs1.getInt("MSGTYPE");
result1="MessageID="+MessageID+" "+"MsgType="+MsgType;
//write File
try
{
out.write(result1,0,result1.length());
out.newLine();
}
catch(IOException e)
{
System.out.println("out.write to file fail!");
System.out.println("The error is:"+e);
}
}
//文件关闭
try
{
out.close();
}
catch(IOException e)
{
System.out.println("close file fail!");
System.out.println("The error is:"+e);
}
try
{
Thread.sleep(30000);
}
catch(InterruptedException e) {}
//判断是否要重新生成文件,为tofile做准备
endtime=getTime();
if ((endtime-starttime)>=60000)
{
enddate=getDate();
if(enddate.equalsIgnoreCase(startdate))
{
i=i+1;
}
else
{
i=1;
startdate=enddate;
}
createFile(0,i);
createFile(1,i);
starttime=endtime;
}
}
catch(Exception e){}
finally
{
try
{
if(st!=null)st.close();
if(rs1!=null)rs1.close();
}
catch(SQLException se){}
}
}
}
最后只生成了两个文件,却是空的(估计是在while(true)前初始化时生成的),数据没有写入,各位老大能不能帮我看看,到底哪里有问题?小菜自己觉得可能是createFile(int,int)里面的那个"out"没有出来?还请各位指点,小菜感激不禁!!!
小菜最近接到一个任务,从数据库中select一些数据,然后写入指定的文件。文件要求是每5分钟自动生成一个,文件名格式如:XXXX.20051105.0001其中:XXXX是常量,20051105是日期,最后的序列号在0001到9999之间循环,为此,我定义了个方法用来生成文件
public void createFile(int fileType,int j)
{
String s1,s2,s3,s4;
s3=getDate()+".";
s1="WXSMG.";
if (fileType==1) //fileType=1,为SMT,否则为SMO
{
s2="SMT.";
}
else
{
s2="SMO.";
}
DecimalFormat df = new DecimalFormat("0000");
s4 = df.format(j);
String s=s1+s2+s3+s4;
try
{
tofile=new FileWriter(".\\backup\\"+s+".txt");
out=new BufferedWriter(tofile);
}
catch(IOException e)
{
System.out.println("write to file fail!");
System.out.println("The error is:"+e);
}
}
然后又在另一个方法中使用(那个操作数据库的方法)
public String getDate()
{
String fileDate;
java.util.Date nowDate=new java.util.Date();
SimpleDateFormat matter=new SimpleDateFormat("yyyyMMdd");
fileDate=matter.format(nowDate);
return fileDate;
}
public long getTime()
{
return System.currentTimeMillis();
}public void run()
{
Statement st=null;
ResultSet rs1=null;
PreparedStatement pstat1=null;
String result1;
starttime=getTime();
startdate=getDate();
int i=1;
createFile(0,i);
createFile(1,i);
while(true)
{
try
{
//取数据
st=smgwpool.createStatement();
rs1=st.executeQuery("select * from smpp_submit");
while(rs1.next())
{
int id=rs1.getInt("ID");
String MessageID=rs1.getString("MSG_ID");
int MsgType=rs1.getInt("MSGTYPE");
result1="MessageID="+MessageID+" "+"MsgType="+MsgType;
//write File
try
{
out.write(result1,0,result1.length());
out.newLine();
}
catch(IOException e)
{
System.out.println("out.write to file fail!");
System.out.println("The error is:"+e);
}
}
//文件关闭
try
{
out.close();
}
catch(IOException e)
{
System.out.println("close file fail!");
System.out.println("The error is:"+e);
}
try
{
Thread.sleep(30000);
}
catch(InterruptedException e) {}
//判断是否要重新生成文件,为tofile做准备
endtime=getTime();
if ((endtime-starttime)>=60000)
{
enddate=getDate();
if(enddate.equalsIgnoreCase(startdate))
{
i=i+1;
}
else
{
i=1;
startdate=enddate;
}
createFile(0,i);
createFile(1,i);
starttime=endtime;
}
}
catch(Exception e){}
finally
{
try
{
if(st!=null)st.close();
if(rs1!=null)rs1.close();
}
catch(SQLException se){}
}
}
}
最后只生成了两个文件,却是空的(估计是在while(true)前初始化时生成的),数据没有写入,各位老大能不能帮我看看,到底哪里有问题?小菜自己觉得可能是createFile(int,int)里面的那个"out"没有出来?还请各位指点,小菜感激不禁!!!
这两句连续执行,tofile和out被赋了两次值
tofile=new FileWriter(".\\backup\\"+s+".txt");
out=new BufferedWriter(tofile);
怎么能够不出错呢!?去掉其中一个createFile的调用试试吧!还有,不知道你生成两个不同名但相同内容的文件有什么目的
unreachable statement
System.out.println("end of while");
^
是不是我程序里的异常处理里面有问题啊?还望高手们指教!!!
小菜不甚感激!
如果有异常,io没有关闭的话会导致写入失败,因为你用的是BufferedWriter
catch(Exception e)
{
System.out.println("big fail!");
System.out.println("The error is:"+e);
}
这样编译没有错,运行时循环报错:
big fail!
The error is:java.lang.NullPointerException
估计是那个最大的try里有错误吧?望指教,万分感激!
在他下面设个断点看看它是否为null
程序直接输出它也可以
System.out.println("before st!");
st=smgwpool.createStatement();
System.out.println("st success!");
结果,上面的那条可以输出,下面的就出不来了
运行时循环输出:
before st!
big fail!
The error is:java.lang.NullPointerException
这样是不是说明st的值还是空的???
应该就是st=smgwpool.createStatement();的问题了
请问在java中如何才能将select后得到的结果集高效地写入文件,我现在是用rs.getXXX取一条记录里的相关属性值,在将从一条记录里选出来的属性值赋值给一个String result,然后就将result写入文件(用的Bufferedwrite)。本来打算定义一个String类的数组,将结果集放入,一次性写入文件。但又不知是否可行,如何操作。望指教!