【工具】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;
     }*/
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值