Writing XML File blocking access - java

I have a web platform with users who write and read a XML file. How can i block users to write on the XML file while another one is doing it? For security, i don't want 2+ users writing the XML file at the same time...

You need to synchronize the write method. Read up on it here:
http://docs.oracle.com/javase/tutorial/essential/concurrency/sync.html
You may want to rethink the design if you have multiple users writing to the same config file. I expect you'll have lots of problems. Can you use a database instead?

Related

Need to upload and parse 15MB files, open files twice?

I have a file which I need to upload to a service, and parse into relevant data. The parser and the uploader both require an InputStream. Ought I to open the file twice? I could save the file to a String, but having many of these files in memory is concerning.
EDIT: Thought I should make it clear that the parsing and uploading are entirely separate processes.
Since you are parsing it already it would be most efficient to load the file into a string. Parse it into indexes to the string, you will save memory and can just upload the string whenever you want to. This would be the most effective way, with memory but maybe not processing time.
A reply to one of the comments above.
Separate processes does not mean different threads or processes, just they do not need each other to operate.

inserting existing PDF into one being created, iText 2

I have a web application using iText v2.1.7 to create PDFs; before anyone tries to move me to a different library, let me point out that, like most programmers, I don't choose the libraries my company uses for things, or I certainly would not use this one.
I have code that generates these PDFs; now I am to add code that takes the contents of an existing PDF and inserts it into the PDF I'm creating.
I've found examples of how to do this, but they all use files. Except for the one I'm reading, I don't have files; I'm in a web application where I don't have easy access to a place to write a file.
Can't I open the existing PDF and somehow insert its entire content into the document I'm creating, without having to write to a file?
After I do this, I will have more content to add to the document, either from another file, dynamically created content, or both, so it isn't a simple merge of my content with one existing file. I also haven't created the existing file as its own entity, to be merged with another file, though I suppose I can do that IF it's necessary.
But I was hoping there was a way (or were ways) to do this without having to reorganize my existing code. It's possible the answer is implied in one of these examples, but they don't explain the concepts behind things, so I don't know where I can put input Streams instead of file input streams, output streams instead of file output streams, etc.

Android: extract data from external XML file and store them in internal XML file (same file structure)

I'm building an app which takes some data from a Web XML file (similar to RSS feed) and shows those data in a Webview.
This is done through a DOMParser activity which i feed with different XML URLs.
Now, i would like to take some of these data and store them in the internal storage (a kind of "Favorite" option), to let the user to see them offline.
To see these stored data, i would like to use the same DOMParser, feeding an internal path instead of a web URL (so i don't need to create another activity).
To go in this way, i need to create an empty XML file somewhere (res/XML/file.xml), store the necessary data inside this file (whenever the user choose to do it) and let the user to retrieve the data from this XML (instead of the one on the web URL) when offline thorugh the DOMParser.
Which is the better way to store data to keep what above?
Is SQLite able to store and delete data in a XML file or do i need to use other tools?
Thanks to anyone who wants to help me
You can't store this files inside res/xml folder, it's not allowed. You can store it in filesystem. Android official tutorial explains storing files in great details.
When you store files or read them you have to work with Streams.
Update in response to first comment
It depends how big and complex is that part of XML data. If you wish to retrieve that part XML in whole then I would go with filesystem.
SQLite would be a good choice if you like to have SQL structure in respect to your XML part, then you could benefit e.g. creating query to find XML bits you are interested in. I don't see a point to use SQLite and store XML part as a String per row.
If you still don't know which to chose, have a look at example implementations and see which one is more comfortable for you.

Java XML practice

I have an application that receives weather information every x seconds. I am wanting to save this data to an XML file.
Should I create a new XML file for each weather notification, or append each notification to the same XML file? I am not sure of the XML standards of what is common practice.
I highly recommend appending not because that is a standard practice of XML, but more because creating a new file every x seconds will likely be a very difficult way to manage your data. You may also run into limitations of your file system (e.g. maximum files per directory).
You might also consider using a database instead of files to store your data.
XML files have only one root element. You can write multiple XML fragments into the file but it won't be a valid document then. So while both options are fine, and you should consider your other requirements too, the standard somewhat nudges you towards writing a file (or a database row) per notification.

Writing to text file in GAE

Has anyone figured out a way to write to text file on the server-side using Google App Engine (GAE). I understand the limitation of not being able to use FileWriter, but I was wondering if there was a work around. Thanks!
Not only can you not use FileWriter, you can not write to files at all, since you do not have access to the filesystem from within the GAE. It's impossible by design.
As a "work-around" (which I hesistate designating as such since this is a technically valid solution), you can emulate a filesystem using the GAE datastore using GAEVFS: http://code.google.com/p/gaevfs/
Try the blob store. Now, you can write to the blob store like you write files to the file system.
https://developers.google.com/appengine/docs/java/blobstore/overview#Writing_Files_to_the_Blobstore

Categories

Resources