//文件创建
File FileDir=new File(fileNameAndpath);
try{
if(!FileDir.exists()){
FileDir.createNewFile();
//向文件中传入信息
WriterFileThread writerIn=new WriterFileThread(fileNameAndpath,fileContent,contentName);
Thread t=new Thread(writerIn);
t.start();
t.join();
bool=t.isAlive();
while(bool){
Thread.sleep(500);
}
}
}
catch(Exception e){
e.printStackTrace();
}
--------------------------------------------------------------------------
//文件写入信息
StringBuilder stringHtml = new StringBuilder();
OutputStreamWriter write;
try {
write = new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8");
BufferedWriter writer = new BufferedWriter(write);
stringHtml.append("helloword”);
writer.write(stringHtml.toString());
writer.flush();
writer.close();
System.out.println("ending");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
System.out.println("ending1");
}
catch(Exception e){
e.printStackTrace();
System.out.println(""+e);
}finally{
System.out.println("end");
}
System.out.println("ending2");
}
http://blog.sina.com.cn/s/blog_4922d8a50102vfx8.html
希望可以帮到你
把报错的地方贴出来 首次访问报错控制台信息贴出来
其实,我这问题就是,在项目中创建文件,创建的文件在代码运行结束后,并没有第一时间存放到项目路径下。所以首次调用才没有找到文件。有没有解决办法。
让代码运行结束这个文件马上就在路径下生成。
这个应该是一个延迟反应,你的文件应该是已经创建或者正在创建中,但是这个还是需要一个反应时间,看你具体需要,可以设置一个延时加载,也可以设置一个中转页面提示跳转