ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Let me now include something that i didnt say before. I am also trying to make my records (the Store class) to be read in a kind of circular way, meaning that when someone presses the next record button the next record will be displayed and if the counter has reached the maximun should start from zero.
Therefore i have set in my App class a
Quote:
private static int CQCounter = 0;
private static final int MAXSTORE=10;
My first problem while trying to make this work was that the following class which is also included in my App class
Quote:
private int checkReset(int x)
{
return x % MAXStore;
}
I know this algo from data structures. Unfortunately that was not working and i tried to put some other code in my actionPerformed method which finally worked.
Any ideas why?
I'm not quire sure what you're asking here, but when you say a "circular pattern", is each record stored in its own file?
Also, check the syntax; by Java 5.0 Standard (I happen to have the language conventions right next to me), MAXSTORE is properly defined - all constants, as denoted by the "final" declaration, should be capitalized. However, when you're referencing it in checkReset(), you have it spelled out MAXStore. Try changing it to MAXSTORE in checkReset(), and see if that fixes anything.
Ok, back again to the problem that i can not write to file. I just want to ask how this could be possible as soon the Store class implements a Serializable interface. The following is how i save to the file. Can you identify anything that could causing a problem to write to the file?
Dear Sir indienick
Dont know how to thank u for all of your replies. Thanks a lot.
I 've got something new . I just had a look again to the file that i am trying to write. I open the file with a text editor like 'kwrite'. The fact is that i can see the information that i have stored. Nothing seems wrong. On the other hand i still cant re-open the file after a later execution of my program neither your program returns any output.
Something also that i have noticed is that when i first created the text file after execution of my program when i tried to open the file it asks me always with which program to open. I then choose a text editor like kwrite.
Once more obliged if i have your opinion
Thanks for sharing you knowledge and your time.
manolakis
You are very welcome, manolakis.
That's what I'm here for.
Well, let's see...if you can open the file and view all the contents - and they're showing up correctly - the writing part is working just fine.
Now, the reading part is what's throwing us flak.
If your solution, and my solution don't give us what you want to see - and I do know that both of our solutions do work (I've tested them) - it could be a problem with the Java compiler that you're using (which while a very legal culprit, we'll dismiss it for now), it could also be the way you are handling the I/O of the file itself.
1. How are you creating the file?
--My suggestion would be to use the createNewFile() method, which comes from the java.io.File class.
2. Are you closing the output stream once you are done with it?
--Using the close() method, which is inherent from the java.io.PrintStream class.
--Although Java does run in a VM - and this could be a totally bogus stream of thought - if you aren't using the close() method when you're done with the file, or before you issue System.exit(n), an outbound stream may be "remembered" by the VM, and upon the next execution of the program, the VM may try to re-assign the stream to the program as an outbound stream.
Moreso, on the second point for the second question, I highly doubt you can have a single file being acted upon by both an in-stream and an out-stream - I/O conflicts may arise. Try explicity closing any directional streams on a file before performing a stream action in the opposite direction. Java may have a class to handle bi-directional streams, and I'll look it up in the meantime. But until then, make sure you explicitly open and close all I/O streams.
EDIT: I just have to ask, are you using SUN Java (preferred), or GCJ (GNU Java)?
Could you please give me a short example explaining how to close the files explicity.
I am just wondering that if closing the files could cause the problem if i wont try to close them then it should be ok. Is that right?
Hey again
Well, I am glad to say that i made my gui working with other classes for reading and writing to files (BufferedWriter,BufferedReader).
Now I would like once more again your precious help on another issue.
I want to make the gui working for a Multi-Server. What I have planned to do
is to put the store class into the Server class as soon it should hold the only copy and the GUI into the Client class. The Clients should be able to send packets ('Employees' in my case)to the Server and then the Server should store that information.
I know that to do that i need to put things in a thread. I have also found enough src codes on the web of multi-servers but i find my self quite insecure on how to do that
Thanks for your time and for sharing your knowledge
I'm sorry about the lack of response lately, but I've been out of town for Spring Break.
I'm glad you got the data storage method(s) working.
What I mean by when I say "explicitly closing streams" is using the java.io.PrintStream.close() method (for output), and java.util.Scanner.close() (for input). It's a simple practice, and while it means an extra line of code here and there, it's not hurting anyone, and nearly completely safe-guarding your thread interactions.
Now, funny you should ask about transmitting data - because I'm actually doing the same thing right now, with Java. I've deduced it to this:
1. You need to use either java.net.Socket - or java.net.SSLSocket - for the connection to the server.
2. You need to make use of an outstream class (like java.io.PrintStream) for sending data over the socket.
3. You need to have a listener on that socket in case the server is transmitting data back to you. (In this case, you would need to design your program to act as a server, which could make your job much harder than it has to be.)
4. Encryption/decryption algorithms: to tighten up your data and keep it safe from prying eyes. (This is completely optional, and more than likely not needed if you're just doing this as a mini-project with no sensitive information being sent, or if you decide to utilize the SSLSocket class. Also, if you are using cryptographic algorithms that Java does not supply, make sure your remote system can the properly methods and algorithms to decrypt your data.)
You would only need to use a java.lang.Thread class if - and only if - you wished to do concurrent processing. The only time I could think to make use of a Thread class is if you are sending more than one set of data to more than one remote location.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.