• 【工具】java 文本文档txt写出记录工具


    彩蛋!http://abowman.com/google-modules/dog/

    以下是自己小游戏生成人物经历的传记时保存txt所用到的工具类,功能简单,不多说什么,贴上代码:

    package com.yy.diabio.v1.utils;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    
    /**
     * 传记
     * @author Administrator
     *
     */
    public class Biography {
    	/**
    	 * 读一行写一行
    	 * @param path
    	 */
    	public static void readLineAndWrite(String path,String pathout){
    		try {
    			File file = new File(path);
    			String expath= path.replace(".", "_re.");
    			expath=expath.replaceAll("fileImp", "fileExp");
    			System.out.println(expath);
    			//int index =path.lastIndexOf(".");
    			//File expFile = new File("D:/scc0511_others_r.net");
    			File expFile = new File(expath);
    			BufferedReader br = new BufferedReader(new FileReader(file));
    			 
                 PrintWriter out = new PrintWriter(new BufferedWriter(
                         new FileWriter(expFile, true)));
                 String line   = null;
                 while ((line = br.readLine()) != null) {
                     out.println(line);      
                 }
                 if (br != null) {
                     br.close();
                 }
                 if (out != null) {
                     out.close();
                 }
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    	
    	/**
    	 * 书写append,新建时会刷新或者创建新的
    	 * @param path
    	 * @param pathout
    	 */
    	public static void writeBioGraPhy(String msg){
    		String path=getPath();
    		File file = new File(path);
    		if(!file.exists()){
    			try {
    				file.createNewFile();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    		PrintWriter out =null;
    		 try {
    			out = new PrintWriter(new BufferedWriter(
    			         new FileWriter(file, true)));
    			out.println(msg);  
    		} catch (IOException e) {
    			e.printStackTrace();
    		}finally{
    			out.flush();
    		}
    		
    		
    		
    	}
    	
    	/**
    	 * 测试路径
    	 * @return
    	 */
    	 public static String getPath() {  
             //构造时获取到项目的物理根目录  
             //String project_root = this.getClass().getResource("/").toString().replace("file:/", "");  
             String project_root=ReadPropertiesUtil.class.getResource("/").toString().replace("file:/", ""); 
             //web pro
             //project_root = project_root.substring(0,project_root.indexOf("/WEB-INF"));  
             project_root = project_root.substring(0,project_root.indexOf("/bin")); 
     		 String path=project_root+"/src"+"/Biography.txt";
     		 //path=project_root+"/Role.txt";
             return path;
         }
    	 /**
    	  * bat启动文件使用路径
    	  */
    	/* public static String getPath() {  
             //构造时获取到项目的物理根目录  
             //String project_root = this.getClass().getResource("/").toString().replace("file:/", "");  
             String project_root=ReadPropertiesUtil.class.getResource("/").toString().replace("file:/", ""); 
             //web pro
             //project_root = project_root.substring(0,project_root.indexOf("/WEB-INF"));  
             //project_root = project_root.substring(0,project_root.indexOf("/bin")); 
     		 String path=project_root+"/src"+"/Biography.txt";
     		 //path=project_root+"/Role.txt";
             return path;
         }*/
    }
     
     
  • 相关阅读:
    分享一个网上搜不到的「Redis」实现「聊天回合制」的方案
    【转】Python函数装饰器原理与用法详解
    java后台过滤vue前台传的空字符串
    uniapp项目 引用uview与配置
    微信 JSSDK 配置wx.config
    MySQL 的 JSON 格式字段用法
    MySQL 的 limit 深分页问题
    MyBatis 的 #{} 和 ${} 的使用场景
    SpringCloudAlibaba学习(二):Nacos注册中心
    Jackson转换时间字符串为LocalDateTime类型报错解决
  • 原文地址:https://www.cnblogs.com/the-fool/p/11054213.html
Copyright © 2020-2023  润新知