io字節流寫入文件 -- study20180421

String word="高考是人生的分水嶺";


String uploadPath = request.getSession().getServletContext().getRealPath(File.separator+"upload");
//項目中建一個文件夾“upload”
FileOutputStream fop = null;
File file;
try {
file= new File(uploadPath,"docbook.xml");
if (!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
if (!file.exists()) {
file.createNewFile();
}
fop = new FileOutputStream(word.getBytes());
fop.write(file.getBytes());
fop.flush();
fop.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fop != null) {
fop.close();
}
} catch (IOException e) {
e.printStackTrace();
}

}


//刪除臨時文件
if (uploadPath != null) {
    deleteDirectory(uploadPath);
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章