File读写文件
1.file方法:
1.创建文件夹:file.mkdir()
2.创建文件:file.createNewFile()
3.删除文件:file.delete()
4.判断文件是否存在:file.exists()
5.判断是否是文件:file.isFile()
2.用流读取文件一
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());
3.使用流读取文件二
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();
4.读取.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("结束");
5.使用file删除指定文件夹中的内容
File file=new File("E:/test/test2/"); File sz[] =file.listFiles();//得到所有的文件和目录 for(File f:sz){ f里存的是文件的路径和名+后缀 f.delete();//删除对应的文件 }
6.file删除指定文件夹
File file1 = new File("E:\\我创建1的"); if(file1.exists()){ System.out.println("存在删除"); file1.delete(); } File file = new File("E:\\我创建1的"); file.mkdir();//创建文件夹
7.file实现上传文件
public static String uploadingFile(MultipartFile multipartFile, String url){ String newFileName = null; try { //获得文件名加后缀 String fileName = multipartFile.getOriginalFilename(); //得到后缀 String suffix = fileName.substring(fileName.lastIndexOf(".")); //创建新的名字 newFileName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date())+suffix; //调用转移方法保存到磁盘上 multipartFile.transferTo(new File(url + newFileName)); } catch (IOException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } return newFileName; }
8.file实现下载文件
public void downloadFileByUrl(String filePath, HttpServletResponse response) { //得到下载的文件名 String doFileName = filePath.substring(filePath.lastIndexOf("/") + 1); File file = new File(filePath); byte[] buffer = new byte[1024]; BufferedInputStream bis = null; OutputStream os = null; //输出流 try { //判断文件父目录是否存在 if (file.exists()) { //设置返回文件信息 response.setContentType("application/vnd.ms-excel;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Disposition", "attachment;fileName=" + java.net.URLEncoder.encode(doFileName,"UTF-8")); os = response.getOutputStream(); bis = new BufferedInputStream(new FileInputStream(file)); while(bis.read(buffer) != -1){ os.write(buffer); } } } catch (Exception e) { e.printStackTrace(); } finally { try { if(bis != null) { bis.close(); } if(os != null) { os.flush(); os.close(); } } catch (IOException e) { e.printStackTrace(); } } }
9.根据文件size获取文件大小
public static String getPrintSizeByFileSize(long size) { // 如果字节数少于1024,则直接以B为单位,否则先除于1024 double value = (double) size; if (value < 1024) { return String.valueOf(value) + "B"; } else { value = new BigDecimal(value / 1024).setScale(2,BigDecimal.ROUND_DOWN).doubleValue(); } // 如果原字节数除于1024之后,少于1024,则可以直接以KB作为单位 // 因为还没有到达要使用另一个单位的时候 // 接下去以此类推 if (value < 1024) { return String.valueOf(value) + "KB"; } else { value = new BigDecimal(value / 1024).setScale(2,BigDecimal.ROUND_DOWN).doubleValue(); } if (value < 1024) { return String.valueOf(value) + "MB"; } else { // 否则如果要以GB为单位的,先除于1024再作同样的处理 value = new BigDecimal(value / 1024).setScale(2, BigDecimal.ROUND_DOWN).doubleValue(); return String.valueOf(value) + "GB"; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构