文件创建读取(自用勿点)

小时候控制台游戏所用的工具类,有毒哈哈哈哈:

1、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;
     }
	 /**
	  * 启动文件使用路径
	  */
	/* 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;
     }*/
}

2、存档对象记录工具类:

package com.yy.diabio.v1.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.yy.diabio.v1.role.Role;

public class ObjStreamUtil {
	//1.创建目标路径
   
 
    
     
	     //(一)先写入对象
    public static void saveRole(Role role){
        File file = null;
     	String path = getPath();
     	file = new File(path);
     	if(!file.exists()){
     		try {
 				file.createNewFile();
 			} catch (IOException e) {
 				e.printStackTrace();
 			}
     	}
        //2.创建流通道
        FileOutputStream fos=null;
      //3.创建对象输出流
        ObjectOutputStream objOP=null;
		try {
			System.out.println(file);
			fos = new FileOutputStream(file);
			objOP = new ObjectOutputStream(fos);
			objOP.writeObject(role);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(objOP!=null){
				try {
					objOP.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
		}
     }
     
     //再读取对象
     public static Role readRole(){
    	File file = null;
      	String path = getPath();
      	file = new File(path);
      	if(!file.exists()){
      		try {
  				file.createNewFile();
  			} catch (IOException e) {
  				e.printStackTrace();
  			}
      	}
    	 
         FileInputStream fis=null;
         ObjectInputStream objIP=null;
         Role role =null;
		try {
			System.out.println(file+"?????????");
			fis = new FileInputStream(file);
			objIP = new ObjectInputStream(fis);
			role = (Role)objIP.readObject();
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		} catch (ClassNotFoundException e) {
		}catch (Exception e) {
		}finally{
			if(objIP!=null){
				try {
					objIP.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
         return role;
     }
     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"+"/Role.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"+"/Role.txt";
 		 //path=project_root+"/Role.txt";
         return path;
     }*/
}

3、properties记录类:

package com.yy.diabio.v1.utils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

public class propertiesBath {
	//初始化properties

	public static void main(String[] args) {
		Properties pro = new Properties();
		try {
		    InputStream inStr = ClassLoader.getSystemResourceAsStream("Role.properties");
		    pro.load(inStr);
		} catch (FileNotFoundException e) {
		    e.printStackTrace();
		} catch (IOException e) {
		    e.printStackTrace();
		}


		//返回所有的属性名,即键名。
		//propertyNames(),返回属性列表中所有键的枚举
		Enumeration enu2=pro.propertyNames();
		while(enu2.hasMoreElements()){
		    String key = (String)enu2.nextElement();
		    System.out.println(key);
		} 


		//返回所有的属性值。
		//Properties 继承于 Hashtable,elements()是Hashtable的方法,返回哈希表中的值的枚举。
		Enumeration enu=pro.elements();
		while(enu.hasMoreElements()){
		    String key = (String)enu.nextElement();
		    System.out.println(key);

		} 


		//返回所有的属性(属性名,属性值)。
		//Properties 继承于 Hashtable,entrySet()是Hashtable的方法,
		//返回此 Hashtable 中所包含的键的 Set 视图。此 collection 中每个元素都是一个 Map.Entry
		Iterator it=pro.entrySet().iterator();
		while(it.hasNext()){
		    Map.Entry entry=(Map.Entry)it.next();
		    Object key = entry.getKey();
		    Object value = entry.getValue();
		    System.out.println(key +":"+value);
		}
	}
}

02:

package com.yy.diabio.v1.utils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

/**
 * 读取properties文件
 * 
 * @author ZX
 */
public class ReadPropertiesUtil {

	// 测试方法,先运行看使用情况
	

	/*
	 * 两种方式 InputStream inStream =
	 * this.getClass().getResourceAsStream("/pro.properties");//普通类好用
	 * InputStream inStream =
	 * 类名.class.getResourceAsStream("/pro.properties");//静态类好用
	 */
	/**
	 * 根据key取得类路径下properFilePath文件中的value值
	 * 
	 * @param properFilePath
	 *            pro文件的path
	 * @param key
	 * @return
	 */
	public static String getProKey(String properFilePath, String key) {
		String value = "";
		InputStream inStream = null;
		try {
			inStream = ReadPropertiesUtil.class
					.getResourceAsStream(properFilePath);
			Properties prop = new Properties();
			prop.load(inStream);
			value = prop.getProperty(key);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (inStream != null) {
				try {
					inStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		// Object [] fmtargs = {""};//替换string中的{0}
		// String content =MessageFormat.format (registerMessage, fmtargs);
		return value;
	}

	
	private static final int SUCCESS =1;
	private static final int FAIL =1;
	public static int saveProKey(String properFilePath, String key, String value) {
		
		String path = getPath();
		path=path+"/src"+properFilePath;
		
		File file = new File(path);
		if(!file.exists()){
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		Properties props = new Properties();
		OutputStream fos=null;
	    try {
	        props.load(new FileInputStream(path));
	        
	        fos = new FileOutputStream(path);
            props.setProperty(key, value);
            props.store(fos, key);
	    } catch (Exception e) {
	    	 e.printStackTrace();
	    	return FAIL;
	    }finally{
	    	if(fos!=null){
	    		try {
					fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	    	}
	    }
		return SUCCESS;
	}
	/**
	 * 添加可变参数替换properties中的{0}
	 */
	
	
	public static String getRoleKey(String key) {
		String properFilePath="/Role.properties";
		String proKey = getProKey(properFilePath, key);
		return proKey;
	}
	public static int saveRoleKey(String key,String value) {
		String properFilePath="/Role.properties";
		int saveProKey = saveProKey(properFilePath, key, value);
		return saveProKey;
	}
	
	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"));  
        return project_root;
    }
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	//================================from network=======================================
	
	    /**
	    * 采用静态方法
	    */
	    private static Properties props = new Properties();

	    /**
	    * 读取属性文件中相应键的值
	    * @param key
	    *            主键
	    * @return String
	    */
	    public static String getKeyValue(String key) {
	        return props.getProperty(key);
	    }

	    /**
	    * 根据主键key读取主键的值value
	    * @param filePath 属性文件路径
	    * @param key 键名
	    */
	    public static String readValue(String filePath, String key) {
	        Properties props = new Properties();
	        try {
	            InputStream in = new BufferedInputStream(new FileInputStream(
	                    filePath));
	            props.load(in);
	            String value = props.getProperty(key);
	            System.out.println(key +"键的值是:"+ value);
	            return value;
	        } catch (Exception e) {
	            e.printStackTrace();
	            return null;
	        }
	    }
	  
	    /**
	    * 更新(或插入)一对properties信息(主键及其键值)
	    * 如果该主键已经存在,更新该主键的值;
	    * 如果该主键不存在,则插件一对键值。
	    * @param keyname 键名
	    * @param keyvalue 键值
	    */
	    public static void writeProperties(String keyname,String keyvalue) {      
	        try {
	            // 调用 Hashtable 的方法 put,使用 getProperty 方法提供并行性。
	            // 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
	            OutputStream fos = new FileOutputStream("profilepath");
	            props.setProperty(keyname, keyvalue);
	            // 以适合使用 load 方法加载到 Properties 表中的格式,
	            // 将此 Properties 表中的属性列表(键和元素对)写入输出流
	            props.store(fos, "Update '" + keyname + "' value");
	        } catch (IOException e) {
	            System.err.println("属性文件更新错误");
	        }
	    }

	    /**
	    * 更新properties文件的键值对
	    * 如果该主键已经存在,更新该主键的值;
	    * 如果该主键不存在,则插件一对键值。
	    * @param keyname 键名
	    * @param keyvalue 键值
	    */
	    public void updateProperties(String keyname,String keyvalue) {
	        try {
	            props.load(new FileInputStream("profilepath"));
	            // 调用 Hashtable 的方法 put,使用 getProperty 方法提供并行性。
	            // 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
	            OutputStream fos = new FileOutputStream("profilepath");          
	            props.setProperty(keyname, keyvalue);
	            // 以适合使用 load 方法加载到 Properties 表中的格式,
	            // 将此 Properties 表中的属性列表(键和元素对)写入输出流
	            props.store(fos, "Update '" + keyname + "' value");
	        } catch (IOException e) {
	            System.err.println("属性文件更新错误");
	        }
	    }
	    
	     
}

 

 

 

 

4、名字随机类:

package com.yy.diabio.v1.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RandomUtils {
	public static void main(String[] args) {
		System.out.println(ranName());
	}
	
	public static List<String> na =new ArrayList<String>();
	static{
		na.add("蛋");na.add("黄");na.add("派");na.add("大");na.add("星");
		na.add("蠢");na.add("豆");na.add("卡");na.add("蛮");na.add("疼");
		na.add("愚");na.add("牛");na.add("猪");na.add("羊");na.add("狗");
		na.add("熊");na.add("鸡");na.add("鸭");na.add("恶");na.add("蛇");
		na.add("犬");na.add("夜");na.add("叉");na.add("鱼");na.add("肉");
		na.add("虎");na.add("蛇");na.add("羽");na.add("翼");na.add("驴");
		na.add("小");na.add("儿");na.add("痴");na.add("呆");na.add("卡");
		na.add("赤");na.add("绿");na.add("青");na.add("蓝");na.add("紫");
		na.add("冰");na.add("火");na.add("二");na.add("锅");na.add("头");
	}
	public static int getCount(int edge){
		Random ran = new Random();
		int nextInt = ran.nextInt(edge);
		return nextInt;
	}
	public static int getCount2(int edge){
		Random ran = new Random();
		int nextInt = ran.nextInt(edge);
		return nextInt+2;
	}
	public static int getNameCount(){
		return getCount2(3);
	}
	public static int poolSize(){
		int poolSize = na.size();
		return getCount(poolSize);
	}
	public static String ranName(){
		
		int nameCount = getNameCount();
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<nameCount;i++){
			String strTmp = na.get(poolSize());
			sb.append(strTmp);
		}
		return sb.toString();
	}
	
	//sors first Random Skill,just for fun ,hah ,its dosent metter
	//and this skill have no consume,but low demage. 
	//create skill package and skill object later 
	
	public static String randomSkill(){
		//
		return "";
	}
	
	
	
}

5、神奇的东西:

package com.yy.diabio.v1.box;

public class MapPic {
	
	public static void showMap(){
		System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++");
		System.out.println("+    .      .                           ++        ~");
		System.out.println("+      .   . +  + +  + +   +  + +   +  +  +   + ~  ");
		System.out.println("+          +                 +             +~  ~  ~");
		System.out.println("+       .  +        福莱特王国                                                      ~  ~  ");
		System.out.println("+       .  +                        +          ~ 大 ");
		System.out.println("+  戴泽特国      .   + +  +                          ~  海");
		System.out.println("+        .  =    =    +  +           +         ~  ~");
		System.out.println("+      .   =        = =    +  +   +    普莱恩王国     ~ ~ ");
		System.out.println("+    .   =            = =                       ~ ~");
		System.out.println("+  .  =  =     芒特那联合体          = =                 ~ ~  ");
		System.out.println("+                            =                 ~ ~ ");
		System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++");
		System.out.println("++++++++++++++++++++*世界-地图*+++++++++++++++++++++++");
		System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++");

		
	}
	public static void showCity(){
		System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++");
		System.out.println("+                                                 +");
		System.out.println("+                + + ++ + ++ ++                            +");
		System.out.println("+                +============+                     +");
		System.out.println("+                +            +                     +");
		System.out.println("+        + + + + +            + + + ++++                      +");
		System.out.println("+        +=======             =========+             +");
		System.out.println("+        +         ----------          +            +");
		System.out.println("++ ++ ++ +         |        |          +++ ++ ++ ++        +");
		System.out.println("+--------+         |        |          +           +");
		System.out.println("+        +         |        |          +            +");
		System.out.println("+        +         |        |          +           +");
		System.out.println("+        +         |        |          +           +");
		System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++");
		System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++");
		System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++");
		System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++");
		System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++");
		System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++");
		System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++");
		
	}
	


	public static void showBear(){
		System.out.println("┴┬┴┬/ ̄\_/ ̄\");
		System.out.println("┬┴┬┴▏  ▏▔▔▔▔\");
		System.out.println("┴┬┴/\ /      ﹨");
		System.out.println("┬┴∕       /   )");
		System.out.println("┴┬▏        ●  ▏");
		System.out.println("┬┴▏           ▔█");
		System.out.println("┴◢██◣       \__/");
		System.out.println("┬█████◣       /");
		System.out.println("┴█████████████◣");
		System.out.println("◢██████████████▆▄");
		System.out.println("◢██████████████▆▄");
		System.out.println("█◤◢██◣◥█████████◤\");
		System.out.println("◥◢████ ████████◤   \");
		System.out.println("┴█████ ██████◤      ﹨");
		System.out.println("┬│   │█████◤        ▏");
		System.out.println("┴│   │              ▏");
		System.out.println("┬∕   ∕    /▔▔▔\     ∕");
		System.out.println("*∕___/﹨   ∕      \  /\");
		System.out.println("┬┴┬┴┬┴\    \_     ﹨/  ﹨");
		System.out.println("┴┬┴┬┴┬┴ \___\     ﹨/▔\﹨/▔\");
		System.out.println("▲△▲▲╓╥╥╥╥╥╥╥╥\   ∕  /▔﹨ /▔﹨");
		
	}
	public static void showBook(){
		System.out.println("┏┳━━━━━━━━┓");
		System.out.println("┃┃████████┃");
		System.out.println("┃┃████┏━┓█┃");
		System.out.println("┃┃████┃-┃█┃");
		System.out.println("┣┫████┃如┃█┃");
		System.out.println("┃┃████┃来┃█┃");
		System.out.println("┃┃████┃神┃█┃");
		System.out.println("┃┃████┃掌┃█┃");
		System.out.println("┃┃████┃-┃█┃");
		System.out.println("┣┫████┗━┛█┃");
		System.out.println("┃┃████████┃");
		System.out.println("┃┃███ 定价5毛┃");
		System.out.println("┗┻━━━━━━━━┛");
		
	}
	public static void showHose(){
		System.out.println("     /\\~~~~~~~~~~~~~\\   ");
		System.out.println("  ./   \\~~~▓~        ~~~~\\ ◆ ");
		System.out.println("  / == \\      ══════\\.◆ ");
		System.out.println(" ..▎[] ▎田                     田 ▎ |┃◆  .   ");
		System.out.println(" &&▎    ▎                      ▎'|'▎ @  ");
		System.out.println("# ■■■■■■■■■■〓▄▃▂           }||}}");
		System.out.println("");
		System.out.println("");
	}
	public static void showHelmet(){
		System.out.println("*☆∵ ◥■▄■◤  .★∵∴");
		System.out.println("∴★◢██★██◣* ☆.∴");
		System.out.println("☆◢██    █    ██◣.∴");
		System.out.println("◢    ◤██     ██◥◣.☆");
		System.out.println("◥    ∴█████.∵");
	}
	public static void showJacket(){
		System.out.println("*☆∵ ▁▂▄▂▁.★∵∴");
		System.out.println("∴★◢█████◣* ☆.∴");
		System.out.println("☆◢████☆██◣.∴");
		System.out.println("◢■◤█████◥█◣.☆");
		System.out.println("◥◤∴█████.◥◤∵");
	}
	public static void showpants(){
		System.out.println("*☆∵ ▁▂★▂▁.★∵∴");
		System.out.println("∴★    █████ * ☆.∴");
		System.out.println("☆◢    ██    ██  .∴");
		System.out.println("◢    ◤██    ██       .☆");
		System.out.println("◥◤∴██    ██.       ∵");
		System.out.println("◥◤∴██    ██.       ∵");
		System.out.println("◥◤∴██    ██.       ∵");
	}
	public static void showBoot(){
		System.out.println("∴★    █████ * ☆.∴");
		System.out.println("☆◢        █    ██  .∴");
		System.out.println("◢    ◤    █        █       .☆");
		System.out.println("◥◤        █    ██.       ∵");
		System.out.println("◥◤∴    █          ███.       ∵");
		System.out.println("◥◤∴██    █████.       ∵");
	}
	public static void showHouse(){
		System.out.println("                               _(\\_/) ");
		System.out.println("                             ,((((^`\\");
		System.out.println("                             ((((  (6 \\ ");
		System.out.println("                          ,((((( ,    \\");
		System.out.println("       ,,,_              ,(((((  /'._  ,`,");
		System.out.println("      ((((\\ ,...       ,((((   /    `-.-'");
		System.out.println("      )))  ;'    `''''''((((   (");
		System.out.println("    (((  /            (((      \\");
		System.out.println("      )) |                      |");
		System.out.println("     ((  |        .       '     |");
		System.out.println("     ))  \\     _ '      `t   ,.')");
		System.out.println("     (   |   y;- -,-''''-.\\   \\/");
		System.out.println("    )   / ./  ) /         `\\  \\");
		System.out.println("        |./   ( (           / /'");
		System.out.println("        ||     \\          //'|");
		System.out.println("        ||      \\       _//'||");
		System.out.println("        ||       ))     |_/  ||");
		System.out.println("        \\_\\     |_/          ||");
		System.out.println("        `''                  \\_\\");
		System.out.println("                             `'''");
	}
	public static void main(String[] args) {
		showBear();
		showHose();
		showHouse();
	}
}

 

 

 

posted @ 2018-03-27 15:13  黑猫先生  阅读(158)  评论(0编辑  收藏  举报