一个把String转成byte数组的小程序

来源:http://www.study-code.com/java/j2se/66420.htm

 

/*
*@类功能:将一个文件中的String声明转换成byte[] 声明
*
*
*@作者:关文柏
*@时间:2006年11月11日
*/

//---------------------------------------------------------------//
//NOTE: Make sure the identifier of variable as same as "inflag_0"
//
//---------------------------------------------------------------//
import java.io.*;
import java.util.*;

public class MakeResource
{
 //Output file declare symbol
 private static final String outflag_0 = " public final static byte[] ";
 private static final String outflag_1 = " = {";
 private static final String outflag_2 = "};";
 private static final String outflag_3 = ",";
 private static final int ch_0 = 0x0d;
 private static final int ch_1 = 0x0a;
 
 //Input file declare symbol
 private static final String inflag_0 = "public final static String"; //' the squence may variety
 private static final char inflag_1 = '"';
 private static final char inflag_2 = ';';
 private static final char inflag_3 = '=';
 private static final char inflag_4 = '/';
 

 //Resource of all string
 private String[] res = null;
 //Names of all string
 private String[] resNames = null;

 //For debug
 private boolean debug = false;
 public MakeResource()
 {
  System.out.println("Please enter the file name:");
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  try{
   doInput(br.readLine());
  
  }catch(Exception e){
   e.printStackTrace();
  }

  doOutput(res,resNames);
  
 }

 public static void main(String[] args)
 {
  new MakeResource();
 }

 private void doInput(String fileName)
 {
  try{
  
   FileInputStream fis = new FileInputStream( new File( fileName ) );
   
   byte[] contents = new byte[ fis.available() ];
   
   fis.read( contents );
   if(debug)
    System.out.println("The file has "+contents.length+" bytes");
   
   char[] declare = inflag_0.toCharArray();
   
   Vector names = new Vector(); //For save variable names
   
   Vector resStr = new Vector(); //For save the variable's content
   
   int x = 0;   //index of all bytes
   
   boolean out = false; //Flag for out of loop

   int variableNum = 0;
   while(!out)
   {
    int counter = 0; //Matching char numbers
    
    int y = 0;   //index of 'declare' array
    if(contents[x]==inflag_4)  //skip a comment line
    {
     boolean out_0 = false;
     while( !out_0 && x+2 < contents.length)
     {
      if( ((int)contents[x+1]!= ch_0) && ((int)contents[x+2]!= ch_1) )
      {
       x++;
      }else
      {
       out_0 = true;
      }
     }
    }
    if(contents[x] == declare[y])
    {

     for(y = 1; y < declare.length  ; y++)
     {
      if(x + 1 < contents.length)
      {
       if(contents[++x]==declare[y])
        counter++;
      }
      else
      {
       out = true;      
      }
     }
    }
    //If some chars begin with "public final static String"
    if(counter==declare.length-1)
    {
     variableNum++;

     if(debug)
      System.out.println("A variable matching at "+x);
     if(x + 1 < contents.length)
      x++;  //skip a space in front of the variable name
     else
     {     
      out = true; 
     }

     StringBuffer avName = new StringBuffer(); //For save the variable name
     
     boolean out_1 = false;
     while( !out_1 && x+1 < contents.length)
     {
      if(contents[++x]!=inflag_3)
      {
       avName.append((char)contents[x]); 
      }else
      {
       out_1 = true;
      }
     }
     names.addElement( avName.toString() );
     
     boolean out_2 = false;
     StringBuffer str = new StringBuffer();  //For save the variable content
     while(!out_2 && x + 1 < contents.length)
     {
      if(contents[++x]==inflag_1)
      {
       boolean out_3 = false;
       while(!out_3 && x + 1 < contents.length)
       {       
        if( contents[++x]!=inflag_2 )
        {
         str.append( (char)contents[x] );
        }else
        {
         out_3 = true;
         out_2 = true;
        }
       }
       
      }
     }
     if(str.length() > 0) 
     str.deleteCharAt( str.length() -1  );  //Delete the last char of content,it is must a ' " '
     resStr.addElement( str.toString() );
     
    }
    counter = 0;  //reset counter

    if(x + 1 < contents.length)
     x++;  //If does not matched any char, move index of all bytes
    else
    {    
     out = true;
    }
   }
   res = new String[resStr.size()];
   //if(debug)
    //System.out.println("//--------------Variable contents--------------//");

   for(int p = 0 ; p < resStr.size(); p++)
   {
    res[p] = (String)resStr.elementAt(p);
    //if(debug)
     //System.out.println( (String)resStr.elementAt(p) );   
   }
   if(debug)
    System.out.println("");
   resNames = new String[names.size()];
   if(debug)
    System.out.println("//--------------Variable names--------------//");
   for(int q = 0 ; q < names.size(); q++)
   {
    resNames[q] = (String)names.elementAt(q);
    if(debug)
     System.out.println( (String)names.elementAt(q) );   
   }

   if(debug)
    System.out.println(variableNum+" variables has been finded!");

  }catch(Exception ex)
  {
   ex.printStackTrace();
  }
 }

 private void doOutput(String[] res,String[] names)
 {
  int strLength = 0;
  if((res==null)||(names==null))
  {
   System.out.println("Array does not initial");
   return;
  }
  if( (strLength = res.length ) != names.length )
  {
   System.out.println("Current array not correct");
   return;
  }
  try
  {
  FileWriter fw = new FileWriter( new File(".","result.txt") ); //A file named 'result' for save the content
  for(int m =0 ; m < strLength; m++)
  {
   byte[] b = res[m].getBytes();
   fw.write( outflag_0, 0, outflag_0.length() ); //write the identifier
   fw.write( names[m], 0, names[m].length() );  //write variable name
   fw.write( outflag_1, 0, outflag_1.length() ); //'={'
   for(int z = 0 ; z < b.length; z++)
   {
    fw.write( new Integer((int)b[z]).toString() ); //write bytes with int format
    if(z<b.length-1)
    fw.write( outflag_3, 0, outflag_3.length() ); //','
   }
   fw.write( outflag_2, 0, outflag_2.length() ); //'};'
   fw.write( ch_0 );  //write a enter for new line
   fw.write( ch_1 );
  }

  fw.close();
  System.out.println("Output successfully!!!!!");
  }catch(Exception e)
  {
   e.printStackTrace();
  }
  
 } 

posted @ 2008-11-08 13:08  KK in cnblog  阅读(1278)  评论(0编辑  收藏  举报