Hey You just forgot to close the writer .
Here is the modified 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()"