
-----------------------------------
Chinmay
Sat Dec 29, 2007 11:53 pm

Why wouldnt this work?
-----------------------------------
I am new to Java programming as well as the I/O class of java.

I made this code to write data to a file and was wondering why it wasnt working  :cry: Can u guys help me out and explain it plz.

import java.io.*;
class createFile
{
	public static void main(String str[]) throws IOException {
		File f = new File("c:/create.txt");
		
		if (f.canRead()) {
			System.out.println("Can read.");
		} else {
			System.out.println("Cant read.");
		}
	
		if (f.canWrite()) {
			System.out.println("Can write.");
		} else {
			System.out.println("Cant write.");
		}
		
		OutputStream f2 = new FileOutputStream(f);
		f2.write("Hello");
		
	}

}


@MyProjects\createFile.java:26: cannot find symbol
symbol  : method write(java.lang.String)
location: class java.io.OutputStream
		f2.write("Hello");
		  ^
1 error


can someone explain why this wouldnt work and how I can get it to work, THX  :D

-----------------------------------
HeavenAgain
Sun Dec 30, 2007 1:52 am

RE:Why wouldnt this work?
-----------------------------------
and just by loking at it, if the file connot write, then how can you write "hello" therefore it should be added after this
OutputStream f2 = new FileOutputStream(f);
if (f.canWrite()) {
System.out.println("Can write.");
f2.write("Hello");
} else {
System.out.println("Cant write.");
}
}
} 
and as for why this will not work.... let see now
hereherePrintWriter output = new PrintWriter (new BufferedWriter (new FileWriter("writeme.txt")));
output.println("Hello");
output.close();
I hope this helps, Good luck

-----------------------------------
Chinmay
Sun Dec 30, 2007 1:06 pm

Re: Why wouldnt this work?
-----------------------------------
Can you also gimme a link or explain what is a bufferedWriter and how it works, greatly appreciated and also thx the above responce, it did help. :)

-----------------------------------
HeavenAgain
Sun Dec 30, 2007 1:46 pm

RE:Why wouldnt this work?
-----------------------------------
i believe bufferedwriter is a writer which writes in memory first, then when you close the writer, it "dumps" everything on to the file which you are writing, if you want more details you can check out google or the java api :roll:

-----------------------------------
Chinmay
Wed Jan 02, 2008 4:24 pm

Re: Why wouldnt this work?
-----------------------------------
ok so i found out how to append text to the file using

output.append("Bye");

but then, it prints it right next to the Hello, how do i make it write the appending text to the next line in the file?

Is there any other way to write text to the next line? other then using append

-----------------------------------
HeavenAgain
Wed Jan 02, 2008 5:14 pm

RE:Why wouldnt this work?
-----------------------------------
in PrintWriter there is print and println, just like how you use standard output System.out.println
like i said, if you are not using PrintWriter, look up the class you are using in the api, and you will find your answers there

-----------------------------------
Tony
Wed Jan 02, 2008 5:23 pm

RE:Why wouldnt this work?
-----------------------------------
output.append("Bye"); does just what it says it will -- it appends the text to the end. I would not want it to add arbitrary characters (such as new lines) without asking it to.


output.append("\nThis is a new line");

should work though.
