Ok, I am pretty new at Java. Started it a year ago when I started my Computer Science course in Uni and did some work for Sony in the Summer. I've not had much practice at GUI coding but have programmed since I was about 11 or 12 (about 7 or 8 years). So forgive me if this is incorrect anyway.
1) I do not believe that you have to declare a JTextField as a constant (final). Looking at an example piece of coursework (I got 6/7) my declaration is:
private JTextField txtKey;
And is later referred to using:
txtKey = new JTextField("1");
2) Again, on the topic of files and streams, I'm not EXACTLY sure of this, but I'll do my best. I believe that the reason that you must open a file and then send it into a stream before using the data is because the stream will input or output the data in pieces. This is something to do with if you have large pieces of data, then it ensures that it will be transferred correctly. I've just finished writing some of this stuff for my coursework:
Code:
private static Vector fileReader(String fileName) {
Vector output = new Vector();
try {
FileReader file = new FileReader(fileName);
BufferedReader reader = new BufferedReader(file);
String stringLine;
while((stringLine = reader.readLine()) != null) {
output.add(stringLine);
stringLine = new String();
}
reader.close();
return output;
} catch(Exception e) {
System.out.println("-- Decryption.fileReader() -> " + e);
return output;
}
}
private static void fileWriter(String fileName, Vector input) {
try {
FileWriter file = new FileWriter(fileName);
PrintWriter writer = new PrintWriter(file);
System.out.println(input.size());
Enumeration enumerate = input.elements();
while (enumerate.hasMoreElements()) {
writer.println((String) enumerate.nextElement());
}
writer.close();
} catch(Exception e) {
System.out.println("-- Decryption.fileWriter() -> " + e);
}
}
That will let you used the following to open a file and copy it to a different file using:
fileWriter("first.txt", fileReader("second.txt"));
Obviously, that has little use. So my program will manipulate the data in between. In case you're interested I'm writing a decryption program.
Hope this helps!
Bookmarks