Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Why wouldnt this work?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Chinmay




PostPosted: 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 Crying or Very sad 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
Sponsor
Sponsor
Sponsor
sponsor
HeavenAgain




PostPosted: 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
Chinmay




PostPosted: 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
HeavenAgain




PostPosted: 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
Chinmay




PostPosted: 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
HeavenAgain




PostPosted: 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
Tony




PostPosted: 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: