Why wouldnt this work?
Author |
Message |
Chinmay
|
Posted: 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 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 ![Very Happy Very Happy](images/smiles/icon_biggrin.gif) |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
HeavenAgain
![](http://compsci.ca/v3/uploads/user_avatars/139122102045e603120b143.jpg)
|
Posted: 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
code: | 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
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
code: | PrintWriter output = new PrintWriter (new BufferedWriter (new FileWriter("writeme.txt")));
output.println("Hello");
output.close(); |
I hope this helps, Good luck |
|
|
|
|
![](images/spacer.gif) |
Chinmay
|
Posted: 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. ![Smile Smile](http://compsci.ca/v3/images/smiles/icon_smile.gif) |
|
|
|
|
![](images/spacer.gif) |
HeavenAgain
![](http://compsci.ca/v3/uploads/user_avatars/139122102045e603120b143.jpg)
|
Posted: 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 ![Rolling Eyes Rolling Eyes](http://compsci.ca/v3/images/smiles/icon_rolleyes.gif) |
|
|
|
|
![](images/spacer.gif) |
Chinmay
|
Posted: 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 |
|
|
|
|
![](images/spacer.gif) |
HeavenAgain
![](http://compsci.ca/v3/uploads/user_avatars/139122102045e603120b143.jpg)
|
Posted: 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 |
|
|
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: 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.
code: |
output.append("\nThis is a new line");
|
should work though. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
|
|