java创建文件到本地

来源:互联网 发布:php mysql apache 编辑:程序博客网 时间:2024/06/05 21:11

“# 创建文件保存本地和删除以及读取文件简单实例

简单的demo实例:

public class Test {    public static void main(String[] args) throws IOException, WriteException{        setMessage();    }//创建    public static void setMessage() throws IOException, WriteException{                 WritableWorkbook book= Workbook.createWorkbook(new File("D://test.xls"));                //创建exel(第一页)                 WritableSheet sheet=book.createSheet("表格数据",0);                 try {                    //在这里第一行代表列,第二行代表行                    sheet.addCell(new Label(0,0,"领导"));                    sheet.addCell(new Label(1,0,"员工"));                    sheet.addCell(new Label(0,1,"0"));                    sheet.addCell(new Label(1,1,"2"));                } catch (WriteException e) {                    e.printStackTrace();                }                 //把创建的内容写入到输出流中,并关闭输出流                book.write();                book.close();    }

//读取文件在页面展示
public List<Map<String, Object>> getPathFileData(String dataaTime) throws BiffException, FileNotFoundException, IOException, ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //返回页面的
list List<Map<String, Object>> list = new ArrayList<>();
//读取文件
Workbook workbook = Workbook.getWorkbook(new FileInputStream("D://"+dataaTime+".xls"));
Sheet rs = workbook.getSheet(0);
//所有行数
int rows = rs.getRows();
for(int i=1;i<rows;i++){
Map<String, Object> map = new HashMap<String, Object>(); map.put("leaderName", rs.getCell(0, i).getContents()); map.put("staffName", rs.getCell(1, i).getContents());
list.add(map);
}
return list;
}
//删除文件
public void deleteFile(){
//删除本地文件
File file = new File("D://"+dataId+datas.get(0).getDataTime()+".xls");
// 判断文件是否存在
if (file.exists()) {
// 存在执行删除 file.delete();
}
}

“`

原创粉丝点击