Copy and re-name a file on android - java

I have a file which I am using as a temporary file until the user selects to save this file, How do I go about making a copy of this file but at the same time re-naming it. Example below:
File 1:
/sdcard/Folder/file1.wav
File 2:
Copy of file 1, renamed to file2.wav in /sdcard/Folder/file2.wav
I understand using Environment.getExternalStorageDirectory().getAbsolutePath() gets me the path to the external storage so i'm not asking how I get to the sdcard or whatever. just to make a copy of a file and re-name it.

Try Apache Commons IO FileUtils.moveFile(File, File).

Related

Need File or URL, referencing a folder (NOT FILE) from raw or assets

I have once again had trouble using JWI Wordnet in Android. The only way to access it, is to create a URL or File pointing to the directory where the files it needs are located. http://projects.csail.mit.edu/jwi/api/index.html I have downloaded the files I need, and added the jar into my project. My error is when I try to create the Dictionary object used to access wordnet. I don't point to a valid directory. Which makes sense, but I just don't know how to do so pointing the correct way. Basically, the folder name is "dict", I can place it into either raw or Assets (Although I read something about files in assets needing to be < 1 mb, the files inside the folder are greater than 1 mb) I need to create either a File or URL that points to said folder. How would I go about doing this?
So, you cannot create a file from raw or assets directly, as they are not stored as files, but you can copy/unpack them somewhere else, and make a File object using the copy. Android: how do I create File object from asset file?

Write in a txt file located in /raw folder

here's my problem.
I have a txt file in the /raw folder and I want to write into it.
From the Android guide i saw this:
Tip: If you want to save a static file in your application at compile time, save the file in your project res/raw/ directory. You can open it with openRawResource(), passing the R.raw. resource ID. This method returns an InputStream that you can use to read the file (but you cannot write to the original file).
So in my code I did this:
FileOutputStream myFile = openRawResource(R.raw.max_easy, Context.MODE_PRIVATE);
But gives me error in openRawResource()... How do i solve this?
Thank you for the help!
I have a txt file in the /raw folder and I want to write into it
That is not possible at runtime. Resources and assets are read-only at runtime.
But gives me error in openRawResource()... How do i solve this?
openRawResource() is for reading in the resource, and it gives you an InputStream. You are welcome to write your data to an ordinary file, such as on internal storage.

how to dynamically download of zip file without taking physical memory in servlet

i have one zip file(130 MB size). i need to download that,before that i need to append dynamic new text file in that zip, due to that original source of the zip file not be affect. With no duplicate of zip file create in server.
i need to download that dynamic zip file without taking any physical memory in SERVLET.
please help me how to do in java
orginal file
(C:\user\sample.zip)
text file
(c:\temp\userinfo.txt)
download file(C:\user\sample.zip + c:\temp\userinfo.txt) new zip file

How to copy a file to another location for later use (java)

I am using this code
JFileChooser jf=new JFileChooser();
File f=jf.getSelectedFile();
name=f.getName();
I get the name of the file that I have chosen. In the project that I am doing I need to access this attached file again. I couldn't copy the original path. I want to know if I can copy the file to another location (the contents of the file- file being a .txt file) after it is attached, so that I can access the same for later use.
Thank you.
You can copy file using Files class
Files.copy(f.toPath(), dest.toPath());
or
You can use Apache Commons IO lib to copy
FileUtils.copyFile(File source, File dest);

Copy file to assets folder

After searching 1 hour i do not found any solution to my problem.
I want to move a file from sdcard to assets folder and also overwrite the existing file in assets folder (both file are sqlite database have same name with just a little difference in data )
??
Actually, The behaviour of the android .apk file is read only so the directory structure which are in that apk file follows the same, So no one can write this files or make changes on that at runtime,that's why You can't make a changes in /asset folder at runtime. you can read file from it (means copy files which are available in asset to any internal storage or /sdcard but not write it to vice-versa)
Its apply on all the directories which are build in .apk files. As per docs.
Assets folder are not Writable it is only readable so you cant copy from sdcard to assets, yes you can copy from assets to sdcard
If you want other apps not to access your database then what you can do is
Download the database from website. create a temp database also in app directory.
getDatabasePath() this will give you the path of the dummy database you have created now replace this with the one you downloaded.
PS.: User and apps having root access still able to access it and you cant do anything in that scenario

Categories