Keeping saved files of app after an update - java

I've seen this question, it's not a duplicate.
I'm pretty new to Android-Development, so my way to store personal data on the device might be not the best one.
However, when I update the App, the stored files aren't found anymore.
File path = myContext.getFilesDir();
File pathUse = new File(path, "lists.ser");
if(!pathUse.exists()) {
try {
pathUse.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try(ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(pathUse))) {
os.writeObject(myObject);
} catch(Exception e) {
e.printStackTrace();
}
How can I store the data that way, that it is kept after an update?

Internal files are removed only if you uninstall and reinstall. An upgrade will not remove internal files -- and it shouldn't. Consider that if it did that, your database would be removed for every upgrade as well.
Check the official documentation for more Info Click here
Hence storing the application specific data in external storage is not recommended because anyone can access it.

Related

Moving Java Application to Azure PaaS Function App Loader Writing To Azure File - Use Azure Storage SDK or Leave Oracle JDK as is?

Completely new to Azure.
Moving currently standalone Oracle JDK 1.8 Java application to
Azure PaaS Function App Loader.
Application is to write to Azure File.
Application currently writes to file on local disk using
java.io.FileOutputStream(java.io.File(java.lang.String pathname)).
Have been told no change to this is required.
Except I should change it to Oracle JDK 11 (for support, update reasons etc.)
Write will occur using SMB interface. (What is that?)
Is this correct?
Or should I use the Microsoft Azure Libraries for Java?
In particular: com.microsoft.azure.storage.file.FileOutputStream?
Which is better (if both will work) i.e. performance, better supported?
Thanks,
Brett
If you want to write file to Azure File, Yes, you need to use Azure Storage SDK to call Storage File methods. You could refer to this doc and this is library about storage file.
And here is a simple code to write a file to Azure Storage File, you could refer to this.
#Test
public void writeFile(){
try {
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
CloudFileClient fileClient = storageAccount.createCloudFileClient();
CloudFileShare share=fileClient.getShareReference("storagefileshare");
CloudFileDirectory rootDir = share.getRootDirectoryReference();
File sourceFile = File.createTempFile("sampleFile", ".txt");
System.out.println("Creating a sample file at: " + sourceFile.toString());
Writer output = new BufferedWriter(new FileWriter(sourceFile));
output.write("Hello Azure!");
output.close();
CloudFile cloudFile=rootDir.getFileReference(sourceFile.getName());
cloudFile.uploadFromFile(sourceFile.getAbsolutePath());
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (StorageException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
If you want to know how to develop a Java Azure Function, you could refer to this doc.
If you still have other questions, please let me know.

AccessDeniedException when Creating a recently deleted file

I am trying to delete a file and then recreate it. First I check to see if the file already exists, then, if it does, I delete it. Then I try to create a new file in the same place with the same name. When I do this I get this error:
java.nio.file.AccessDeniedException: inputLog.txt
However, if the file did not exist before running these three operations, then the file is created without issue.
Here is my code:
final Path INPUTLOGPATH = FileSystems.getDefault().getPath("inputLog.txt");
try {
reader = Files.newBufferedReader(INPUTLOGPATH, charset);
} catch (IOException e) {
reader = null;
}
if (reader != null) {
try {
Files.delete(INPUTLOGPATH);
} catch (IOException e) {
e.printStackTrace();
}
}
try {
Files.createFile(INPUTLOGPATH);
} catch (IOException e) {
e.printStackTrace();
}
First I check to see if the file already exists, then, if it does, I delete it.
Why? Opening the file for output will already do all that. You're just repeating work that the operating system already has to do. Remove all this. You're doing it wrong by not closing the file reader, but it's irrelevant. Don't write unnecessary code.
Then I try to create a new file in the same place with the same name
That is also unnecessary as shown. Just open the file for output when you need it.
As you have it now:
you're opening the file, which is a search, among many other things
you're deleting the file, which is another search
you're creating the file, which is another search
then presumably you're opening the file for output, which requires another search, another deletion, and another creation, internally to the operating system.
Don't do this. Just remove all this code. It accomplishes exactly nothing.
You're also introducing all kinds of timing-window problems by this approach, and you still have to deal with eventual failure at the point where you actually open the file for output.

Looping through multiple YAML files to find specific data

I would like to just ask, how would one be able to loop through YAML files to find the needed data?
My situation: I have a Spigot/Bukkit server, and it has a folder filled with lots of files. What I'd need to do, is go through each of these files separately in the plugin to find which file contains the data I need. How could I achieve this?
You can loop through Files by using:
YamlConfiguration config = new YamlConfiguration();
File[] files = this.getDataFolder().listFiles();
for(File file : files){
try {
config.load(file);
if(config.contains("Path")){
//What you need to do.
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidConfigurationException e) {
e.printStackTrace();
}
}
loading them and then cycling through loading them, and then checking if they contain the path you need. However, you may need to specify a folder, to do that simply do a statement within the for loop like:
file = new File(file.getAbsolutePath() + File.separator + "FOLDER_NAME");
But really that last part is incase you have other types of files. You can end up getting an exception if you aren't careful. In general what you are doing isn't generally necessary and there is most likely a much better solution. Just answering your question though.

How do i open a PDF file in Netbeans?

I want my application to open a pdf file when I click a dedicated button. How would i approach this? Also if I run the application from netbeans it shows the pdf but when compiled nothing comes up?
My code
private void showHelpMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
File f = new File("ana.pdf");
try {
Desktop.getDesktop().open(f);
} catch (Exception ex) {
System.out.println(ex);}
You can explicitly give the entire file path, which might solve your problem. Also the OS you are using must support the operation. This might help:
if (Desktop.isDesktopSupported()) {
try {
File myFile = new File("C:\\Users\\klinks\\Documents\\pdf.pdf");
Desktop.getDesktop().open(myFile);
} catch (IOException e) {
// System probably doesn't have a default PDF program
}
}
Your code gets the file from the current directory. The file is there when you run it from netbeans, but the file is not there when you run it.
Unfortunately, there's no easy way to do this. I think the best idea would be write the documentation as HTML, put it on a server, and open the web browser (using Desktop.browse). If someone else has a better idea, please comment.

import external db into app and use it in android

Im new to android development. Now im trying to use sqlite db.
I created a database sqlite file using sqlite manager.
I imported it to the project by /data/data/packagename/dbname, it works fine in emulator , but if I took release in device the app crashes,I didnt know what happens and why it happens. Any Suggestions for it?
You cannot use a External DB in that manner. When you import it into your project it doesn't mean it is available in all devices from there after(Assuming you used DDMS for this). It means that DB is available to that particular emulator only. Follow the below link to find out how to add a External DB to your Application..
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
private void StoreDatabase() {
File DbFile=new File("data/data/packagename/DBName.sqlite");
if(DbFile.exists())
{
System.out.println("file already exist ,No need to Create");
}
else
{
try
{
DbFile.createNewFile();
System.out.println("File Created successfully");
InputStream is = this.getAssets().open("DBName.sqlite");
FileOutputStream fos = new FileOutputStream(DbFile);
byte[] buffer = new byte[1024];
int length=0;
while ((length = is.read(buffer))>0)
{
fos.write(buffer, 0, length);
}
System.out.println("File succesfully placed on sdcard");
//Close the streams
fos.flush();
fos.close();
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
View this link, this will be very helpful if you are using your own database in Android.
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
in this tutorial you have to put your database in assets folder of your project and the database will automatically transferred to database folder of you device.
See https://github.com/jgilfelt/android-sqlite-asset-helper for a helper lib to take care of this.
(I haven't used this library personally but I came across it yesterday while searching for something else. It appears to do what you need, though).

Categories