-
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();
}
}
}
-
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());
-
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
-
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.
-
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
-
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()"
-
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
-
-
And What about website that have unicode character like farsi ?
can write into file in unicode ?
-
hi guys
Thank you for ur suggestion vata2999.still i did nt think about unicodes