Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-07-2007, 04:43 PM
Member
 
Join Date: Aug 2007
Posts: 15
one198 is on a distinguished road
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();
}
}


}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-07-2007, 05:14 PM
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
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:

Code:
output.write(acontents.toString());
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-07-2007, 06:05 PM
Member
 
Join Date: Aug 2007
Posts: 15
one198 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-07-2007, 08:12 PM
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-08-2007, 12:22 AM
Member
 
Join Date: Aug 2007
Posts: 2
igorgf is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 08-08-2007, 12:49 AM
Senior Member
 
Join Date: Mar 2007
Posts: 136
goldhouse is on a distinguished road
Hey You just forgot to close the writer .
Here is the modified code
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()"
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 08-08-2007, 12:10 PM
Member
 
Join Date: Aug 2007
Posts: 15
one198 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 08-09-2007, 12:41 AM
Senior Member
 
Join Date: Mar 2007
Posts: 136
goldhouse is on a distinguished road
You are welcome
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 08-09-2007, 10:32 AM
Member
 
Join Date: Jul 2007
Posts: 9
vata2999 is on a distinguished road
And What about website that have unicode character like farsi ?
can write into file in unicode ?
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 08-09-2007, 11:07 AM
Member
 
Join Date: Aug 2007
Posts: 15
one198 is on a distinguished road
hi guys

Thank you for ur suggestion vata2999.still i did nt think about unicodes
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +3. The time now is 06:13 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org