I am wondering if (and if so, how) it is possible to create new, empty text file(s) in the app directory when the app first installs. Until now I've always done a:
File cart = new File(this.getFilesDir(), "cart.txt");
if(cart.exists() == false) {
try {
cart.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
But this seems a bit impractical if I need to make about 10 empty text files and it needs to check all those 10 before it starts every time.
I was thinking maybe somewhere in the Manifest but I haven't found any online solutions.
Related
somehow I want to open a file form a web application with the desktop application from client side.
My boss told me to use Applet. I've been through all the internet could provide me, but still can't find how to do it.
I've build a code program from java class to open the file directly but I can't make the applet running from JSP file.
Here's my code :
public static void main(String[] a) {
try {
URI uri = new URI("your/local/file/path");
Desktop desktop = null;
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
}
if (desktop != null)
desktop.browse(uri);
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (URISyntaxException use) {
use.printStackTrace();
}
}
If somebody ever done it before, I'll be really thankful.
Normally, Applets do not have access to the local file system due to security issues. However, there are ways to grant file system access to applets. This article describes the procedure: https://www.developer.com/java/other/article.php/3303561/Creating-a-Trusted-Applet-with-Local-File-System-Access-Rights.htm. Altough I have not tested it myself and it is rather old, the article seems promising. I hope, this helps you.
I have a electronic phone book application with MySql, everything works just fine in my Eclipse, BUT... when i export runnable jar file, when i run my program everything works fine except that one of my JFrame dont want to show (but it show only in eclipse. no erorrs no nothing, i dont know what to do) ...i talk about my frame where the user can add data to database.
my code for showing that JFrame is this
if (conectat) {
try {
PaginaAdd frameAdd = new PaginaAdd();
if (VariabileGlobale.pagAdd == "NU") {
VariabileGlobale.pagAdd = "DA";
// sa aiba iconita
try {
frameAdd.setIconImage(
ImageIO.read(getClass().getResourceAsStream("/data-add-icon.png")));
} catch (IOException e) {
e.printStackTrace();
}
// terminare sa aiba iconita
frameAdd.setLocationRelativeTo(null);
frameAdd.setVisible(true);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else
JOptionPane.showMessageDialog(null, "You are not connected to database!", " Electronic Phone Book",
JOptionPane.WARNING_MESSAGE);
}
Please help. I dont understand why in eclipse work and why on exported jar file not working :((
With the help of user "MadProgrammer" i was able to figure and solve my problem.
Also with this i learned how to use a very importand JAVA command console for running my jar files, where is showing everyting in execution of the program. Soo the command that i used to find the problems is this
java -jar myExecutableNameFile.jar
And my problem was this:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at cnbi.AgendaTelefon.Java.GUI.PaginaAdd.<init>(PaginaAdd.java:388)
at cnbi.AgendaTelefon.Java.GUI.PaginaPrincipala$9.actionPerformed(PaginaPrincipala.java:371)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
My solution was to remove a duplicat image (that has similar name with the current menu image who was calling that jframe to be visible). After i deleted the code and inserted the correct one with new image path... everything works perfect now.
I want to thanks once again to you "MadProgrammer" because you saved me. I was searching to solve this for 5 hours until you came :)
So I'm making the transition from BlueJ to Eclipse like any good college Freshman would and I can't, for the life of me, Find where to store an image in the workspace which would allow me to access it from my code. any ideas?
Usually, you store your images in a resources folder.
Here's an example from one of my projects.
My images are stored in an images folder, which is included in the Java classpath.
I read them with a method call:
model.setCheckMark(readImage("/check_mark.png"));
And here's the readImage method:
private Image readImage(String streamString) {
try {
return ImageIO.read(getClass().getResourceAsStream(streamString));
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
Right click on your project on the left side-bar and create a 'New -> Source Folder'. A common mistake and one I made is to simply create a 'New -> Folder'. This wont work and I can relate to anyones frustrations at this and similar hurdles when moving from BlueJ to a proper IDE like Eclipse.
i am creating an application for android in eclipse.
i have a chat application in which user's contact's images are downloaded in sdcard and are put in a HashMap (url, localAddress), when i want to load contact list, for any contacts I use a function to find address of contacts' pictures on sdcard, three different state may occur
1- image found on sdcard then return path.
2- image don't download before and HashMap return null, then download it.
3- HashMap return a path but user deleted image from sdcard then remove key from Hashmap and download again.
my code:
public static String findFile(Context ctx, String url, String type)
{
try
{
String value = globalVars.addresses.get(url);
if(value != null)
{
File ff = new File(value);
if(ff.exists())
return value;
globalVars.addresses.remove(url);
}
globalVars.enqueueJob(ctx, new globalVars.downloadJob(url, url, type));
return null;
}
catch(Exception ex)
{
Log.e("Find File", "Start");
Log.e("Find File", ex.toString());
ex.printStackTrace();
return null;
}
}
but when i delete an image it download more and more .
function enqueueJob :
public static void enqueueJob(Context context, downloadJob dj)
{
if(inQueue.get(dj.getAddress())!= null && inQueue.get(dj.getAddress())== true)
return;
downloads.add(dj);
inQueue.put(dj.address, true);
FileUtils.doDownload(context);
}
It work's fine for pictures don't download yet and pictures that download and don't delete yet.
Your title is confusing as it indicates you suspect the File.exists as the problem. Looking at your code, the fact that deleted images are added to download queue means File.exists works fine at least the first time. Otherwise, you would not even add it to the queue. Now the question is what happens when it is added to queue and downloaded. Check your code to make sure that it added back to the map properly. In your code, something should be added to the download queue only under two conditions: one if nothing in the map and the other is whatever in the map is deleted. So either it is not added back properly or may be a race condition where your File.exists is happening before the download is completed fully and you are adding it back to the queue again.
Good luck.
I am writing an android application that takes in a string, read in by a bar code reader, and then when a particular button press occurs, it will send that string to the database and set their values either to "in stock" or "out of stock". I have not been able to find anything on this except for jackcesss, which does not seem to have any really good documentation on it. I cannot even get it to open the file with their example code. The code looks like this:
try {
Database db = Database.open(new File("sdcard/download/Inventory-1.mdb"));
db.close();
} catch (IOException e) {
new AlertDialog.Builder(CheckInActivity.this).setTitle("CRITICAL ERROR").setMessage("DATABASE FILE NOT FOUND. Please check your wireless connection").setPositiveButton("OK",null).show();
e.printStackTrace();
}
When I run this code on my phone I get a force close (I run it on my phone each time to avoid emulator issues,also I am also try to make it work locally before I attempt to make it a remote file) However if I change the location of the file to something I know does not exist, it will catch it and pop up the dialog box that I specified. I tried this with and without closing the file, with throws and the try and catch, nothing seems to work. So what am I missing here?
How do you know the database open was unsuccessful? I'd put in an explicit indication to hit me over the head:
try {
Database db = Database.open(new File("sdcard/download/Inventory-1.mdb"));
new AlertDialog.Builder(CheckInActivity.this).setTitle("SUCCESS").setMessage("DATABASE WAS OPENED SUCCESSFULLY. ").setPositiveButton("OK",null).show(); db.close();
} catch (IOException e) {
new AlertDialog.Builder(CheckInActivity.this).setTitle("CRITICAL ERROR").setMessage("DATABASE FILE NOT FOUND. Please check your wireless connection").setPositiveButton("OK",null).show();
e.printStackTrace();
}