加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

File读写文件

发布时间:2020-12-16 02:55:19 所属栏目:百科 来源:网络整理
导读:1 写导出的话要用 window.location.href="地址"(多个参数用号分割)不能用ajax请求,因为ajax不能解析二进制的文件 如果window.location.href不生效的话在这句话的地下加window.event.returnValue=false;就好了 2 POI导出: HSSFWorkbook workbook = new HSS

1 写导出的话要用 window.location.href="地址"(多个参数用&号分割)不能用ajax请求,因为ajax不能解析二进制的文件

 如果window.location.href不生效的话在这句话的地下加window.event.returnValue=false;就好了

2 POI导出:

  HSSFWorkbook workbook = new HSSFWorkbook();//创建Excel文件(Workbook)
  HSSFSheet sheet = workbook.createSheet();//创建工作表(Sheet)
  String[] exportTitleArray = exportTitle.split(",");//得到标题行
  HSSFRow row = sheet.createRow(0);
  for (int i=0;i<exportTitleArray.length;i++){//创建列并赋值
    row.createCell(i).setCellValue(exportTitleArray[i]);
  }
  String [] sz=exportData.split("-");
  for(int i=1;i<=sz.length;i++){
    String[] split = sz[i-1].split(",");
    HSSFRow row1 = sheet.createRow(i);
    for (int j=0;j<split.length;j++){
      row1.createCell(j).setCellValue(split[j]);
    }
  }
  response.setContentType("application/ms-excel;charset=UTF-8");
  response.setHeader("Content-Disposition","attachment;filename=".concat(String.valueOf(URLEncoder.encode(title,"UTF-8"))));
  OutputStream out = response.getOutputStream();
  workbook.write(out);// 将数据写出去
  out.flush();
  out.close();

3 从数据库得图片
  Map<String,String> map = new HashMap<>();
  FastFileStorageClient storageClient = upLoadingService.getStorageClient();//得到数据库连接地址
  map.put("nationalityPicture",null);
  map.put("safetyPicture",null);
  map.put("seaFishingPicture",null);
  map.put("lefts",null);
  map.put("rightBoat",null);
  map.put("stern",null);
  //转换国籍证书图片
  String nationalityPicture = ship.getNationalityPicture();//国籍证书图片地址

  String npGroupName = nationalityPicture.substring(0,nationalityPicture.indexOf("|"));//得到营业执照名
  String npSubstring = nationalityPicture.substring(nationalityPicture.indexOf("|") + 1);//得到|的位置加一
  byte[] nbBytes = storageClient.downloadFile(npGroupName,npSubstring);
  BASE64Encoder npBase64Encoder = new BASE64Encoder();
  map.put("nationalityPicture",npBase64Encoder.encode(nbBytes));


4 用流读取文件一

  File file = new File("C:gfs190409.t00z.pgrb2f00");

  FileInputStream ins = new FileInputStream(file);

  int count = ins.available();

  byte[] bytes = new byte[count];

  ins.read(bytes);

  ins.close();

  System.out.println(file.getAbsoluteFile()+"-------"+file.getFreeSpace());

5 使用流读取文件二

  FileInputStream fis = new FileInputStream("C:Z_NAFP_C_BCSH_20190409000000_P_high-warms-f00.BIN");

  DataInputStream dis = new DataInputStream(fis);

  String s="d";

  byte[] bytes = new byte[dis.available()];

  int i=dis.read(bytes);

  String ss=new String(bytes,"UTF-8");

  System.out.println("s="+ss+"nlen="+bytes.length+"i"+i);

  fis.close();
  
  dis.close();

6 读取.nc文件(.nc.gz文件也可以)

  读取.nc.gz文件的时候会自动解压再读取

  jar包:netcdfAll-4.6.11.jar

  依赖:

  方法:
  System.out.println("开始");

  NetcdfFile openNC = NetcdfFile.open("C:hycom_glb_sfc_u_2019040900_t000.nc");

  List<Dimension> dimensions = openNC.getDimensions();//读取纬度信息(得到规模大小)

  List<Variable> variables = openNC.getVariables();//读取各个变量

  System.out.println("开始循环输出");

  for (Variable v:variables) {

    System.out.println(v);

  }

  System.out.println("结束");


7 使用file删除指定文件夹中的内容

  File file=new File("E:/test/test2/");

  File sz[] =file.listFiles();//得到所有的文件和目录

  for(File f:sz){

    f里存的是文件的路径和名+后缀
  
    f.delete();//删除对应的文件

  }

?

8 file删除指定文件夹并创建

  File file1 = new File("E:我创建1的");

  if(file1.exists()){     System.out.println("存在删除");     file1.delete();   }   File file = new File("E:我创建1的");   file.mkdir();//创建文件加

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读