【工具】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;
     }*/
}
 
 
原文地址:https://www.cnblogs.com/the-fool/p/11054213.html