Embedded Applet Cannot Read File - java

I imagine this would be a very rudimentary problem, since I am not quite familiar with applet deployments: I was made to convert a Swing application into an applet and embed it to a webpage. This applet constructs its knowledge base by reading lines from a text file (in the same directory as the .class file), and when I launched it from the applet viewer it reads the file with no problem.
Upon embedding, however, it fails to read the file and the exception handling is triggered. Perhaps this is one of those security restrictions?
I use File, FileReader, and LineNumberReader to read this document.
Additional problem: this also happens with images, where the applet would not run at all. I temporarily got around this by placing said images within an HTML file, but it could be done much better.

Don't try to load it directly using a file - use Class.getResourceAsStream and InputStreamReader. In other words, load it as a resource rather than as a file.

Related

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.

Starting HTML/CSS/Javascript file from Java

I have written an Java code that process some data and stores it in .JSON format.
I also have HTML/CSS/Javascript file, that reads that data, and shows it in much nicer way.
So my question is, is it possible to combine Java and HTML code(in one file .jar) so that, for example on a click of a mouse Java opens HTML file in Default browser?
Thanks
Write out the html file to the filesystem (e.g. a temporary directory How to create a temporary directory/folder in Java?) and then open it using the default program: How to open a file with the default associated program

What is a .lck file and why can't I read it with a buffered reader?

I'm trying to use crawler4j to crawl websites. I was able to follow the instructions on the crawler4j website. When it is done it creates a folder with two different .lck files, one .jdb file and one .info.0 file.
I tried to read in the file using the code that I provided in this answer to read in the file but it keeps failing. I've used the same function to read text files before, so I know the code works.
I also found someone else that asked the same question a few months ago. They never got an answer.
Why can't I use my code to open and read these .lck files to memory?
Crawler4j uses BerkeleyDB to store crawl informations. See here in the source.
From the command line you can use DB utils to acces the data. Already covered in SO here.
If you want to access the data in your Java code, you simply import BerkeleyDB library (Maven instruction there) and follow the tutorial on how to open the DB.

Using zip file without extracting in java

I'm facing a problem that, we have a .zip file that contains some text files. Now I'm using java to access that files. If it is not in the .zip file I can read and print on my console easily using FileInputStream.
But how to read a file from .zip file? I use J2SE only..
You should try a ZipInputStream. The interface is a little obtuse, but you can use getNextEntry() to iterate through the items in the .zip file.
As a side note, the Java class-loader does exactly this to load classes from .jar files without extracting them first.
Everything you need is in ZipFile: https://docs.oracle.com/javase/7/docs/api/java/util/zip/ZipFile.html. Google for examples on the web, and if you have specific problems then come back to SO for help.
(The link will eventually break; when it does simply websearch java zipfile.)

Reading XML in Java Applet

I know:
I have to sign the applet so it can read files
How to parse XML files
My Questions would be:
Should I keep the XML file in the .jar or separate?
Are there some best practices tutorials? if so please link me
If you ship your XML with the applet, then you won't need to sign it, as you're not really reading files, but just loading additional resources.
Only you can answer where the XML should be, because we don't know what it's used for, how often it changes and if its the same for every user of your applet.

Categories

Resources