standardtool

来源:互联网 发布:java工程师职业发展 编辑:程序博客网 时间:2024/06/11 21:51

package mtool;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class StandardTool {
 
 BufferedReader mbr;
 BufferedWriter mbw;
 List<BufferedWriter> lsmbw;
 List<BufferedReader> lsmbr;
 
 public BufferedReader readText(String path) throws FileNotFoundException, UnsupportedEncodingException{
  File f = new File(path);
  FileInputStream fi = new FileInputStream(f);
  InputStreamReader isr = new InputStreamReader(fi,"UTF-8");
  mbr = new BufferedReader(isr);
  return mbr;
 }
 
 public List<BufferedReader> readText(List<String> path) throws FileNotFoundException, UnsupportedEncodingException{
  lsmbr = new ArrayList<BufferedReader>();
  for(int i=0;i<path.size()&&path!=null;i++){
   File f = new File(path.get(i));
   FileInputStream fi = new FileInputStream(f);
   InputStreamReader isr = new InputStreamReader(fi,"UTF-8");
   lsmbr.add(new BufferedReader(isr));
  }
  return lsmbr; 
 }
 
 public BufferedWriter writeText(String path) throws FileNotFoundException, UnsupportedEncodingException{
  File f = new File(path);
  FileOutputStream fo = new FileOutputStream(f);
  OutputStreamWriter osw = new OutputStreamWriter(fo,"UTF-8");
  mbw = new BufferedWriter(osw);
  return mbw;
 }
  
 public List<BufferedWriter> writeText(List<String> path) throws FileNotFoundException, UnsupportedEncodingException{
  lsmbw = new ArrayList<BufferedWriter>();
  for(int i=0;i<path.size()&&path!=null;i++){
   File f = new File(path.get(i));
   FileOutputStream fo = new FileOutputStream(f);
   OutputStreamWriter osw = new OutputStreamWriter(fo,"UTF-8");
   lsmbw.add(new BufferedWriter(osw));
  }
  return lsmbw;  
 }
 
 /**
  * initializing system std
  * @author jc08346
  */
 static PrintStream systemout;
 static PrintStream systemerr;
 static {
   System.out.println("saving system out&err\n");
   systemout = System.out;
   systemerr = System.err;
 }
 
 public void SetOut() throws FileNotFoundException{
  PrintStream out = new PrintStream(new File("rsc//out"));
  System.out.println("redirecting system out to rsc//out");
  System.setOut(out);
 } 
 public void SetOut(String s) throws FileNotFoundException{
  PrintStream out = new PrintStream(new File(s));
  System.out.println("redirecting system out to "+s);
  System.setOut(out);
 }
 public void SetErr() throws FileNotFoundException{
  PrintStream err = new PrintStream(new File("rsc//err"));
  System.out.println("redirecting system err to rsc//err");
  System.setErr(err);
 }
 public void SetErr(String s) throws FileNotFoundException{
  PrintStream err = new PrintStream(new File(s));
  System.out.println("redirecting system out to "+s);
  System.setErr(err);
 }
 public static void ResetOut(){
  System.setOut(systemout);
 }
 public static void ResetErr(){
  System.setErr(systemerr);
  List l = new ArrayList<Integer>();
 }
 
 /**
  * timer for performance testing
  * @author jc08346
  *
  */
 abstract class Timer<T>{
  T s,e;
  public abstract void tStart();
  public abstract void tEnd();
  public abstract T calc(T s, T e);
  public T tRs(){return calc(s,e); }
 }
 public Timer<Long> getTimer(){
  return new Timer<Long>(){
   public void tStart(){ s = System.currentTimeMillis(); }
   public void tEnd(){ e = System.currentTimeMillis(); }
   @Override
   public Long calc(Long s,Long e){ return (e-s); }
  };
 }
}

原创粉丝点击