about changeing a word in one file into another word


i'm trying to learn io stuff in JAVA right now..
and i don't know how to do this..

right now..i have one file which has this structure

i call this file "log1.txt"

i want to transform the word "http" into "BAD"

i then tried the following code


import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class io3
      public static void main(String args[])
            try {
            boolean eof = false;
            int count = 0;
            int a =0;
            System.out.println("This is a book\n");
            System.out.println("This is a book\n");

            FileReader in = new FileReader("c:/log1.txt");
            FileWriter out = new FileWriter("c:/log1.txt");
                  int input = in.read();
                  System.out.println((char)input + " ");
                  if((char)input == "http://")

                  if(input == -1)
                        eof = true;
            catch(IOException e)
                  System.out.println("Error : " + e.toString());


but there is one error on this line
if((char)input == "http://")

which i don't know how to correct it..and i don't think i have the right code to satisfy mu purpose..

anybody can help me

another problem

what's "static" in java
i don't really understand...this one meaning..
can i have some example or anythign that can make me undetrstand 'static"??

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
you need to write the new content to a second file, and replace the existing file once you are done.
Have a look at the replace method in the String object (the one that takes regular expressions).
Mick BarryJava DeveloperCommented:
try using a BufferedReader to read the file line by line:

String line = null;
while (null!=(line=in.readLine()))
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

I missed the last part of your question before.

There are a couple of uses for static.  A static method or static variable in a class can be accessed without needing a instance (object) to be created.  One good example of a static methods is the "main" method in your applications, which is accessed without actually creating an instance of your class.  This is why you can't access instance variables from the main method, no object exists.  Another good example are the methods in the Math class (eg. Math.abs()).  You don't need to create an instance of a Math object in order to access these methods.

If you make an attribute static, then one instance of that variable exists for the class (this is sometimes referred to as a class variable).  An example of the use of a class variable would be if you wanted a count of instances created.  A normal (non static) instance variable would be no use because a new one would be created for each object, but with a static, you could increment this within the constructor.

The other use of static is to execute some code in a class when the class is loaded.  This may be used to initialise complex variables that are also static.  To do this, you would do:

public class MyClass
        // Code that is executed when class is loaded

tom_mkAuthor Commented:


how can i access the word inside the line if i use bufferedreader to access my file line by line la?


i'm not that sure that i understand ur explanation much..can u post some example of code?

If you follow objects code, "line" is a String that is available inside the while loop.

Sorry, "replace" was a typo (and a bad one too :-( it could have got very confusing).  It should have been replaceAll or replaceFirst, as in the following example:

line = line.replaceAll("http", "BAD");  // You could use replaceFirst() if you expect the http string to occur again.
Mick BarryJava DeveloperCommented:
i was a bit confused on your actual requirements, you said you wanted to replace "http" with "BAD" but your code appears to be trying to do something different.
What exactly is the transformation you require?
Certainly :

 if((char)input == "http://")

should be either:

  if (input.startsWith("http://"))  // only lines that begin with http://


  if (input.indexOf("http://") != -1)  // lines that contain http:// anywhere

Sorry, because I originally saw that objects had answered the bit about reading the file, I hadn't looked at the code properly :-(

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tom_mkAuthor Commented:
thx jimmack

btw ..what does indexOf do?
is it try to find whereever the word be in the line?

>> is it try to find whereever the word be in the line?

Yes, eg:

String line = "This is a sentence";
System.out.println(line.indexOf("is"));  // would ouput 5
System.out.println(line.indexOf("x")); // would output -1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.