how to use this little program: java TextRep c:\temp\test2.txt "server" "client" java TextRep mytest.txt mystrings.txtimport java.io.*; import java.util.*;class TextRep { // the main function public static void main (String[] args) { if(args.length!=2&&args.length!=3) { // invalid number of arguments specified at command line System.out.println("Usage(s):\n"); System.out.println(" 1. java TextRep file_to_process \"search_for_word\" \"replace_with_word\""); System.out.println(" 2. java TextRep file_to_process file_of_words_to_search_replace"); return; } try { // make 32k buffer for output StringBuffer strOutput = new StringBuffer(32768); // read input file into a byte array byte[] pInput = ReadFile(args[0]); // make a backup copy WriteFile(args[0]+".backup.copy",pInput); String strInput = new String(pInput); if(args.length==3) { // check if words are empty if(args[1].equals("")||args[2].equals("")) { System.out.println("Cannot process empty words"); return; } System.out.println("Replacing \""+args[1]+"\" with \""+args[2]+"\" in file: "+args[0]); // find all instances of args[1] and replace it with args[2] int nPos = 0; while(true) { int nIndex = strInput.indexOf(args[1],nPos); // if args[1] can no longer be found, then copy the rest of the input if(nIndex<0) { strOutput.append(strInput.substring(nPos)); break; } // otherwise, replace it with args[2] and continue else { strOutput.append(strInput.substring(nPos,nIndex)); strOutput.append(args[2]); nPos = nIndex+args[1].length(); } } strInput = strOutput.toString(); } else if(args.length==2) { System.out.println("Processing file: "+args[0]); // create a string tokenizer with file args[1] StringTokenizer tokens = new StringTokenizer(new String(ReadFile(args[1])),"\r\n"); // check to see of the tokenizer has even number of tokens int nCount = tokens.countTokens(); if(nCount<1||nCount%2!=0) { System.out.println("Invalid number of non-empty lines in file: "+args[1]); return; } // for each pair of string tokens, replace the first one with the next one nCount = nCount/2; for(int i=0;i<nCount;i++) { // string to search in the input String strSearch = tokens.nextToken(); // string used to replace the previous one String strReplace = tokens.nextToken(); // check if words are empty if(strSearch.equals("")||strReplace.equals("")) { System.out.println("Cannot process empty words"); return; } // replace each instance of strSearch with strReplace System.out.println("Replacing \""+strSearch+"\" with \""+strReplace+"\""); int nPos = 0; while(true) { int nIndex = strInput.indexOf(strSearch,nPos); // if strSearch can no longer be found, then copy the rest of the input if(nIndex<0) { strOutput.append(strInput.substring(nPos)); break; } // otherwise, replace it with strReplace and continue else { strOutput.append(strInput.substring(nPos,nIndex)); strOutput.append(strReplace); nPos = nIndex+strSearch.length(); } } // continue to process the next pair of string tokens strInput = strOutput.toString(); strOutput = new StringBuffer(32768); } } // write the output string to file WriteFile(args[0],strInput.getBytes()); } catch(Exception e) { System.out.println(e.getMessage()); } } // helper function to read a file into a byte array static public final byte[] ReadFile(String strFile) throws IOException { int nSize = 32768; // open the input file stream BufferedInputStream inStream = new BufferedInputStream(new FileInputStream(strFile),nSize); byte[] pBuffer = new byte[nSize]; int nPos = 0; // read bytes into a buffer nPos += inStream.read(pBuffer,nPos,nSize-nPos); // while the buffer is filled, double the buffer size and read more while(nPos==nSize) { byte[] pTemp = pBuffer; nSize *= 2; pBuffer = new byte[nSize]; System.arraycopy(pTemp,0,pBuffer,0,nPos); nPos += inStream.read(pBuffer,nPos,nSize-nPos); } // close the input stream inStream.close(); if(nPos==0) { return "".getBytes(); } // return data read into the buffer as a byte array byte[] pData = new byte[nPos]; System.arraycopy(pBuffer,0,pData,0,nPos); return pData; } // helper function to write a byte array into a file static public final void WriteFile(String strFile, byte[] pData) throws IOException { BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(strFile),32768); if(pData.length>0) outStream.write(pData,0,pData.length); outStream.close(); } }
package test;import java.io.*; import java.util.*;public class PrintTest { public static void main(String[] args) { PrintWriter out = null; BufferedReader in = null; try { in = new BufferedReader(new FileReader(aFile)); } catch (Exception e) { e.printStackTrace(System.out); } try { String s = in.readLine(); while (s != null) { s = s.replaceAll("abc", "cba"); vec.add(s); s = in.readLine(); } in.close(); try { out = new PrintWriter(new FileWriter(aFile, false)); } catch (IOException e) { } for (int i = 0; i < vec.size(); i++) { out.println( (String) vec.get(i)); System.out.println( (String) vec.get(i)); } out.close(); } catch (IOException ex1) { } } private static String str = "c:\\test.txt"; static File aFile = new File(str); private static Vector vec = new Vector(); }
把true改成false就可以了。
把你程序的true改成false后,结果是test.txt变成空文件了。原因是:out =new PrintWriter(new FileWriter("c:\\test.txt", true))会直接先把文件清空,然后等待写入,而while ( (s = in.readLine()) != null) 这个循环使用readLine就读不到任何字符串了,所以s=null,循环没有执行就退出了,所以test.txt就是个空文件了。所以你最好还是先把文件内容全部读出来后再修改存回去。
java TextRep c:\temp\test2.txt "server" "client"
java TextRep mytest.txt mystrings.txtimport java.io.*;
import java.util.*;class TextRep
{
// the main function
public static void main (String[] args)
{
if(args.length!=2&&args.length!=3)
{
// invalid number of arguments specified at command line
System.out.println("Usage(s):\n");
System.out.println(" 1. java TextRep file_to_process \"search_for_word\" \"replace_with_word\"");
System.out.println(" 2. java TextRep file_to_process file_of_words_to_search_replace");
return;
}
try
{
// make 32k buffer for output
StringBuffer strOutput = new StringBuffer(32768);
// read input file into a byte array
byte[] pInput = ReadFile(args[0]);
// make a backup copy
WriteFile(args[0]+".backup.copy",pInput);
String strInput = new String(pInput);
if(args.length==3)
{
// check if words are empty
if(args[1].equals("")||args[2].equals(""))
{
System.out.println("Cannot process empty words");
return;
}
System.out.println("Replacing \""+args[1]+"\" with \""+args[2]+"\" in file: "+args[0]);
// find all instances of args[1] and replace it with args[2]
int nPos = 0;
while(true)
{
int nIndex = strInput.indexOf(args[1],nPos);
// if args[1] can no longer be found, then copy the rest of the input
if(nIndex<0)
{
strOutput.append(strInput.substring(nPos));
break;
}
// otherwise, replace it with args[2] and continue
else
{
strOutput.append(strInput.substring(nPos,nIndex));
strOutput.append(args[2]);
nPos = nIndex+args[1].length();
}
}
strInput = strOutput.toString();
}
else if(args.length==2)
{
System.out.println("Processing file: "+args[0]);
// create a string tokenizer with file args[1]
StringTokenizer tokens = new StringTokenizer(new String(ReadFile(args[1])),"\r\n");
// check to see of the tokenizer has even number of tokens
int nCount = tokens.countTokens();
if(nCount<1||nCount%2!=0)
{
System.out.println("Invalid number of non-empty lines in file: "+args[1]);
return;
}
// for each pair of string tokens, replace the first one with the next one
nCount = nCount/2;
for(int i=0;i<nCount;i++)
{
// string to search in the input
String strSearch = tokens.nextToken();
// string used to replace the previous one
String strReplace = tokens.nextToken();
// check if words are empty
if(strSearch.equals("")||strReplace.equals(""))
{
System.out.println("Cannot process empty words");
return;
}
// replace each instance of strSearch with strReplace
System.out.println("Replacing \""+strSearch+"\" with \""+strReplace+"\"");
int nPos = 0;
while(true)
{
int nIndex = strInput.indexOf(strSearch,nPos);
// if strSearch can no longer be found, then copy the rest of the input
if(nIndex<0)
{
strOutput.append(strInput.substring(nPos));
break;
}
// otherwise, replace it with strReplace and continue
else
{
strOutput.append(strInput.substring(nPos,nIndex));
strOutput.append(strReplace);
nPos = nIndex+strSearch.length();
}
}
// continue to process the next pair of string tokens
strInput = strOutput.toString();
strOutput = new StringBuffer(32768);
}
}
// write the output string to file
WriteFile(args[0],strInput.getBytes());
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
} // helper function to read a file into a byte array
static public final byte[] ReadFile(String strFile) throws IOException
{
int nSize = 32768;
// open the input file stream
BufferedInputStream inStream = new BufferedInputStream(new FileInputStream(strFile),nSize);
byte[] pBuffer = new byte[nSize];
int nPos = 0;
// read bytes into a buffer
nPos += inStream.read(pBuffer,nPos,nSize-nPos);
// while the buffer is filled, double the buffer size and read more
while(nPos==nSize)
{
byte[] pTemp = pBuffer;
nSize *= 2;
pBuffer = new byte[nSize];
System.arraycopy(pTemp,0,pBuffer,0,nPos);
nPos += inStream.read(pBuffer,nPos,nSize-nPos);
}
// close the input stream
inStream.close();
if(nPos==0)
{
return "".getBytes();
}
// return data read into the buffer as a byte array
byte[] pData = new byte[nPos];
System.arraycopy(pBuffer,0,pData,0,nPos);
return pData;
} // helper function to write a byte array into a file
static public final void WriteFile(String strFile, byte[] pData) throws IOException
{
BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(strFile),32768);
if(pData.length>0) outStream.write(pData,0,pData.length);
outStream.close();
}
}
import java.util.*;public class PrintTest {
public static void main(String[] args) {
PrintWriter out = null;
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(aFile));
}
catch (Exception e) {
e.printStackTrace(System.out); }
try {
String s = in.readLine();
while (s != null) {
s = s.replaceAll("abc", "cba");
vec.add(s);
s = in.readLine();
}
in.close();
try {
out = new PrintWriter(new FileWriter(aFile, false));
}
catch (IOException e) {
}
for (int i = 0; i < vec.size(); i++) {
out.println( (String) vec.get(i));
System.out.println( (String) vec.get(i));
}
out.close();
}
catch (IOException ex1) {
}
} private static String str = "c:\\test.txt";
static File aFile = new File(str);
private static Vector vec = new Vector();
}