zoukankan      html  css  js  c++  java
  • Java文件读写

    文件写入:

            // 写入数据保存在本地D:/user/error/目录下
    	private void write() {
                   //获取系统毫秒数
                   long currentTimeMillis = System.currentTimeMillis();
    		String ts = String.valueOf(currentTimeMillis);
    		// 文件名称系统毫秒数
    		String filepath = "D:/user/error/" + String.valueOf(currentTimeMillis) + ".txt";
    		File file = new File(filepath);
    		//判断文件是否存在,不存在则创建
    		if (!file.exists()) {
    			file.getParentFile().mkdirs();
    		}
    		OutputStreamWriter opw;
    		try {
    			opw = new OutputStreamWriter(new FileOutputStream(filepath), "GBK");
    			BufferedWriter bufw = new BufferedWriter(opw);
    			bufw.write("写入数据123456");
    			bufw.close();
    			opw.close();
    		} catch (Exception e) {
    			
    			e.printStackTrace();
    		}
    
    	}
    

      文件读取:

    public  void read() {
    
    		String filepath = "D:/user/error/";
    		File file = new File(filepath);
                    //获取D:/user/error/目录下所有文件
    		File[] files = file.listFiles();
    		Arrays.sort(files);
    		String read = "";
    		try {
    			for (int i = 0; i < files.length; i++) {
    				if (files[i].isFile()) {
    					InputStreamReader opw = new InputStreamReader(new FileInputStream(files[i]), "GBK");
    					BufferedReader bufr = new BufferedReader(opw);
    					read = bufr.readLine();
    					bufr.close();
    					System.out.println(read);
                                            //删除当前文件
    					files[i].delete();
    				}
    
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}                
    

      

  • 相关阅读:
    vue 自定义指令
    vue 插槽
    vue 菜单跳转 页面错乱
    vue项目中使用elementUI的el-tabs组件 浏览器卡死问题 解决办法
    vue 环境配置
    移动端页面 问题 注意事项
    定义全局 强制刷新指令
    手机端样式 处理
    手机访问电脑本地开发的页面
    百度AI
  • 原文地址:https://www.cnblogs.com/staticking/p/9044204.html
Copyright © 2011-2022 走看看