I am writing an application where I would like to allow the created csv file to be used on a PC. Android has Internal Storage which is viewable on the Android and can be accessed via USB on the PC. I tried various methods to generate a file name:
/data/user/0/com.ed.ilan.ioioterbium/files/documents/2016_9_16_10_28_12.csv
/data/user/0/com.ed.ilan.ioioterbium/files/2016_9_16_10_35_57.csv
2016_9_16_10_46_34.csv
The name of the application I got using getFilesDir(), but the first 2 examples didn't work as Java complained about the slashes in the name. The 3rd one worked but nothing was visible in (appName)/files/. I don't know if this by design for security reasons, or if the file was really not there. The code is
String name = getFileName();
try {
FileOutputStream fOut = new FileOutputStream(name);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write("test1");
osw.flush();
osw.close();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
Where I would really like the file to be would be in Internal Storage. I created a folder called myData, which I can see both on the phone and on the PC. The question is: how do I direct the application to write to Internal Storage. I recognize that this isn't "secure", but I just want to get the file.
To write to the Internal storage one needs this in the manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
If we should decide to put the file on a cloud server, is there a standard way to do that?
Thanks,
Ilan
If you are using getFilesDir() then it returns a path which is specific to android application. User's simply can't access the path returned by getFilesDir().
If you have a rooted device then you can get access to the above path.
If you want that only your application can access files stored by you then use getFilesDir(), else you can use
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath();
I understand that you want to write a file to your phone's Documents folder, am I right? If so, the following may be of help where you can write your .csv files at.
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
Related
Please, help.
I have a java program that currently reads a list of files from physical directory (shared) and while reading, creating output files - also on the shared location.
So, for several files read, there can be a a single output file.
New requirements came to take the files from that output location and ftp them to a given ftp server
The caveat is: The ftp procedure is done by microservice that most likely runs on the machines which do not have access to the output folder. So, I am making a call to a service, not using FTP API (Appache, or etc..) internally.
Is it possible to send a OutputStream (byteOutputStream, or ObjectOutputStream) over an http call? If so, how?
I know how to ftp the files from within my application, but the goal is .. to make a microservice call (webservice call, for what it matters, since my application is a plain old core java app).
I had a (rather different) project recently, however it also had the requirement to write to a http response.
I used this approach which works in my case:
try (OutputStream os = response.getOutputStream()) {
final PrintWriter w = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));
Then after that, I write to the PrintWriter w.
My application is writtent in Spring boot. I follow this tutorial https://spring.io/guides/gs/uploading-files/ to write an endpoint to upload image.
I was able to receive the image . All I want to do is to upload it to my own storage in another host and I also can do this. But I don't understand why the file is automatically saved in my project's directory. The directory will always contain images (sample.png , sample1.png) like this and it's not good on the production server.
The directory looks like this.
How can i prevent this?.
If you simply copied the source code of the guide on Spring's website, then that's the expected behaviour since that's what the code in the guide does.
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(Application.ROOT + "/" + name)));
FileCopyUtils.copy(file.getInputStream(), stream);
stream.close();
You need to pass a different path to FileOutputStream where the file will be written.
I've done a procedure to backup/restore user's DB into an external SD card. Theoretically it works fine, the file is being copied and restored every time I launch it.
What I don't understand is WHERE that file is copied. According to the message I receive when I do my backup, the archive is put inside "\storage\emulated\0" folder.
But .. Where is that folder? According to the documentation that should be my SD folder but actually, watching on my phone, that directory is INSIDE my phone's internal memory and that file is not present inside my SD card.
I even tried this path to understand where Android sees my SD :
String state = Environment.getExternalStorageState();
File sdPath = Enviroment.getExternalStorageDirectory();
*** The results are:***
- state : Mounted (so the SD can be read and written)
- sdPath: \storage\emulated\0
So, once again, the SD seems ok, can be written... but why It keeps writing on my internal phone's memory?
The path to SD Card is usually \storage\extSdCard\YourDir
You can get it By :
String sdCardPath= System.getenv("SECONDARY_STORAGE");
Android take Mobile Storage as External Storage and Storage where application data stored as Internal Storage.
Devices which does not provide internal storage
If you write code like this then you can see your db files in your mobile inside sdcard
public DataBaseHandler(Context context)
{
super(context,"/sdcard/Folder/"+DATABASE_NAME,null,DATABASE_VERSION);
}
One folder will be created inside your mobile sdcard, you can check. If it is just a file then change the extension with .db. After that copy it to desktop and open the sqlite browser, then click on open database, open this file and in sqlite browser you can see one option "browse". Now you can see your data is inserted or not. Enjoy !
I have this java application project(client) based on peer to peer file sharing system.
It doesn't use any kind of database.
There's a Download tab which shows the names of previously downloaded files as well as currently downloading file too.
What I don't understand is that where this information is getting stored and retrieved from. Every time I close and reopen the application , The information of previous downloads is there.
Also, it stores the IP addresses of the previously connected hosts and shows them in a tab.
I checked that there isn't any flat file, db file or log file in the project folder.
There are 150 files of code so i really cant go through it and find out.
Sorry if the question is too naive but if anybody have even a single clue then please do comment.
edit : I found this bit of code
public void saveDownloadInfo()
throws IOException
{
FileOutputStream fos = new FileOutputStream(ServiceManager.getDownloadSaveFilename());
ObjectOutputStream oos = new ObjectOutputStream(fos);
try
{
// Use latest version to serialize.
serialize1(oos);
}
finally
{
oos.close();
}
}
public void loadDownloadInfo()
throws Exception
{
FileInputStream fis = new FileInputStream(ServiceManager.getDownloadSaveFilename());
ObjectInputStream ois = new ObjectInputStream(fis);
Still not able to track where it's getting stored.
Maybe it is just using the filesystem as the database. For example, if it knows all the files it downloads are to a specific folder, then it just scans that folder for previously downloaded files.
Edit: After reading that this application also stores IP Addresses, then either this information must be stored locally or remotely in some fashion. If you are certain it is not stored locally, then the only answer is that this information is retrieved from the server using a unique client identifier such as a session cookie, ip address, or a unique id baked into the application client.
There's a class called Preferences which stores this kind of information in an OS-dependent way. For Windows, it stores it in the Windows registry. On other OSs, there may be some kind of flat file in a "special" location. It is probably what your application is using.
I want to make an Applet write to a text file. I think have overcome the security problem with my own certificate, but the text file isn't receiving the output.
My Applet can be seen at tomrenn.com/fallball, and should ask if you trust the certificate.
I'm not sure an Applet can write to a text file within the web hosted contents, so I'm asking if this is possible; and if not, at least be able to do it on the local machine. I had been using a FileOutputStream, but my teacher recommended this very basic output method.
public void writeToFile()
{
try {
java.io.File file = new java.io.File("highscores.txt");
java.io.PrintWriter output = new java.io.PrintWriter(file);
output.print(name + " " +score);
output.close();
} catch (IOException e) {
System.err.println ("Unable to write to file");
System.exit(-1);
}
}
This is not what I would be implementing, just trying to see if I get any output. Thanks for looking over my question!
From an Applet, you cannot directly write to the server's file system. You can issue a request to the server that causes the server to write to its own file system, but an Applet does not have a way to write to a file system on a remote machine. (Of course, unless it's mounted NFS or otherwise.) To issue such a request, you could use Apache HttpClient to issue HTTP requests, for example. This may be more heavyweight than you are looking for. You can also have the client issue a POST to the server to say, "This is my high score," and let the server manage high scores.
A signed Applet has every right to write to the local file system of the person running the Applet. If you are writing to the "current directory" (rather than an absolute full path), then make sure you know what directory the Applet is running in. Otherwise you may indeed create a file, but not be able to find it!
If you want an applet to store data on the local machine, from 6u10 the javax.jnlp.PersistenceService is available. Creating a secure "signed applet" is difficult, and I wouldn't recommend it to anyone.
Applets run on the client so cannot access the servers disk.
The code you posted will write to the clients local disk. I'd suggest changing it though to specify the directory you want to place the file. The users home directory would seem a good place for it
java.io.File file = new java.io.File(System.getProperty("user.home"), "highscores.txt");
just sign your applet.
it is easy using netbeans.
open the project in netbeans.
right click on your project and select properties now you will have new window.
go to > application > webstart on that window.
check enable webstart.
press customize button at signing and choose "self signed by a generated key".
check "applet descriptor" and target your applet.
press ok.
rebuild the project (now netbeans will create certificates with all privileges )
use "launch.html" at your projects dist dir to run the applet via jnlp.
That's all.
** used netbeans version = 7.0
** used JDK = 1.6
** certificates will expire in 6 months.
Thanks