I am trying to make a saving/loading of settings for my GUI.
So far, I've got saving, using Properties:
Properties prop = new Properties();
prop.setProperty("a", Boolean.valueOf(boxRandomMouse.isSelected()).toString());
prop.setProperty("b", listModelPath.toString());
The problem I've got here, is that I am saving listModelPath of my JList into String:
public static DefaultListModel<String> listModelPath;
In my file under property b. I can't find any way to load back and parse this listModelPath into my desired type, which is Position.
The class, Position, can be initialised with below constractor:
Position newPos = new Position(int x, int y, int z);
or for array:
public Position[] toTown = new Position[] {
new Position(3094, 3491, 0), new Position(3088, 3487, 0),
new Position(3080, 3475, 0)};
This is how the String of listModelPath looks when It's been saved into the file
c=[[x\=3094, y\=3491, z\=0], [x\=3092, y\=3491, z\=0], [x\=3090, y\=3491, z\=0], [x\=3088, y\=3490, z\=0], [x\=3086, y\=3488, z\=0], [x\=3084, y\=3486, z\=0], [x\=3082, y\=3484, z\=0], [x\=3081, y\=3482, z\=0], [x\=3080, y\=3481, z\=0], [x\=3080, y\=3479, z\=0], [x\=3080, y\=3477, z\=0], [x\=2045, y\=5194, z\=0], [x\=2044, y\=5192, z\=0], [x\=2042, y\=5190, z\=0], [x\=2041, y\=5189, z\=0]]
I need to figure out a way to parse the String above, into my Positions, or somehow find a way to save this array to a File, which I honestly have no idea how to do.
I've tried doing this numerously on different ways, always failing, so that's why I'm here.
Hopefully you guys knows how to handle a situation I have here.
PS. I have just started reading about serialization, perhaps it is the way to go? Would it allow me to save the array I have dynamically created in the application in the file without converting it to string and back from string?
You can serialize your listModelPath object sending it then to file as an Object which will then let you read it as an Object then cast it back to its real Class.
This can be done by chaining an ObjectOutputSteam to an underlying stream such as a FileOutputStream then writing the Obejet to it (Position[])
public void writePositions() throws IOException, ClassNotFoundException {
File file = new File("positions.ser");
FileOutputStream outFile = new FileOutputStream(file);
ObjectOutputStream outStream = new ObjectOutputStream(outFile);
public Position[] toTown = new Position[] {
new Position(3094, 3491, 0),
new Position(3088, 3487, 0),
new Position(3080, 3475, 0)};
outStream.writeObject(toTown);
outStream.close();
outFile.close();
}
You can the read your serialized array of Position back from the file it was written to, positions.ser like below:
public void readPositions() throws IOException, ClassNotFoundException {
File file = new File("positions.ser");
FileInputStream inFile = new FileInputStream(file);
ObjectInputStream inStream = new ObjectInputStream(inFile);
Position[] toTown = (Position[]) inStream.readObject());
for(int i = 0; i < toTown.length; i++) {
System.out.println(toTown[i]);
}
inStream.close();
inFile.close();
file.delete();
}
You should note that the Position class should be Serializable, otherwise the JVM won't let you send any object of this type back and forth to an in memory file or even through any stream.
I'm not really sure if I have well understood the main matter concerning the listModelPath and how it relates to the Position array but you shoul provide more informations, especially how you are creating the listModelPath instance.
Edit (Save many objects with different types):
You can, not only save one Object, but as many as you want and wathever type they are. When loading back the objects to the JVM, they should be read in the same order they were written in to avoid a ClassCastException:
public void writePositions() throws IOException, ClassNotFoundException {
...
public Position[] toTown = new Position[] {
new Position(3094, 3491, 0),
new Position(3088, 3487, 0),
new Position(3080, 3475, 0)};
outStream.writeObject(toTown); //Write the array of Position
boolean selected = boxRandomMouse.isSelected();
outStream.writeObject(selected); //Write the Boolean Object
...
}
public void readPositions() throws IOException, ClassNotFoundException {
...
Position[] toTown = (Position[]) inStream.readObject()); //Read the array of Position which was written first
for(int i = 0; i < toTown.length; i++) {
System.out.println(toTown[i]);
}
boolean selected = (boolean) inStream.readObject(); //Read the Boolean value which was written in the second order
// And so on
...
}
You can only have Strings in property files, so you have to find a way to convert listModelPath in a inversible way. I think that you could try json (Gson or Jackson) whose job is to serialize-deserialize objects to-from String.
I can do no more without source of your Position class.
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.
EDIT (for the sake of confusion): null has been written into the files "abc" and "efg".
After running the following code, the contents of file "abc" change which were initially null , and I get EOFException in every next execution :
ObjIStream = new ObjectInputStream(new FileInputStream("abc"));
M[][] objs = (M[][]) ObjIStream.readObject();
FS.objs = objs;
ObjIStream.close();
Here, FS.objs is a static member of class FS of type M[][] type.
On the other hand, this one has no effect on the file and I don't get any Exceptions after any number of executions:
ObjIStream = new ObjectInputStream(new FileInputStream("abc"));
M[][] objs = (M[][]) ObjIStream.readObject();
ObjIStream.close();
EDIT: I just found the trouble that exists in class FS in this form:
static{
try {
ObjOStream = new ObjectOutputStream(new FileOutputStream("abc"));
ObjOStream.close();
ObjOStream = new java.io.ObjectOutputStream(new java.io.FileOutputStream("efg"));
ObjOStream.close();
}
catch (IOException ex) { }
}
How is it troubling anyways?
The problem is new FileOutputStream("abc") itself, which means new FileOutputStream("abc", false). It cleans up all the data in file because you are not going to append anything. It calls FileOutputStream.open(String name, boolean append) which is a private native function. It erases everything in file in overwrite mode.
I'm doing an animation in Processing. I'm using random points and I need to execute the code twice for stereo vision.
I have lots of random variables in my code, so I should save it somewhere for the second run or re-generate the SAME string of "random" numbers any time I run the program. (as said here: http://www.coderanch.com/t/372076/java/java/save-random-numbers)
Is this approach possible? How? If I save the numbers in a txt file and then read it, will my program run slower? What's the best way to do this?
Thanks.
If you just need to be able to generate the same sequence for a limited time, seeding the random number generator with the same value to generate the same sequence is most likely the easiest and fastest way to go. Just make sure that any parallel threads always request their pseudo random numbers in the same sequence, or you'll be in trouble.
Note though that there afaik is nothing guaranteeing the same sequence if you update your Java VM or even run a patch, so if you want long time storage for your sequence, or want to be able to use it outside of your Java program, you need to save it to a file.
Here is a sample example:
public static void writeRandomDoublesToFile(String filePath, int numbersCount) throws IOException
{
FileOutputStream fos = new FileOutputStream(new File(filePath));
BufferedOutputStream bos = new BufferedOutputStream(fos);
DataOutputStream dos = new DataOutputStream(bos);
dos.writeInt(numbersCount);
for(int i = 0; i < numbersCount; i++) dos.writeDouble(Math.random());
}
public static double[] readRandomDoublesFromFile(String filePath) throws IOException
{
FileInputStream fis = new FileInputStream(new File(filePath));
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
int numbersCount = dis.readInt();
double[] result = new double[numbersCount];
for(int i = 0; i < numbersCount; i++) result[i] = dis.readDouble();
return result;
}
Well, there's a couple of ways that you can approach this problem. One of them would be to save the random variables as input into a file and pass that file name as a parameter to your program.
And you could do that in one of two ways, the first of which would be to use the args[] parameter:
import java.io.*;
import java.util.*;
public class bla {
public static void main(String[] args) {
// You'd need to put some verification code here to make
// sure that input was actually sent to the program.
Scanner in = new Scanner(new File(args[1]));
while(in.hasNextLine()) {
System.out.println(in.nextLine());
}
} }
Another way would be to use Scanner and read from the console input. It's all the same code as above, but instead of Scanner in = new Scanner(new File(args[1])); and all the verification code above that. You'd substitute Scanner in = new Scanner(System.in), but that's just to load the file.
The process of generating those points could be done in the following manner:
import java.util.*;
import java.io.*;
public class generator {
public static void main(String[] args) {
// You'd get some user input (or not) here
// that would ask for the file to save to,
// and that can be done by either using the
// scanner class like the input example above,
// or by using args, but in this case we'll
// just say:
String fileName = "somefile.txt";
FileWriter fstream = new FileWriter(fileName);
BufferedWriter out = new BufferedWriter(fstream);
out.write("Stuff");
out.close();
}
}
Both of those solutions are simple ways to read and write to and from a file in Java. However, if you deploy either of those solutions, you're still left with some kind of parsing of the data.
If it were me, I'd go for object serialization, and store a binary copy of the data structure I've already generated to disk rather than having to parse and reparse that information in an inefficient way. (Using text files, usually, takes up more disk space.)
And here's how you would do that (Here, I'm going to reuse code that has already been written, and comment on it along the way) Source
You declare some wrapper class that holds data (you don't always have to do this, by the way.)
public class Employee implements java.io.Serializable
{
public String name;
public String address;
public int transient SSN;
public int number;
public void mailCheck()
{
System.out.println("Mailing a check to " + name
+ " " + address);
}
}
And then, to serialize:
import java.io.*;
public class SerializeDemo
{
public static void main(String [] args)
{
Employee e = new Employee();
e.name = "Reyan Ali";
e.address = "Phokka Kuan, Ambehta Peer";
e.SSN = 11122333;
e.number = 101;
try
{
FileOutputStream fileOut =
new FileOutputStream("employee.ser");
ObjectOutputStream out =
new ObjectOutputStream(fileOut);
out.writeObject(e);
out.close();
fileOut.close();
}catch(IOException i)
{
i.printStackTrace();
}
}
}
And then, to deserialize:
import java.io.*;
public class DeserializeDemo
{
public static void main(String [] args)
{
Employee e = null;
try
{
FileInputStream fileIn =
new FileInputStream("employee.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
e = (Employee) in.readObject();
in.close();
fileIn.close();
}catch(IOException i)
{
i.printStackTrace();
return;
}catch(ClassNotFoundException c)
{
System.out.println(.Employee class not found.);
c.printStackTrace();
return;
}
System.out.println("Deserialized Employee...");
System.out.println("Name: " + e.name);
System.out.println("Address: " + e.address);
System.out.println("SSN: " + e.SSN);
System.out.println("Number: " + e.number);
}
}
Another alternative solution to your problem, that does not involve storing data, is to create a lazy generator for whatever function that provides you your random values, and provide the same seed each and every time. That way, you don't have to store any data at all.
However, that still is quite a bit slower (I think) than serializing the object to disk and loading it back up again. (Of course, that's a really subjective statement, but I'm not going to enumerate cases where that is not true). The advantage of doing that is so that it doesn't require any kind of storage at all.
Another way, that you may have not possibly thought of, is to create a wrapper around your generator function that memoizes the output -- meaning that data that has already been generated before will be retrieved from memory and will not have to be generated again if the same inputs are true. You can see some resources on that here: Memoization source
The idea behind memoizing your function calls is that you save time without persisting to disk. This is ideal if the same values are generated over and over and over again. Of course, for a set of random points, this isn't going to work very well if every point is unique, but keep that in the back of your mind.
The really interesting part comes when considering the ways that all the previous strategies I've described in this post can be combined together.
It'd be interesting to setup a Memoizer class, like described in the second page of 2 and then implement java.io.Serialization in that class. After that, you can add methods save(String fileName) and load(String fileName) in the memoizer class that make serialization and deserialization easier, so you can persist the cache used to memoize the function. Very useful.
Anyway, enough is enough. In short, just use the same seed value, and generate the same point pairs on the fly.
Please tell me how to append data in docx file using java and docx4j.
What I'm doing is, I am using a template in docx format in which some field are dilled by java at run time,
My problem is for every group of data it creates a new file and i just want to append the new file into 1 file. and this is not done using java streams
String outputfilepath = "e:\\Practice/DOC/output/generatedLatterOUTPUT.docx";
String outputfilepath1 = "e:\\Practice/DOC/output/generatedLatterOUTPUT1.docx";
WordprocessingMLPackage wordMLPackage;
public void templetsubtitution(String name, String age, String gender, Document document)
throws Exception {
// input file name
String inputfilepath = "e:\\Practice/DOC/profile.docx";
// out put file name
// id of Xml file
String itemId1 = "{A5D3A327-5613-4B97-98A9-FF42A2BA0F74}".toLowerCase();
String itemId2 = "{A5D3A327-5613-4B97-98A9-FF42A2BA0F74}".toLowerCase();
String itemId3 = "{A5D3A327-5613-4B97-98A9-FF42A2BA0F74}".toLowerCase();
// Load the Package
if (inputfilepath.endsWith(".xml")) {
JAXBContext jc = Context.jcXmlPackage;
Unmarshaller u = jc.createUnmarshaller();
u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler());
org.docx4j.xmlPackage.Package wmlPackageEl = (org.docx4j.xmlPackage.Package) ((JAXBElement) u
.unmarshal(new javax.xml.transform.stream.StreamSource(
new FileInputStream(inputfilepath)))).getValue();
org.docx4j.convert.in.FlatOpcXmlImporter xmlPackage = new org.docx4j.convert.in.FlatOpcXmlImporter(
wmlPackageEl);
wordMLPackage = (WordprocessingMLPackage) xmlPackage.get();
} else {
wordMLPackage = WordprocessingMLPackage
.load(new File(inputfilepath));
}
CustomXmlDataStoragePart customXmlDataStoragePart = wordMLPackage
.getCustomXmlDataStorageParts().get(itemId1);
// Get the contents
CustomXmlDataStorage customXmlDataStorage = customXmlDataStoragePart
.getData();
// Change its contents
((CustomXmlDataStorageImpl) customXmlDataStorage).setNodeValueAtXPath(
"/ns0:orderForm[1]/ns0:record[1]/ns0:name[1]", name,
"xmlns:ns0='EasyForm'");
customXmlDataStoragePart = wordMLPackage.getCustomXmlDataStorageParts()
.get(itemId2);
// Get the contents
customXmlDataStorage = customXmlDataStoragePart.getData();
// Change its contents
((CustomXmlDataStorageImpl) customXmlDataStorage).setNodeValueAtXPath(
"/ns0:orderForm[1]/ns0:record[1]/ns0:age[1]", age,
"xmlns:ns0='EasyForm'");
customXmlDataStoragePart = wordMLPackage.getCustomXmlDataStorageParts()
.get(itemId3);
// Get the contents
customXmlDataStorage = customXmlDataStoragePart.getData();
// Change its contents
((CustomXmlDataStorageImpl) customXmlDataStorage).setNodeValueAtXPath(
"/ns0:orderForm[1]/ns0:record[1]/ns0:gender[1]", gender,
"xmlns:ns0='EasyForm'");
// Apply the bindings
BindingHandler.applyBindings(wordMLPackage.getMainDocumentPart());
File f = new File(outputfilepath);
wordMLPackage.save(f);
FileInputStream fis = new FileInputStream(f);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
try {
for (int readNum; (readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum);
}
// System.out.println( buf.length);
} catch (IOException ex) {
}
byte[] bytes = bos.toByteArray();
FileOutputStream file = new FileOutputStream(outputfilepath1, true);
DataOutputStream out = new DataOutputStream(file);
out.write(bytes);
out.flush();
out.close();
System.out.println("..done");
}
public static void main(String[] args) {
utility u = new utility();
u.templetsubtitution("aditya",24,mohan);
}
thanks in advance
If I understand you correctly, you're essentially talking about merging documents. There are two very simple approaches that you can use, and their effectiveness really depends on the structure and onward use of your data:
PhilippeAuriach describes one approach in his answer, which entails
appending all components within a MaindocumentPart instance to
another. In terms of the final docx file, this means the content
that appears in document.xml -- it won't take into account headers
and footers ( for example), but that may be fine for you.
You can insert multiple documents into a single docx file by inserting them
as AltChunk elements (see the docx4j documentation). This will
bring everything from one Word file into another, headers and all.
The downside of this is that your final document won't be a proper
flowing Word file until you open it and save it in MS Word itself
(the imported components remain as standalone files within the docx
bundle). This will cause you issues if you want to generated
'merged' files and then do something with them like render PDFs --
the merged content will simply be ignored.
The more complete (and complex) approach is to perform a "deep merge". This updates and maintains all references held within a document. Imported content becomes part of the main "flow" of the document (i.e. it is not stored as separate references), so the end result is a properly-merged file which can be rendered to PDF or whatever.
The downside to this is you need a good knowledge of docx structure and the API, and you will be writing a fair amount of code (I would recommend buying a license to Plutext's MergeDocx instead).
I had to deal with similar things, and here is what I did (probably not the most efficient, but working) :
create a finalDoc loading the template, and emptying it (so you have the styles in this doc)
for each data row, create a new doc loading the template, then replace your fields with your values
use the function below to append the doc filled with the datas to the finalDoc :
public static void append(WordprocessingMLPackage docDest, WordprocessingMLPackage docSource) {
List<Object> objects = docSource.getMainDocumentPart().getContent();
for(Object o : objects){
docDest.getMainDocumentPart().getContent().add(o);
}
}
Hope this helps.
I have three classes libraryDB, libraryItems and libraryGUI. libraryDB() is essentially a hash map with the keys as book barcodes/ISBN's and the values are libraryItems, which consist of and therefore take two String parameters: Title and Author.
I have a JFileChooser all set up in the GUI to save, but my save() and open() methods are giving problems. I want it set up so that when it is saved each libraryDB object has its own 3 lines (one each for Barcode, Title, Author, respectively). I tried loading them back in by reading each individual line in, here is the code I wrote for that:
//Suppose to construct a LibraryDB by reading one from a previously-saved file.
public LibraryDB (File file) throws IOException {
Scanner readFile = new Scanner(file);
int barcode;
String title;
String author;
while (readFile.hasNext()){
barcode = Integer.parseInt(readFile.nextLine());
title = readFile.nextLine();
author = readFile.nextLine();
LibraryItem authorTitleValues = new LibraryItem(title,author);
this.libraryItems.put(barcode, authorTitleValues);
}
}
//Trying to save to text file, where for each object n there are 3n lines.
public void save(File file) throws IOException {
PrintStream writer = new PrintStream(file);
for (Iterator<Integer> localIterator = libraryItems.keySet().iterator();
localIterator.hasNext();){
int barcode = ((Integer)localIterator.next()).intValue();
writer.println(barcode);
writer.println((libraryItems.get(Integer.valueOf(barcode))).getTitle());
writer.println((libraryItems.get(Integer.valueOf(barcode))).getAuthor());
}
}
Any help or insight that you can provide that will aid me in successfully being able to save/open would be much appreciated! Thanks!
More explicity, whenever I save a libraryDB to a file I am unable to go back later and open up the file?
You should flush() and close() your PrintStream before ending the save function. I'm not sure that is the problem, since your description is not too accurate, but do it anyway.
File streams and writer have to be explicitly closed at the end of writing to them - otherwise they will lock the file.
public void save(File file) throws IOException {
PrintStream writer = new PrintStream(file);
try {
for (Iterator<Integer> localIterator = libraryItems.keySet().iterator(); localIterator.hasNext();) {
int barcode = ((Integer) localIterator.next()).intValue();
writer.println(barcode);
writer.println((libraryItems.get(Integer.valueOf(barcode))).getTitle());
writer.println((libraryItems.get(Integer.valueOf(barcode))).getAuthor());
}
writer.flush();
} finally {
writer.close();
}
}
So, I just forgot to redeclare libraryDB!? Grrr...lol I don't think the compiler will complain because it has already been declared. However, the information being read from the file was just going into oblivion or something because there was no object for it to be put into. At least, that is what I think was happening. Thanks for your help. Here is my solution:
public LibraryDB (File file) throws IOException {
//this next line was what I was missing...sigh.
this.libraryItems = new HashMap<Integer, LibraryItem>();
Scanner readFile = new Scanner(file);
int barcode;
String title;
String author;
while (readFile.hasNext()){
barcode = Integer.parseInt(readFile.nextLine());
title = readFile.nextLine();
author = readFile.nextLine();
LibraryItem authorTitleValues = new LibraryItem(title, author);
libraryItems.put(Integer.valueOf(barcode), authorTitleValues);
}
readFile.close();
}