Click here to Skip to main content
15,896,154 members
Home / Discussions / Java
   

Java

 
AnswerRe: Compiling Java Code Pin
nkadivar29-Oct-08 11:52
nkadivar29-Oct-08 11:52 
AnswerRe: Compiling Java Code Pin
toxcct29-Oct-08 22:31
toxcct29-Oct-08 22:31 
GeneralRe: Compiling Java Code Pin
leesoief4-Nov-08 20:28
leesoief4-Nov-08 20:28 
QuestionJava Servlets with AJAX Pin
ro88o29-Oct-08 5:22
ro88o29-Oct-08 5:22 
QuestionData access objects used in jsp and beans? [modified] Pin
pdoy070827-Oct-08 15:16
pdoy070827-Oct-08 15:16 
QuestionSingle Sign On Pin
vedabj27-Oct-08 1:18
vedabj27-Oct-08 1:18 
Question"Help" JTable problem Pin
slslooo26-Oct-08 20:54
slslooo26-Oct-08 20:54 
QuestionHelp on Search and Replace in a file using java Pin
go4rib24-Oct-08 20:21
go4rib24-Oct-08 20:21 
Hi Xiang,

I found, your program for Searching and replacing strings (TextRep.java) in a file very usefull. Thanks for posting it.

Explaing below a scenario which im working on :

1. My Program should read the file name, search string and string to be replaced from an xml file.
<cmvc>
<cmvcauto>
<filename>C:\cmvc\Readme.txt</filename>
<string>Following are the contents of this CD.</string>
<replace>Im chnaged : Following are the contents of this CD.</replace>;
</cmvcauto>
</cmvc>
2. Get the file, search for the existence of the string in tht file , replace the old one with the new string and save modified file in a diff location.

I thought of modifying your program to match my requirement. Since your program TextRep.java also takes a backup of the file....my issue of saving the modified file is also solved. I have a program to get the values from the xml file. Now im passing those values to ur program instead of args[0], args[1] and args[2].

i get the value of filename from xml file in the fashion :
NodeList textFNList = firstNameElement.getChildNodes();
System.out.println("First file name : " +
((Node)textFNList.item(0)).getNodeValue().trim());

Now i define a method "replaceString" and call it in the main program by passing the values that i get from the xml file :
NodeList textFNList = firstNameElement.getChildNodes();
System.out.println("First file name : " +
((Node)textFNList.item(0)).getNodeValue().trim());

//-------
NodeList lastNameList = firstPersonElement.getElementsByTagName("string");
Element lastNameElement = (Element)lastNameList.item(0);

NodeList textLNList = lastNameElement.getChildNodes();
System.out.println("Search String : " +
((Node)textLNList.item(0)).getNodeValue().trim());

//----
NodeList ageList = firstPersonElement.getElementsByTagName("replace");
Element ageElement = (Element)ageList.item(0);

NodeList textAgeList = ageElement.getChildNodes();
System.out.println("Replace string " +
((Node)textAgeList.item(0)).getNodeValue().trim());

replaceString(((Node)textFNList.item(0)).getNodeValue().trim(), ((Node)textLNList.item(0)).getNodeValue().trim(), ((Node)textAgeList.item(0)).getNodeValue().trim());

Below is the method defenition and the defenition is your program :

public static void replaceString(String fileName, String match, String replacingString)
{

try
{
// make 32k buffer for output
StringBuffer strOutput = new StringBuffer(32768);
// read input file into a byte array
//byte[] pInput = ReadFile(args[0]); This is the original line from your program
byte[] pInput = ReadFile((filename);
// make a backup copy
WriteFile(fileName +".backup.copy",pInput);
String strInput = new String(pInput);

Doing this modification, i get the below error :

C:\Documents and Settings\Administrator\Desktop\CMVC_Automation>javac fullSearch.java
fullSearch.java:107: cannot find symbol
symbol : method ReadFile(java.lang.String)
location: class fullSearch
byte[] pInput = ReadFile(fileName);
^
fullSearch.java:109: cannot find symbol
symbol : method WriteFile(java.lang.String,byte[])
location: class fullSearch
WriteFile(fileName +".backup.copy",pInput);
^
2 errors

My program starts with these lines :
import org.w3c.dom.Document;
import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.io.*;
import java.util.*;

Would really apreciate, if you could help on those errors. Thanks.
QuestionHow to specify character encoding in JavaMail? Pin
davilism24-Oct-08 7:26
davilism24-Oct-08 7:26 
QuestionDifferent between JRE 1.6 and JRE 1.5 Pin
utnqbao22-Oct-08 18:53
professionalutnqbao22-Oct-08 18:53 
AnswerRe: Different between JRE 1.6 and JRE 1.5 Pin
toxcct22-Oct-08 21:27
toxcct22-Oct-08 21:27 
GeneralRe: Different between JRE 1.6 and JRE 1.5 Pin
utnqbao22-Oct-08 22:23
professionalutnqbao22-Oct-08 22:23 
GeneralRe: Different between JRE 1.6 and JRE 1.5 Pin
toxcct22-Oct-08 22:33
toxcct22-Oct-08 22:33 
GeneralRe: Different between JRE 1.6 and JRE 1.5 Pin
utnqbao23-Oct-08 0:06
professionalutnqbao23-Oct-08 0:06 
GeneralRe: Different between JRE 1.6 and JRE 1.5 Pin
toxcct23-Oct-08 2:22
toxcct23-Oct-08 2:22 
GeneralRe: Different between JRE 1.6 and JRE 1.5 [modified] Pin
utnqbao23-Oct-08 16:29
professionalutnqbao23-Oct-08 16:29 
QuestionAfter update of fix Windows Update: KB956390, in HTML buttons are developed in VML are not working Pin
ezhil2421-Oct-08 0:03
ezhil2421-Oct-08 0:03 
QuestionDownload JDK 1.6_04 Pin
Kevin Marois20-Oct-08 8:39
professionalKevin Marois20-Oct-08 8:39 
AnswerRe: Download JDK 1.6_04 Pin
toxcct20-Oct-08 21:24
toxcct20-Oct-08 21:24 
Questioncolumns side by side Pin
ic3e18-Oct-08 14:01
ic3e18-Oct-08 14:01 
AnswerRe: columns side by side Pin
sunil_jvfrm1-Nov-08 1:30
sunil_jvfrm1-Nov-08 1:30 
Questionwondering why it doesnt create another text field Pin
srinath158317-Oct-08 23:11
srinath158317-Oct-08 23:11 
Questionsun.net.smtp.SmtpProtocolException help Pin
davilism16-Oct-08 7:12
davilism16-Oct-08 7:12 
QuestionScanner Not Found Pin
Kevin Marois15-Oct-08 12:17
professionalKevin Marois15-Oct-08 12:17 
AnswerRe: Scanner Not Found Pin
toxcct15-Oct-08 23:16
toxcct15-Oct-08 23:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.