Results 1 to 10 of 10
Thread: Problem in my coding
- 08-07-2007, 03:43 PM #1
Member
- Join Date
- Aug 2007
- Posts
- 15
- Rep Power
- 0
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();
}
}
}
- 08-07-2007, 04:14 PM #2levent Guest
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());
- 08-07-2007, 05:05 PM #3
Member
- Join Date
- Aug 2007
- Posts
- 15
- Rep Power
- 0
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
- 08-07-2007, 07:12 PM #4levent Guest
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.
- 08-07-2007, 11:22 PM #5
Member
- Join Date
- Aug 2007
- Posts
- 2
- Rep Power
- 0
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
- 08-07-2007, 11:49 PM #6
Senior Member
- Join Date
- Mar 2007
- Posts
- 134
- Rep Power
- 0
Hey You just forgot to close the writer .
Here is the modified code
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()"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(); } } }
- 08-08-2007, 11:10 AM #7
Member
- Join Date
- Aug 2007
- Posts
- 15
- Rep Power
- 0
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
- 08-08-2007, 11:41 PM #8
Senior Member
- Join Date
- Mar 2007
- Posts
- 134
- Rep Power
- 0
You are welcome :)
- 08-09-2007, 09:32 AM #9
Member
- Join Date
- Jul 2007
- Posts
- 9
- Rep Power
- 0
And What about website that have unicode character like farsi ?
can write into file in unicode ?
- 08-09-2007, 10:07 AM #10
Member
- Join Date
- Aug 2007
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
Coding an FTP server in java
By Zucheto in forum NetworkingReplies: 3Last Post: 06-22-2008, 04:24 AM -
Help On Coding problem
By mandrake446 in forum New To JavaReplies: 3Last Post: 12-08-2007, 07:01 AM -
Error in my coding
By one198 in forum New To JavaReplies: 2Last Post: 10-13-2007, 05:07 AM -
Cannot solve the coding problem of my assignment
By elimmom in forum New To JavaReplies: 3Last Post: 08-13-2007, 11:33 AM -
Help with program coding
By cachi in forum AWT / SwingReplies: 1Last Post: 07-31-2007, 07:16 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks