TextRep.java
Upload User: nobellight
Upload Date: 2013-06-19
Package Size: 4k
Code Size: 5k
Development Platform:

Java

  1. import java.io.*;
  2. import java.util.*;
  3. class TextRep
  4. {
  5. // the main function
  6. public static void main (String[] args)
  7. {
  8. if(args.length!=2&&args.length!=3)
  9. {
  10. // invalid number of arguments specified at command line
  11. System.out.println("Usage(s):n");
  12. System.out.println("  1.  java TextRep file_to_process "search_for_word" "replace_with_word"");
  13. System.out.println("  2.  java TextRep file_to_process file_of_words_to_search_replace");
  14. return;
  15. }
  16. try
  17. {
  18. // make 32k buffer for output
  19. StringBuffer strOutput = new StringBuffer(32768);
  20. // read input file into a byte array
  21. byte[] pInput = ReadFile(args[0]);
  22. // make a backup copy
  23. WriteFile(args[0]+".backup.copy",pInput);
  24. String strInput = new String(pInput);
  25. if(args.length==3)
  26. {
  27. // check if words are empty
  28. if(args[1].equals("")||args[2].equals(""))
  29. {
  30. System.out.println("Cannot process empty words");
  31. return;
  32. }
  33. System.out.println("Replacing ""+args[1]+"" with ""+args[2]+"" in file: "+args[0]);
  34. // find all instances of args[1] and replace it with args[2]
  35. int nPos = 0;
  36. while(true)
  37. {
  38. int nIndex = strInput.indexOf(args[1],nPos);
  39. // if args[1] can no longer be found, then copy the rest of the input
  40. if(nIndex<0)
  41. {
  42. strOutput.append(strInput.substring(nPos));
  43. break;
  44. }
  45. // otherwise, replace it with args[2] and continue
  46. else
  47. {
  48. strOutput.append(strInput.substring(nPos,nIndex));
  49. strOutput.append(args[2]);
  50. nPos = nIndex+args[1].length();
  51. }
  52. }
  53. strInput = strOutput.toString();
  54. }
  55. else if(args.length==2)
  56. {
  57. System.out.println("Processing file: "+args[0]);
  58. // create a string tokenizer with file args[1]
  59. StringTokenizer tokens = new StringTokenizer(new String(ReadFile(args[1])),"rn");
  60. // check to see of the tokenizer has even number of tokens
  61. int nCount = tokens.countTokens();
  62. if(nCount<1||nCount%2!=0)
  63. {
  64. System.out.println("Invalid number of non-empty lines in file: "+args[1]);
  65. return;
  66. }
  67. // for each pair of string tokens, replace the first one with the next one
  68. nCount = nCount/2;
  69. for(int i=0;i<nCount;i++)
  70. {
  71. // string to search in the input
  72. String strSearch = tokens.nextToken();
  73. // string used to replace the previous one
  74. String strReplace = tokens.nextToken();
  75. // check if words are empty
  76. if(strSearch.equals("")||strReplace.equals(""))
  77. {
  78. System.out.println("Cannot process empty words");
  79. return;
  80. }
  81. // replace each instance of strSearch with strReplace
  82. System.out.println("Replacing ""+strSearch+"" with ""+strReplace+""");
  83. int nPos = 0;
  84. while(true)
  85. {
  86. int nIndex = strInput.indexOf(strSearch,nPos);
  87. // if strSearch can no longer be found, then copy the rest of the input
  88. if(nIndex<0)
  89. {
  90. strOutput.append(strInput.substring(nPos));
  91. break;
  92. }
  93. // otherwise, replace it with strReplace and continue
  94. else
  95. {
  96. strOutput.append(strInput.substring(nPos,nIndex));
  97. strOutput.append(strReplace);
  98. nPos = nIndex+strSearch.length();
  99. }
  100. }
  101. // continue to process the next pair of string tokens
  102. strInput = strOutput.toString();
  103. strOutput = new StringBuffer(32768);
  104. }
  105. }
  106. // write the output string to file
  107. WriteFile(args[0],strInput.getBytes());
  108. }
  109. catch(Exception e)
  110. {
  111. System.out.println(e.getMessage());
  112. }
  113. }
  114. // helper function to read a file into a byte array
  115. static public final byte[] ReadFile(String strFile) throws IOException
  116. {
  117. int nSize = 32768;
  118. // open the input file stream
  119. BufferedInputStream inStream = new BufferedInputStream(new FileInputStream(strFile),nSize);
  120. byte[] pBuffer = new byte[nSize];
  121. int nPos = 0;
  122. // read bytes into a buffer
  123. nPos += inStream.read(pBuffer,nPos,nSize-nPos);
  124. // while the buffer is filled, double the buffer size and read more
  125. while(nPos==nSize)
  126. {
  127. byte[] pTemp = pBuffer;
  128. nSize *= 2;
  129. pBuffer = new byte[nSize];
  130. System.arraycopy(pTemp,0,pBuffer,0,nPos);
  131. nPos += inStream.read(pBuffer,nPos,nSize-nPos);
  132. }
  133. // close the input stream
  134. inStream.close();
  135. if(nPos==0)
  136. {
  137. return "".getBytes();
  138. }
  139. // return data read into the buffer as a byte array
  140. byte[] pData = new byte[nPos];
  141. System.arraycopy(pBuffer,0,pData,0,nPos);
  142. return pData;
  143. }
  144. // helper function to write a byte array into a file
  145. static public final void WriteFile(String strFile, byte[] pData) throws IOException
  146. {
  147. BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(strFile),32768);
  148. if(pData.length>0) outStream.write(pData,0,pData.length);
  149. outStream.close();
  150. }
  151. }