⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 miscfile.java

📁 java xml开发指南(初学者推荐)Java Xml 编程指南书籍源码
💻 JAVA
字号:
package MyNa.utils;

import java.io.*;
import java.util.Hashtable;
import java.util.Enumeration;

public class MiscFile {

public static StringBuffer fileToStringBuffer(String fName) {
  StringBuffer sBuff=new StringBuffer();
  InputStreamReader inStr=null;
  try{
    inStr=new InputStreamReader(new FileInputStream(fName));
    char[] cBuff=new char[4096];
    int charsRead;
    while(-1 != (charsRead=inStr.read(cBuff)))
      sBuff.append(cBuff,0,charsRead);
  }catch(Exception ex){ex.printStackTrace();return null;}
  finally{
    try{inStr.close();}catch(Exception ex){}
    }
  return sBuff;
}
public static String fileSubstByTag(String fName,Env env)
    throws Exception{
  StringBuffer sBuff=fileToStringBuffer(fName);
  if(sBuff==null)throw new Exception("no file for "+fName);
  ParseSubst pS=new ParseSubst(sBuff);
  return pS.toString(env);  
}
public static String fileSubstByTag(String fName,RowSequence rows)
    throws ParseSubstException{
  StringBuffer sBuff=fileToStringBuffer(fName);
  if(sBuff==null)throw new ParseSubstException("no file for "+fName);
  ParseSubst pS=new ParseSubst(sBuff);
  return pS.toString(rows);  
}

public static String fileToString(String fName) {
  String content="";BufferedReader brin=null;
  try{
    brin=new BufferedReader(new FileReader(fName));
    String nextLine;
    while(null!=(nextLine=brin.readLine()))content+=nextLine+"\n";
    brin.close();
    }catch(IOException e)
       {try{brin.close();}catch(IOException ex){}}
  return content;
  }
public static String substLine(String L,Hashtable dict){
  // a preliminary version of the substition within DBFileSubst
  //  if dict.get("joe")=="schmoe" and dict.get("schmoe")="HELP"
  // then the line "$$SUBST:_: joe is _joe_, but schmoe is _schmoe_"
  // is transformed into "joe is schmoe, but schmoe is HELP".
  String theCommandPrefix="$$SUBST";
  if(L==null)return "";
  if(!L.startsWith(theCommandPrefix))return L;
  try{
    int cmdBegin=theCommandPrefix.length();
    int cmdEnd=L.indexOf(':');
    int delimEnd=L.indexOf(':',1+cmdEnd);
    String cmd=L.substring(2,cmdEnd);
    String delim=L.substring(1+cmdEnd,delimEnd);
    L=L.substring(1+delimEnd,L.length());
    return Misc.stringDelimSubst(L,delim,dict);
    }catch(Exception E){}
  return L;
}
public static String substLines(String fName,String defs) {
  return substLines(fName,Misc.splitDelimHash(defs));
}

public static String substLines(String fName,Hashtable dict) {
  String content="";BufferedReader brin=null;
  try{
    brin=new BufferedReader(new FileReader(fName));
    String nextLine;
    while(null!=(nextLine=brin.readLine()))
      content+=substLine(nextLine+"\n",dict);
    brin.close();
    }catch(IOException e)
       {try{brin.close();}catch(IOException ex){}}
  return content;
}
   
public static BufferedReader getBufferedReader(String fName){
 try{
   FileReader fR=new FileReader(fName);
   if(fR==null)return null;
   return new BufferedReader(fR);
 }catch(Exception ex)
    {ex.printStackTrace(); 
     return null;}
}

}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -