Results 1 to 10 of 10
  1. #1
    one198 is offline Member
    Join Date
    Aug 2007
    Posts
    15
    Rep Power
    0

    Default Problem in my coding

    Hi guys

    Iam developing software like offline web browser,but I am at initial stages.
    my idea is geting content of web page and save it to text file.I wrote down my coding below ,but it's gave me error

    Error is "can not resolve method 'write(java.lang.StringBuffer)' "

    pls help

    Thank you

    import java.net.*;
    import java.io.*;
    import java.nio.channels.FileChannel;
    import java.lang.String;
    import java.nio.ByteBuffer;

    public class Test {


    public static void main(String[] args) throws Exception {

    File aFile = new File("D:\\Java\\First.text");
    setcontents(aFile,getcontent());
    }

    static public StringBuffer getcontent()
    {
    StringBuffer contents = new StringBuffer();
    String text[];
    try{
    URL yahoo = new URL("http://www.yahoo.com");
    URLConnection yc = yahoo.openConnection();
    BufferedReader in = new BufferedReader(
    new InputStreamReader(
    yc.getInputStream()));

    String line;
    while (( line = in.readLine()) != null)
    {
    contents.append(line);
    contents.append(System.getProperty("line.separator "));

    }
    }
    catch(IOException e){
    e.printStackTrace();
    }
    return contents;
    }


    static public void setcontents(File afile,StringBuffer acontents)
    {
    Writer output=null;
    try{
    output=new BufferedWriter(new FileWriter(afile));
    output.write(acontents);
    }
    catch(IOException e){
    e.printStackTrace();
    }
    }


    }

  2. #2
    levent Guest

    Default

    Hi one198,

    The error is clear. write method of BufferedWriter class does not accept a StringBuffer object. Instead try converting your StringBuffer object into a String object and sending it as below:

    Java Code:
    output.write(acontents.toString());

  3. #3
    one198 is offline Member
    Join Date
    Aug 2007
    Posts
    15
    Rep Power
    0

    Default Hi guys

    Thank you levent.Now there is no errors when i complie my program.But once i run it expected result wasnt there.It menas contents of web page does
    not write into First.txt.

    contents should be html tages like <html>,body>,etc.....
    Do you think there are some wrong proceedure i following for develope this program?


    Thank you

  4. #4
    levent Guest

    Default

    It looks normal. Do you get any exception, what is the content you are getting? Try writing acontents.toString() to the screen first. In these ways, you can localize the error and then fix it.

  5. #5
    igorgf is offline Member
    Join Date
    Aug 2007
    Posts
    2
    Rep Power
    0

    Default

    Try to trace it using some thing like:
    System.out.print(acontents.toString()) - so you get direct idea what is getting entered in your method

  6. #6
    goldhouse is offline Senior Member
    Join Date
    Mar 2007
    Posts
    134
    Rep Power
    0

    Default

    Hey You just forgot to close the writer .
    Here is the modified code
    Java Code:
    package javaapplication1;
    import java.net.*;
    import java.io.*;
    import java.nio.channels.FileChannel;
    import java.lang.String;
    import java.nio.ByteBuffer;
    
    public class Test {
        
        
        public static void main(String[] args) throws Exception {
            
            File aFile = new File("C:\\Java\\First1.text");
            setcontents(aFile,getcontent());
        }
        
        static public StringBuffer getcontent() {
            StringBuffer contents = new StringBuffer();
            String text[];
            try{
                URL yahoo = new URL("http://www.yahoo.com");
                URLConnection yc = yahoo.openConnection();
                BufferedReader in = new BufferedReader(
                        new InputStreamReader(
                        yc.getInputStream()));
                
                String line;
                while (( line = in.readLine()) != null) {
                    contents.append(line);
                    contents.append(System.getProperty("line.separator "));
                    
                }
            } catch(IOException e){
                e.printStackTrace();
            }
            return contents;
        }
        
        
        static public void setcontents(File afile,StringBuffer acontents) {
            Writer output=null;
            try{
                System.out.println(acontents.toString());
                output=new BufferedWriter(new FileWriter(afile));
                output.write(acontents.toString());
                output.close();// This line I added
            } catch(IOException e){
                e.printStackTrace();
            }
        }
        
        
    }
    I made some modifications to make it working in my machine dont look at that , Only one line I think is important. i.e "output.close()"

  7. #7
    one198 is offline Member
    Join Date
    Aug 2007
    Posts
    15
    Rep Power
    0

    Default

    Hi guys

    Thank you goldhouse.I forget to close the writer.Now program is working properly.contents of web page is save to First.txt.Thank you every one
    for advices.

    Thank you

  8. #8
    goldhouse is offline Senior Member
    Join Date
    Mar 2007
    Posts
    134
    Rep Power
    0

    Default

    You are welcome :)

  9. #9
    vata2999 is offline Member
    Join Date
    Jul 2007
    Posts
    8
    Rep Power
    0

    Default

    And What about website that have unicode character like farsi ?
    can write into file in unicode ?

  10. #10
    one198 is offline Member
    Join Date
    Aug 2007
    Posts
    15
    Rep Power
    0

    Default

    hi guys

    Thank you for ur suggestion vata2999.still i did nt think about unicodes

Similar Threads

  1. Coding an FTP server in java
    By Zucheto in forum Networking
    Replies: 3
    Last Post: 06-22-2008, 05:24 AM
  2. Help On Coding problem
    By mandrake446 in forum New To Java
    Replies: 3
    Last Post: 12-08-2007, 08:01 AM
  3. Error in my coding
    By one198 in forum New To Java
    Replies: 2
    Last Post: 10-13-2007, 06:07 AM
  4. Cannot solve the coding problem of my assignment
    By elimmom in forum New To Java
    Replies: 3
    Last Post: 08-13-2007, 12:33 PM
  5. Help with program coding
    By cachi in forum AWT / Swing
    Replies: 1
    Last Post: 07-31-2007, 08:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •