I have problem with assets files path in android studio. I want give path of text file in assets folder like below:
File f = new File(path_of_text_file_in_assets);
fw = new FileWriter(f);
bw = new BufferedWriter(fw);
I have similar problem in other parts of this code, that I want give the path of images in subfolder of assets like below:
File ff = new File(path_of_image_in_subfolder_of_assets);
StringBuilder sbFeatureX = uniformQuantization( ff );
I have searched a lot on the internet, but no answer!!
Note: please consider I don't want open the file, so the getAssets().open() is not my answer!
You cannot use the File class for files in assets. File is for files in the file system.
The only way to handle assets files is indeed with that .open() function using the input stream.
Further you cannot write to files in assets. They are read only there.
FileWriter and FileReader both cannot be used.
Related
I am trying to read a hi.txt file present in the "Data" folder inside the phone's internal storage.
I tried the following:
try{
File dir = Environment.getDataDirectory();
File file = new File(dir, "hi.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
line = br.readLine());
}catch (Exception e) {
System.out.println(e.getMessage());
}
But, I get the exception: "/data/hi.txt" (no such file or directory)
I am sure the hi.txt file is there!
Same thing happens if I try opening it from the documents folder also in internal storage like this:
File dir= new File("/Documents/hi.txt");
What am I doing wrong? And how can I read a text file that is stored anywhere in the internal storage? like in the DCIM folder or the Documents folder or Movies or Pictures or Music ..etc ?
I need to use a public text file that can be accessed by any application on the phone.
As I can't add a comment, I have to use answer.
Have you tried with Environment.getDataDirectory().getAbsolutePath(), as you are right about the permissions in Manifest, you need them just for external storage. And the other part of the code looks well to me.
I wounder how to make .ini file in Java. I know how to make .txt file, but how to make .ini file I don't. For reading and wrting I use ini4j lib and I thnik it works good. First I make some directory because of saving some data from user, then I want to make file and I get error java.io.FileNotFoundException for codeline ini.load(new FileReader(INI_PATH)); , that means that my code doesn't make .ini file in codeline File newFile = new File(newPath+"connect.ini"); . Please help me!
My code is:
String path =System.getProperty("user.home");
dir = new File(path+"/ProjectName");
String newPath=path+"/ProjectName";
if(dir.exists()){
System.out.println("DIRECTORY EXISTS");
}
else{
dir.mkdir();
}
newPath=newPath+"/";
File newFile = new File(newPath+"connect.ini");
INI_PATH = newFile.getAbsolutePath();
System.out.println("INI_PATH "+INI_PATH);
Wini ini = new Wini();
ini.load(new FileReader(INI_PATH));
...SOME CODE FOR ADDUING PAIRS....
You are getting a FileNotFoundException because the file does not exist on the disk, if you are trying to create the file in code use the following:
File newFile = new File(newPath+"connect.ini");
newFile.createNewFile();
the createNewFile() method on Java's File class will create a file if it doesn't exist, then you can feel free to use a FileReader or FileWriter to work with the newly created (but blank) file.
I have file kanji.xls.
I would like to get that file through new File();
What should I write inside that constructor?
I tried many options but didn't find correct answer...
I tried:
/kanji.xls
/Kanji database/kanji.xls
//Kanji database//kanji.xls
kanji.xls
Use assets directory in your project to store this file and read file from there. Android – Read file from Assets will guide you.
if you don't wish to use assets directory,
Create directory as Kanji_Database instead of Kanji database. Then use below code.
File f= new File(Environment.getExternalStorageDirectory()
+ "/Android/data/" + getApplicationContext().getPackageName()
+ "/Kanji_Database/", "kanji.xls");
I try to write to a Csv file via:
mFileWriter = new FileWriter(
"/sdcard/program/file");
mCsvWriter = new CSVWriter(mFileWriter);
At the moment it throws an exception that the file doesn't exist.
It's true that the file doesn't exist. What's the easiest way to create the file?
Does the FILE not exist, or the DIRECTORY it's supposed to go into?
If you want to create a directory structure, you can always do
File file = new File("/full/path/to/file");
file.mkdirs();
This will create any path leading up to this file that doesn't exist yet.
I suppose the missing quotes around your file name are a typo?
So I have an application with a JFileChooser from which I select a file to read. Then I change some words and write a new file. The problem that I am having is that when I write the new file it's saved in the project directory. How do I save it in the same directory as the file that I chose using the JFileChooser. Note: I don't want to use the JFileChooser to choose the location. I just need to save the file in the same directory as the original file that I read.
You choose a file like this:
File fileToRead = JFileChooser.getSelectedFile();
Then you read and change the content and write it back to the same location with a different name:
File fileToWrite = new File( fileToRead.getParent(), "newName.txt" );