LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Java GUIs Input&OutputObjectStreams problem (https://www.linuxquestions.org/questions/programming-9/java-guis-input-and-outputobjectstreams-problem-535124/)

manolakis 03-06-2007 01:04 PM

Java GUIs Input&OutputObjectStreams problem
 
Hey there
I am trying to make a GUI being able to read and write records.
Ok, now suppose the following.
Quote:

public abstract class Person implements Serializable
{
protected String name;
public Person(String nam)
{
name=nam;
}

public String getName()
{
return name;
}
//end of Person Class
}
Quote:

public class Employee extends Person
{
protected String id;

public Employee(String nam,String id)
{
super(nam);
this.id=id;
}
}
Quote:

public class Store
{
Person [] list;

public Store(int size)
{
list = new Person [size];
}
}
Quote:

public class GUI extends JPanel
{
//I wont put detail on this. Just imagine a GUI with 2 private Jtextfields and a number //of buttons for saving or loading records
}
Quote:

// And here comes the problem
public class App extends JFrame
{
private GUI ourGui;
private InputObjectStream input;
private OutputObjectStream output;
private Store storage;
private Employee emp;

public App
{
ourGUI = new GUI();
storage = new Store(10);
//..................
//..................
//..................
}
}
Well, the case is that i dont get any compilation error and when i try to write to a file or load a file i dont get any exception warnings. The thing is that i know that something goes bad with this because when i exit the program and restart it when i try to load a previous saved store a get a boolean isEmpty() fuction which is not included in the Store class returns true. So therefore no writing to a file took place.
I didnt put the whole code of the program because is really big. Dont know if that tells you something. Can anybody imagine what could go wrong?

Many thanks for your time
manolakis.

indienick 03-06-2007 08:18 PM

Try using a conviluted method of checking the presence of data in the file (it's really backwards, but will definitely override the isEmpty() method.
Code:

java.util.Scanner istream = new java.util.Scanner(new File("/path/to/file"));
java.util.ArrayList buf = new java.util.ArrayList();

while (istream.hasNext() == true) buf.add(istream.nextLine());

for (int i = 0; i < buf.size(); i++) System.out.println(buf.get(i));

Now, the problem could just as easily be with the structure you're using to write to the file - let me/us know if you're doing something similar:
Code:

try {
  java.io.File outf = new java.io.File("/path/to/file");
  outf.createNewFile();

  java.io.PrintStream ostream = new java.io.PrintStream(outf);
  // CODE
  // TO HANDLE
  // THE DATA
  // TO BE
  // WRITTEN TO
  // FILE
} catch (java.io.IOException ioex) {
  System.out.println("uh-oh");
  System.exit(1);
}


manolakis 03-07-2007 05:30 PM

Hey there. Thanks for your reply
Ok, the way that i write to the file looks like:
Quote:

JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY );

int result = fileChooser.showSaveDialog( this );

if ( result == JFileChooser.CANCEL_OPTION )
return;

File fileName = fileChooser.getSelectedFile();
if ( fileName == null || fileName.getName().equals( "" ) )
JOptionPane.showMessageDialog( this, "Invalid File Name",
"Invalid File Name", JOptionPane.ERROR_MESSAGE );
else {
try {
output = new ObjectOutputStream( new FileOutputStream(fileName ) );
output.writeObject(storage);
output.flush();
}
catch ( IOException ioException ) {
JOptionPane.showMessageDialog( this, "Error Opening File",
"Error", JOptionPane.ERROR_MESSAGE );
}
That comes from my private void method 'saveToFile'. which is executed when the save button is pressed

indienick 03-07-2007 08:14 PM

Have you tried doing this with just a basic PrintStream?

manolakis 03-07-2007 08:40 PM

The case is that I am restricted to use InputObjectStream.
I tried to compile the first piece of code that you gave but i got some errors

manolakis 03-07-2007 08:41 PM

or OutputObjectStream better

indienick 03-07-2007 08:45 PM

The first chunk of code gave you an error?! Weird...because I lifted that - directly - from a project of mine, and I know that code works. *shrug*

Are you using SUN Java, GCJ or another 3rd-party Java implementation?

manolakis 03-09-2007 05:09 AM

Sorry about my last post. I was really in a hurry and i didnt state things well
The fact is that i dont get error but i get some warnings for unsafe expressions
Quote:

BT Desktop $ javac x.java
Note: x.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
BT Desktop $ javac x.java -Xlint:unchecked
x.java:11: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
while (istream.hasNext() == true) buf.add(istream.nextLine());
^
1 warning
If i am right this should have been compiled. When i tried to run it didnt return anything so ideally the file where i am trying to store things should be empty.

indienick 03-09-2007 11:04 PM

Ah! You are more than likely using a Java 5.0 (1.5) version compiler - I am using a 1.6 compiler - and for whatever reason, parameterized types were removed for java.util.ArrayList (as far as I know - there may be more) class.

To kill those warnings, try the declaration like so:
Code:

...
java.util.ArrayList<String> buf = new java.util.ArrayList<String>();
...


manolakis 03-10-2007 10:34 AM

Ok that now works. I dont get any compilation error or warnings
Unfortunately when i execute i dont get any output
Any suggestion?

Many thanks

indienick 03-10-2007 06:18 PM

Well, that's proving that the file's empty. Try filling the text file with a few lines of whatever.

manolakis 03-10-2007 06:57 PM

ok I found no problem

indienick 03-10-2007 09:57 PM

Sweet deals. :)

Now, if you incorporate that to override the use of the isEmpty() function, does it work at all, or is the file still coming up as being empty (after data is being written to it)?

manolakis 03-11-2007 10:29 AM

ok, please let me take the chance to ask something
Lets say that we open a file, we write to it but we dont close that file and we exit (by using System.exit( 0 ); ).
Could this cause a problem?

indienick 03-11-2007 12:30 PM

If we were using a language such as C or C++, then I would say yes. But since Java runs in a VM (Virtual Machine), if we call System.exit(0), more than likely, the VM will call File.close() if there are any live out-streams.


All times are GMT -5. The time now is 03:29 PM.