• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to modify a .txt file in java?  RSS feed

 
Nancy Zhang
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to implement the following 4 steps in java:

step1. read a .txt file
step2. find all >= in the file
step3. add a string, say '@here', before each >=
step4. save the modified .txt as a new file

Great thanks!
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Nancy",
Welcome to the JavaRanch!

We're pretty light on rules, but one we take very seriously is one about display names. You can read about it here but basically we require your display name to be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and fix your display name, since accounts with invalid display names get deleted, often without warning

thanks,
Dave
 
Nancy Zhang
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for reminding, I've modified my displayed name.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In typical Ranch style, let's start with some general places to go look. If that gets you a little bit into code and you have more questions, come back and we'll dig for more details.

Reading and writing text files: Look at InputStreamReader and see what it does. Then look at the known implementing classes. One of them ought to look just right. Look at OutputStreamWriter the same way.

Finding >= Look at String.indexOf.

Inserting a new word: In JDK 5 you can do something with replaceAll, in earlier JDKs you probably have to get the substring before the new word, add the new word, add the substring after the new word.

That's deliberately vague ... holler if it's too much so. Have fun, keep coding, let us know how it goes.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you expect to find more than one >=, you may want to familiarize yourself with the java.lang.StringBuffer class which can be modified (String is immutable).
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For steps 1 and 4, see The Java Tutorial, "I/O: Reading and Writing..."
http://java.sun.com/docs/books/tutorial/essential/io/index.html

For steps 2 and 3, see the Java API for StringBuffer...
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuffer.html

Or if you're using Java 1.5, use StringBuilder instead of StringBuffer...
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuilder.html
 
Nancy Zhang
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all replies. Now have some ideas to start it. Might come back later for questions during further coding.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!