MyException - 我的异常网
当前位置:我的异常网» Java Web开发 » 多线程的有关问题,请帮帮我

多线程的有关问题,请帮帮我

www.MyException.Cn  网友分享于:2013-11-09  浏览:771次
多线程的问题,请帮帮我
import   java.io.BufferedInputStream;
import   java.io.BufferedReader;
import   java.io.File;
import   java.io.FileOutputStream;
import   java.io.FileReader;
import   java.io.IOException;
import   java.io.OutputStream;
import   java.net.URL;
import   java.net.URLConnection;
import   java.util.ArrayList;
public   class   Alexa   implements   Runnable   {

public   ArrayList   s   =   new   ArrayList();

public   static   void   main(String[]   args)   throws   IOException   {
Alexa   alexa   =   new   Alexa();
alexa.readFile( "c:\\sss.txt ");
Thread   t   =   new   Thread(alexa);
t.start();
}
public   void   run()   {

String   a   =   " ";
for   (int   i   =   0;   i   <   s.size();   i++)   {
a   =   (String)   s.get(i);
try   {
saveBinaryFile(a);
System.out.println( "i= "   +   i);
System.out.println( "a= "   +   a);
}   catch   (IOException   e)   {
//   TODO   Auto-generated   catch   block
e.printStackTrace();
}
}
}

public   void   saveBinaryFile(String   url1)   throws   IOException   {

BufferedInputStream   in   =   null;
OutputStream   out   =   null;
try   {

URL   url   =   new   URL(url1);
URLConnection   conn   =   url.openConnection();

in   =   new   BufferedInputStream(conn.getInputStream());

out   =   new   FileOutputStream(new   File( "c://tmp// "
+   System.currentTimeMillis()));
int   n   =   0;
byte[]   bytes   =   new   byte[1024];

while   ((n   =   in.read(bytes))   !=   -1)   {
out.write(bytes,   0,   n);
}
}   catch   (Exception   e)   {
}   finally   {
try   {
in.close();
out.close();
}   catch   (Exception   ioe)   {
ioe.getStackTrace();
}
}

}

public   void   readFile(String   path)   {
String   fileline;
ArrayList   domains   =   new   ArrayList();
try   {
BufferedReader   d   =   new   BufferedReader(new   FileReader(path));

while   ((fileline   =   d.readLine())   !=   null)   {
domains.add(fileline);
System.out.println(fileline);
}

}   catch   (Exception   e)   {
System.out.println( "readFile.Error:   "   +   e.getMessage()   +   "   "
+   e.getClass());
e.printStackTrace();
}   finally   {
try   {

}   catch   (Exception   e)   {

}
}
s   =   domains;
}

}
c:\\sss.txt   放了一些htm的连接,我想将这些页面下载下来,想用多线程,但是其中saveBinaryFile(a);不执行,但是
System.out.println( "i= "   +   i);
System.out.println( "a= "   +   a);却打印出来了,请问怎么回事?请指教,谢谢


文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有