Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

about changeing a word in one file into another word

Posted on 2003-12-06
10
Medium Priority
?
272 Views
Last Modified: 2012-05-04
hello,

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
---------------------------------------
http://www.yahoo.com/0.gif
http://www.yahoo.com/10.gif
http://www.yahoo.com/20.gif
http://www.yahoo.com/30.gif
http://www.yahoo.com/40.gif
http://www.yahoo.com/50.gif
http://www.yahoo.com/60.gif
http://www.yahoo.com/70.gif
http://www.yahoo.com/80.gif
http://www.yahoo.com/90.gif
http://www.yahoo.com/100.gif
http://www.yahoo.com/110.gif
http://www.yahoo.com/120.gif
http://www.yahoo.com/130.gif
http://www.yahoo.com/140.gif
http://www.yahoo.com/150.gif
http://www.yahoo.com/160.gif
http://www.yahoo.com/170.gif
---------------------------------------------------

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");
            
            while(!eof)
            {
                  int input = in.read();
                  System.out.println((char)input + " ");
            
                  if((char)input == "http://")
                  {
                        out.write("BAD\n");
                  }
                  else
                  {
                        out.write("GOOD\n");
                  }
                              

                  if(input == -1)
                        eof = true;
                  else
                        count++;
            }            
            
            
            out.close();
            in.close();
            
            
            }
            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"??

Thxx
Tom
0
Comment
Question by:tom_mk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 9889965
you need to write the new content to a second file, and replace the existing file once you are done.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9890001
Have a look at the replace method in the String object (the one that takes regular expressions).
0
 
LVL 92

Expert Comment

by:objects
ID: 9890009
try using a BufferedReader to read the file line by line:

String line = null;
while (null!=(line=in.readLine()))
{
   ...
0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 15

Expert Comment

by:jimmack
ID: 9890037
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
{
    static
    {
        // Code that is executed when class is loaded
    }

    .
    .
}
0
 

Author Comment

by:tom_mk
ID: 9890234
about

bufferedreader

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

Thx
Tom


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

Thxx
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9890259
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 9890278
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?
0
 
LVL 15

Accepted Solution

by:
jimmack earned 1500 total points
ID: 9890306
Certainly :

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

should be either:

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

or

  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 :-(
0
 

Author Comment

by:tom_mk
ID: 9890536
thx jimmack

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

Thx
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9891585
>> 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
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question