1. 程式人生 > >io位元組流寫入檔案 -- study20180421

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);
}