I am creating a little GUI for a "movie manager" with Java and Swing.
I have a class MovieTableModel that extends AbstractTableModel and has the data for the rows of the table in an Object[][]data.
A second class MovieUI manages the JFrame and well, the GUI in general.
The last class MovieManager is actually just a main function to create an instance of movieui and make it visible.
Now my problem is that by now, data is "saved" in my code. I want it to be able to be loaded and saved. If there is no save-file, one should be created and I should be able to add or delete rows of it (the actionlisteners are already set up, I just need a way to handle the file).
So the GUI looks like this:
MovieManager
This is kinda what I tried:
File tabledata = new File("tabledata.class");
if (!tabledata.exists()) {
try {
tabledata.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
tabledata.
FileInputStream in = new FileInputStream("tabledata.class");
ObjectInputStream input = new ObjectInputStream(in);
Object data = input.readObject();
System.out.print(data);
Both didn't work - I think I do understand how this works in general, but I don't know how to make it to the data in my table, especially as it is saved as an Object[][] but the file is an Object.
And if that works out - how can I add or delete single rows?
Thanks for your help in advance! :)
If you serialize you tablemodel or the object that backs your tablemodel, then you can read it later and restore it to the JTable.
When you read the object again from the ObjectInputStream, I think you are missing the cast to the right type you are saving:
FileInputStream in = new FileInputStream("tabledata.class");
ObjectInputStream input = new ObjectInputStream(in);
Object[][] data = (Object[][])input.readObject();
After you create new file you need to write the data using ObjectOutputStream and then read it with ObjectInputStream.
Object[][] data;
//save
File tabledata = new File("tabledata.dat"); //I wouldn't use class extension (class is for compiled Java)
//creation of file omitted
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(tabledata));
out.writeObject(data);
out.close();
//load
ObjectInputStream in = new ObjectInputStream(new FileInputStream(tabledata));
data = (Object[][]) in.readObject(); //explict cast required
in.close();
Because this stores all data as one big chunk I don't think it is possible to just read/write one element. You would have to resort to counting bytes of stored objects and then skip to right position. If you would really need to store huge amount of table data I would use some relational database as backend. Otherwise I wouldn't bother with such optimization. Just rewrite everything on save.
Related
I am given an assignment where we are not allowed to use a DB or libraries but only textfile for data storage.
But it has rather complex requirements, for e.g. many validations, because of that, we need to "access the db" (i.e. read the textfile) many times.
My question is: should I create a class like this:
class SomeRepository{
static ArrayList<Users> users = new ArrayList();
public SomeRepository(){
//instantiate this class on program load
//In constructor, we read the text file, instantiate and store everything inside the arraylist.
}
//public getOneUser(){ // for get methods, we don't read from text file at all }
/public save() { //text file saving code overhere }
}
Is this a good approach to solve the above problem? Currently, what we are doing is reading and writing to the text file every time we want to retrieve some data or write something new.
Wouldn't this be too expensive in terms of heap space memory? Or should I just read/write to the text file for every method?
public class IOManager {
public static void writeObjToTxtFile(String fileName, Object object) {
File file = new File(fileName + ".txt");//File will be created in the root directory where the program runs.
try (FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);) {
oos.writeObject(object);
} catch (IOException e) {
e.printStackTrace();
}
}
public static Object readObjFromTxtFile(String fileName) {
Object obj = null;
File file = new File(fileName + ".txt");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
obj = ois.readObject();
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
return obj;
}
}
Add this class to your project. Since it's general for all Objects, you can pass and receive Objects like these as well: ArrayList<Users>. Play around and Tinker with it to fit whatever your specific purpose is. Hint: You can write other custom methods that calls these methods. eg:
public static void writeUsersToFile(ArrayList<Users> usersArrayList){
writeObjToTxtFile("users",usersArrayList);
}
Ps. Make sure your Objects implement Serializable. Eg:
public class Users implements Serializable {
}
I would suggest reading the contents of your file to a dynamic list such as an arraylist at the start of your program. Make the required queries/changes to your arraylist and then write that arraylist to your file when the program is set to close. This will save significant time over repeated file reads/writes.
This isn't without it's drawbacks, though. You don't want to hogg up memory in case of very large files - but considering this is an assignment, that may not be the case. Additionally, should your program terminate prior to the write at the end, all changes made to your database during the current execution will be lost.
I am writing a small program to help with planning future workouts. I am nearly finished however saving and loading is giving me some trouble. The program works with a list of "ride"(a custom class) objects that hold a number of qualities (like a Dat, and then some ints and doubles)
right now, I have two methods, a "saver" and a "loader":
public void saver() {
try{ // Catch errors in I/O if necessary.
// Open a file to write to, named SavedObj.sav.
FileOutputStream saveFile=new FileOutputStream("SaveObj.sav");
// Create an ObjectOutputStream to put objects into save file.
ObjectOutputStream save = new ObjectOutputStream(saveFile);
// Now we do the save.
for (int x = 0; x < rides.size(); x++) {
save.writeObject(rides.get(x).getDate());
save.writeObject(rides.get(x).getMinutes());
save.writeObject(0);
save.writeObject(rides.get(x).getIF());
save.writeObject(rides.get(x).getTss());
}
// Close the file.
save.close(); // This also closes saveFile.
}
catch(Exception exc){
exc.printStackTrace(); // If there was an error, print the info.
}
}
public void loader() {
try{
// Open file to read from, named SavedObj.sav.
FileInputStream saveFile = new FileInputStream("SaveObj.sav");
// Create an ObjectInputStream to get objects from save file.
ObjectInputStream save = new ObjectInputStream(saveFile);
Ride worker;
while(save.available() > 0) {
worker = new Ride((Date)save.readObject(), (int)save.readObject(), (double)save.readObject(), (double)save.readObject(), (int)save.readObject());
addRide(worker.getDate(), worker.getMinutes(), 0, worker.getIF(), worker.getTss());
}
// Close the file.
save.close(); // This also closes saveFile.
}
catch(Exception exc){
exc.printStackTrace(); // If there was an error, print the info.
}
}
When I run the program, neither "save" nor "load" return any errors. A .sav file is created when one does not exist, and is edited each time the program is executed. Yet, the program never restores data from previous sessions. Please let me know if more information is required.
Thanks in advance for the help!
Don't use available() which returns the number of bytes that can be read without blocking. It doesn't mean what all bytes were read.
If your objects are never null, you could use Object readObject() to check if all data were read from the inputstream.
Date date = null;
while( (date = (Date)save.readObject()) != null) {
worker = new Ride(date, (int)save.readObject(), (double)save.readObject(), (double)save.readObject(), (int)save.readObject());
addRide(worker.getDate(), worker.getMinutes(), 0, worker.getIF(), worker.getTss());
}
Otherwise if read values may be null, you could serialize directly the Ride object or a class containing all fields to serialize rather than unitary fields which could be null With this, the check to know if all data were read with Object readObject() could work.
Do not use available() as a condition. It just tells you whether there is some byte available for immediate reading without any delay, it does not mean the stream has reached its end.
Also you should maybe add a BufferedInputStream and BufferedOutputStream between the Object and File streams, that's almost always a good idea.
To solve your issue you could e. g. first write an integer in the save method that tells you how many objects are in the file and on load read that integer and then make a simple for loop with this amount.
Or you could throw in a PushbackInputStream in the row and then as EOF check use its read() method. It will return -1 on EOF and you can abort reading. If it returns anything else, you unread() the read byte and use the ObjectInputStream that you placed on top.
Before I proceed to my question : please note that I am not working on any client-server application that would require serialization, but the program I am trying to customize stores one big instance of one big class in a .dat file. I have read about this issue (memory leak in ObjectOutputStream and ObjectInputStream)and the fact that I could probably need to :
use the ObjectOutputStream.reset() method after writing the class instance in the .dat file, so that it doesn't hold the reference anymore;
re-write the code without using serialization;
split the file and read it in chunks;
change the JVM memory parameter by using -Xmx;
So, I was provided with one class that generates a language model and saves it with a .dat extension; the code was probably optimized for small model files (there are 2 model files provided as examples, both around 10MB ), but I generated a much larger model class, and it is around 40MB. Then, there is another class in another folder, totally independent on the first one, that uses this model, and the model has to be loaded using ObjectInputStream. Here comes the problem : a classic "OutOfMemoryError : Java heap space".
Writing the object:
try {
// Create an output stream to the file.
FileOutputStream file_output = new FileOutputStream (file);
ObjectOutputStream o = new ObjectOutputStream( file_output );
o.writeObject(this);
file_output.close ();
}
catch (IOException e) {
System.err.println ("IO exception = " + e );
}
Reading the object:
InputStream model = null;
ModelGeneration oRead = null;
ObjectInputStream p = null;
try {
model = new FileInputStream(filename);
BufferedInputStream buf = new BufferedInputStream(model);
p = new ObjectInputStream(buf);
oRead = (ModelGeneration) p.readObject();
p.reset();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
model.close();
} catch (Exception e) {
e.printStackTrace();
}
}
I tried to use the reset() method, but it is useless because we load only one instance of one class at a time, nothing else needed. This is why I can't split the file, too: only one class instance is stored in the .dat file.
Changing the heap space seems like a worse solution than optimizing the code.
I would really appreciate your advice on what I can do.
Btw the code is here : http://svn.apache.org/repos/asf/uima/addons/trunk/Tagger/, I only implemented the required classes for a different language.
P.S. Works fine if I create a smaller model, but I would prefer the bigger one.
I'm working on an Android app and I got to a point where I need to add some Store objects to an ArrayList favoriteStores. The problem is that I want this list to persist after closing the application, because my list of favorite stores must stay there until I chose to delete particular items inside it. Anyone got any idea what type of implementation I might use? Thanks in advance,
If you don't want to save arraylist to database, you can save it to file. It is a great way if you just want to save arraylist and don't want to touch sqlite.
You can save arraylist to file with this method
public static <E> void SaveArrayListToSD(Context mContext, String filename, ArrayList<E> list){
try {
FileOutputStream fos = mContext.openFileOutput(filename + ".dat", mContext.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(list);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
And you can read that saved file to arraylist with this method
public static Object ReadArrayListFromSD(Context mContext,String filename){
try {
FileInputStream fis = mContext.openFileInput(filename + ".dat");
ObjectInputStream ois = new ObjectInputStream(fis);
Object obj= (Object) ois.readObject();
fis.close();
return obj;
} catch (Exception e) {
e.printStackTrace();
return new ArrayList<Object>();
}
}
Hope this help.
You can either use a database like SQLite (howto here) or use some serialization technique. There is a related question to serialization here.
General information about storing data in Android can be found here.
Here's a nice post about data storage options in Android, read it carefully and select the option that you find the most appropriate.
[I'm quite new with Android programming so please excuse me for my nooby questions]
I'm developing a dictionary app. One of this app's feature is the Favourite button which allows user to save favourite words (short-click) and view the list of favourite words (long-click).
So far, I have succeeded in saving words into a text file (myfav.txt). The format of the content of the text file is as below (each item on a line):
A
B
C
...
Z
However, I have problem in loading and viewing this file inside my app. I'm thinking of using ListView to display the content of "myfav.txt" but I don't really know what to do. I have consulted the Qs & As from other similar posts here but found myself more confused as a result.
Therefore, my questions are:
How can I load content of "myfav.txt" and display it using ListView? Could you please give detailed instructions as for beginners?
Are there any better ways to do view the content of "myfav.txt" other than ListView?
Here is my code:
//Reading lines from myfav.txt
btnAddFavourite.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
File sdcard = Environment.getExternalStorageDirectory();
setContentView(R.layout.text_view);
//trying opening the myfav.txt
try{
File f = new File(sdcard,"myfolder/myfav.txt");
InputStream fileIS = new FileInputStream(f);
BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
String readString = new String();
while((readString = buf.readLine())!= null){
Log.d("Content: ", readString);
//How to code to load/view the content of "myfav.txt"
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
return false;
}
});
Thank you very much indeed.
Hi you can find useful example here.
I do not think it's a good idea to use ListView for it. You'll need to provide ListAdapter if you choose this way.
I'd recommend using TextView (if you don't need to edit your text) or EdiText (if you do)
Using a file to save such information is not too sophisticated. I think you should look into tutorials about using SQLite, so you can store the words in a databse, and use cursors to view them in ListViews. You could use a separate boolean coloumn in you schema to mark favourited words that way for example.
Anyways, if you want to stick with files, one solution would be:
Read the contents of the file into a String array. You can use e.g. the Scanner class to easily read in lines from the file, and store them as separate strings in this array.
Construct a simple ArrayAdapter adapter class using this array.
Assign this adapter to a ListView.
Profit.
I am using following for writing to file -
FileOutputStream fout = null;
try {
fout = openFileOutput(fileName, MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(wordList); //writing arraylist<T>
oos.flush();
} catch(IOException e)
{
e.printStackTrace();
}
For reading -
fin = openFileInput(fileName);
ObjectInputStream ois = new ObjectInputStream(fin);
list =(ArrayList<T>)ois.readObject(); //reading in arraylist directly