Java 8 Stream min and max method examples. Java program to replace string in a file. Java program to replace strings in a file : In this tutorial, we will learn how to read strings from a file, how to modify the contents and then again write them back in the same file. This is a new ball. This is a new house. This is a new pen. All new members are invited to the club. This is a old ball. This is a old house. This is a old pen. Step 5 : Replace all the occurrences of oldString with newString using replaceAll method.
You will get a newContent. The below program takes StudentFile. How to sort text file in java? How to compare two text files in java? How to find most repeated word in a text file in java? How to list all files in a directory in java? How to read and write images in java? But I felt it doesn't make sense because I don't want it to get appended to end of file.
It has to replace the existing line. Am I missing something again? I just tried append. It appends all the content including the lines that didn't match to end of file..
At least this tells me the unmatched lines are not totally lost. But I need to figure out how to rewrite the file with the new content. AshwiniR Trying to just replace the lines of the file you want -- especially when the file is text and has newlines, instead of just replacing bytes -- is going to be a huge mess if it's possible at all. Just do the most straightforward thing: read the whole file into lines, replace the appropriate places, write the whole thing back. Add a comment.
Active Oldest Votes. Improve this answer. Louis Wasserman Louis Wasserman k 24 24 gold badges silver badges bronze badges. All the comments were very informative! Thank you all! And btw, parallelStreams on IO bound tasks is often a bad idea. The fileName is the directory ie. I'm not sure what character set should be used, but I ran this code on a Windows XP machine just now and it did the trick without doing that temporary file creation and renaming stuff. You might want to use Scanner to parse through and find the specific sections you want to modify.
There's also Split and StringTokenizer that may work, but at the level you're working at Scanner might be what's needed. Here's some additional info on what the difference is between them: Scanner vs. StringTokenizer vs. This is the sort of thing I'd normally use a scripting language for.
Note that if you want to be particular in manipulating XML, it's advisable to read the file as XML and manipulate it as such the above scripting languages have very useful and simple APIs for doing this sort of work. You can use Java's Scanner class to parse words of a file and process them in your application, and then use a BufferedWriter or FileWriter to write back to the file, applying the changes.
I think there is a more efficient way of getting the iterator's position of the scanner at some point, in order to better implement editting. But since files are either open for reading, or writing, I'm not sure regarding that.
In any case, you can use libraries already available for parsing of XML files, which have all of this implemented already and will allow you to do what you want easily. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 11 years, 3 months ago.
0コメント