Computer Science Canada Why wouldnt this work? |
Author: | Chinmay [ Sat Dec 29, 2007 11:53 pm ] |
Post subject: | 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 ![]() 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 ![]() |
Author: | HeavenAgain [ Sun Dec 30, 2007 1:52 am ] | ||||
Post subject: | 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
and as for why this will not work.... let see now here and here tells that there is no such method takes in parameter as a String, so you cannot do that. So is best for you to check the api sometimes and now if you want to write Hello into a file, what you could do is to use PrintWriter, is old but faster i beleive, see example below
I hope this helps, Good luck |
Author: | Chinmay [ Sun Dec 30, 2007 1:06 pm ] |
Post subject: | 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. ![]() |
Author: | HeavenAgain [ Sun Dec 30, 2007 1:46 pm ] |
Post subject: | 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 ![]() |
Author: | Chinmay [ Wed Jan 02, 2008 4:24 pm ] |
Post subject: | 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 |
Author: | HeavenAgain [ Wed Jan 02, 2008 5:14 pm ] |
Post subject: | 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 |
Author: | Tony [ Wed Jan 02, 2008 5:23 pm ] | ||
Post subject: | 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.
should work though. |