Author |
Message |
GUImaster
|
Posted: Wed Jul 07, 2010 8:40 pm Post subject: Making a program for parents..need help. |
|
|
Im making a program for my parents...and i got stuck. a part of what i need to do is
**Using Netbeans 6.8**
*Needs Button*
*Needs TextField*
---------------------------------------------------------------------------------
Check To see if "input.txt" is empty
if "input.txt" is empty then
write "This is Header"
write "----------------"
write (Text in TextField)
if "input.txt" isnt empty then
append at end of file (Text in TextField)
--------------------------------------------------------------------------------
i had a basic idea of what i needed to do but it didnt work out for me
Thanks in Advance
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Zren
|
Posted: Wed Jul 07, 2010 9:08 pm Post subject: Re: Making a program for parents..need help. |
|
|
Here's some code snippets to point you in a general direction of File Input/Output. Buttons are rather easy in netbeans so that shouldn't be the problem. If ya need more details just ask.
To find if the file exist (Copy n Paste, this forum doesn't like the brackets at the end. Or just look for exists yourself): http://download.oracle.com/docs/cd/E17476_01/javase/1.4.2/docs/api/java/io/File.html#exists()
File Size:
Java: | File f = new File("blarg.txt");
long file_size = f. length(); |
For writing to files: [Source]
Java: |
import java.io.*;
class FileWrite {
public static void main (String args []) {
try {
// Create file
FileWriter fstream = new FileWriter("out.txt");
BufferedWriter out = new BufferedWriter(fstream );
//Trick: BufferedWriter out = new BufferedWriter( new FileWriter("out.txt") );
out. write("Hello Java");
//Close the output stream
out. close();
} catch (Exception e ) { //Catch exception if any
System. err. println("Error: " + e. getMessage());
}
}
}
|
Don't forget to close or flush the bufferedwriter or nothing will write to file. |
|
|
|
|
|
GUImaster
|
Posted: Wed Jul 07, 2010 9:49 pm Post subject: Re: Making a program for parents..need help. |
|
|
i have import.java.io.*;
and this code happens when the button is pressed
code: | try {
fis = new FileInputStream("input.txt");
in = new DataInputStream(fis);
temp = in.readLine();
in.close();
fwriter5 = new FileWriter("input.txt", true);
bfwriter2 = new BufferedWriter(fwriter5);
if (temp.compareTo(" ")==0) {
bfwriter2.write("Hello");
bfwriter2.write("------------------");
bfwriter2.write(jTextField1.getText());
} else {
// bfwriter2.write(jTextField1.getText());
}
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
|
thats what i have after using netbeans gui bulder... but it doesnt work for me |
|
|
|
|
|
Zren
|
Posted: Wed Jul 07, 2010 10:04 pm Post subject: RE:Making a program for parents..need help. |
|
|
Did you clooooo~se the buffered writer? |
|
|
|
|
|
GUImaster
|
Posted: Wed Jul 07, 2010 10:06 pm Post subject: Re: Making a program for parents..need help. |
|
|
i dont exactly know where to close it....the try catch things always throw me off
[Update] I closed it in the if statement and i got the same error}
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException |
|
|
|
|
|
GUImaster
|
Posted: Wed Jul 07, 2010 10:20 pm Post subject: Re: Making a program for parents..need help. |
|
|
I got it working now...Thanks for the help
i changed the if (Temp.compareTo(" ")==0) to if (temp == null) |
|
|
|
|
|
|