public class SearchThread {
/*
private ThreadGroup tG=new ThreadGroup("SearchThread");
Thread sub[];
SubThread st=new SubThread();

public SearchThread()
{
st.init();
/*
for(int i=0;i<10;i++)
{
sub[i]=new Thread(tG,st,"thread"+(i+1));
sub[i].start();
}

//st.run();
}
*/
BufferedWriter bw=null;
static File f[]=new File[6];
public static void main(String args[])
{
try{
for(int i=0;i<=5;i++)
{
f[i]=new File(""+i+".txt");
//if(!f[i].exists())
// f[i].createNewFile();
}
new SearchThread();

}
catch(Exception e)
{
e.printStackTrace();
}
}//class SubThread implements Runnable
public SearchThread()
{
//static int i=0;
BufferedReader dict;
// LineNumberReader lNumber;
// Random ran=null;
String tmp="";
long sentenceNo=0,sentenceLen=0,nextsentenceOff=0; String DocNo;

///public void init()
//{
System.out.println("==");
try{


dict=new BufferedReader(new FileReader(new File("D:\\code\\IR863\\050404url.no\\050722url.no")));
// lNumber=new LineNumberReader(dict);
// ran=new Random();
while((tmp=dict.readLine())!=null)
{
StringTokenizer st=new StringTokenizer(tmp,"  ");
String url=st.nextToken();
if(!st.hasMoreTokens()) {System.out.println("Have no DocNo!");DocNo="";}
DocNo=st.nextToken();
DocNo=DocNo.substring(DocNo.indexOf("-")+1,DocNo.length());
sentenceNo++;
sentenceLen=tmp.length();
//
bw=new BufferedWriter(new FileWriter(f[(int)(Long.parseLong(DocNo)/1000000)],true));
bw.write(DocNo+" "+nextsentenceOff+" "+sentenceLen+System.getProperty("line.separator"));
//if(sentenceNo==100000) 
bw.flush();
//记录每个句子的号码,长度,偏移量
//if(Long.parseLong(DocNo)/1000000)
//System.out.println("sentenceNo="+sentenceNo+" sentenceLen="+sentenceLen+" nextsentenceOff="+nextsentenceOff);
nextsentenceOff+=sentenceLen+1;
}
//System.out.println(""+lNumber.getLineNumber());
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("File Read error!");
}
}
运行时总时出现下面的问题:
java.io.FileNotFoundException: 0.txt (另一个程序正在使用此文件,进程无法访问。)
File Read error!
at java.io.FileOutputStream.openAppend(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:174)
at java.io.FileWriter.<init>(FileWriter.java:90)
at SearchThread.<init>(SearchThread.java:74)
at SearchThread.main(SearchThread.java:34)

解决方案 »

  1.   

    public boolean saveFile(String filePath,String fileName,String content)
        {   boolean bSuccess=true;
            try
            {
                File fFilePath=new File(filePath);
                if(!fFilePath.exists()) fFilePath.mkdirs();
                File fSave=new File(filePath+fileName);            DataOutputStream dos=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(fSave)));
                dos.writeBytes(content);
                dos.close();
            } catch(Exception e)
            {   System.out.println(e.getMessage());
            bSuccess=false;
        }
            return bSuccess;
        }
      

  2.   

    你应该是调用api输入的参数不对