javafx2导出文件选择路径

利用FileChooser


        Button expBtn = new Button ("导出");
        expBtn.setPrefSize(70, 20);
        expBtn.setCrudId("expBtn");
        expBtn.setOnAction(new EventHandler<ActionEvent>() {
	        @Override
		public void handle(ActionEvent arg0) {
		    //得到用户导出的文件路径
		    FileChooser fileChooser = new FileChooser();
	            FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("CSV files (*.csv)", "*.csv");
	            fileChooser.getExtensionFilters().add(extFilter);
	            Stage s = new Stage();
		    File file = fileChooser.showSaveDialog(s);
		    if(file==null) return;
		    String exportFilePath = file.getAbsolutePath().replaceAll(".csv", "")+".csv.";
                    //...此处省略客户端向服务器请求,并在服务器端直接下载到本地,建议用流方式,不易内存溢出。
                }
        }


文件导出

                int times = -1;
		int temp = 1;
		times = maxNum/EVERYEXPNUM;
		String executeSql = "";
		ExpObj expObj = new ExpObj();
		try{
			while(times>=0){
				//导出数据最大值,大于everyExpNUm里,则分段导出。
				if(maxNum>=(EVERYEXPNUM*temp)){
					executeSql = "SELECT *  FROM (select rownum myNum_,TempAA_.* FROM (" + querySql + ")TempAA_ ) where myNum_>="  
	                        + (temp-1)*EVERYEXPNUM + " and myNum_<=" + EVERYEXPNUM*temp;
					this.writeCSVFile(executeSql, queryId,expObj,exportFilePath,userMapInfoMap);
				}else if(maxNum%EVERYEXPNUM!=0){
					executeSql = "SELECT *  FROM (select rownum myNum_,TempAA_.* FROM (" + querySql + ")TempAA_ ) where myNum_>="  
		                    + (temp-1)*EVERYEXPNUM + " and myNum_<=" + maxNum;
					this.writeCSVFile(.....);//此处参数省略
				}
				try {
                 	            	expObj.getWriter().flush();//一定要每查一次都flush一次,要不然一次查出来的数据太过,会内存溢出
				} catch (IOException e) {
					e.printStackTrace();
				}
				times-=1;  
	            temp+=1; 
			}
		} catch (Exception ex) {
            ex.printStackTrace();
        }finally {
            try {
            	expObj.getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
        } 
写文件


private void writeCSVFile(.....){
   StringBuilder text = new StringBuilder();
   //.........//表头只写一次
   //表的内容写多次
   Writer writer = null;
File file = new File(filepath);
                writer = new BufferedWriter(new FileWriter(file));
//写文件的内容
            StringBuilder textemp = new StringBuilder();
text.append(textemp);
writer.write(text.toString());
}



   

你可能感兴趣的:(导出,Export,javafx2,FileChooser)