I have a application where the user can write a text into a EditText and the written text is saved in an auto generated .txt file.
Now I want to show all the txt files from the folder in an activity, not like a regular file browser, just the title of the .txt files in a ListView or as a TextView (each file is illustrated as a TextView).
Get the path of that directory as string and create a new file with this path then,
String path=Environment.getExternalStorageDirectory().getAbsolutePath()+"..../yourFolder";
File file=new File(path);
or
File file=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"..../yourFolder");
Use file.list which will give u all files name in form of an array then use a for loop with endsWith function and store data into a list like this
String arr[]=file.list();
List l=new ArrayList<>();
for(String i:arr){
if(i.endsWith(".txt")){
l.add(i);
}
}
you will have names all text files under selected directory in list l
In Java 8 you can use Files API and Stream API
Files
.list(Paths.get("."))
.forEach(System.out::println);
Related
I am developing Kotlin app.
I am trying to read text and JSON files which I stored in resource folder(res/raw/file.txt).
Looking at the hierachy of app structure, I put absolute path(../../res/raw/file.txt) for file reader.
Unfortunately, my app cannot find the file.
Can you tell me how I can read files stored in res/raw? If I store file in wrong directory, can you tell me where to store and how to access them?
Thanks
If you want to read a file from res/raw folder you can obtain InputStream by R.raw.yourfile id like this:
resources.openRawResource(R.raw.yourfile)
Or you can open file by Uri:
val uri = Uri.parse("android.resource://com.example.your_package/raw/yourfile")
val file = File(uri.getPath());
Alternatevily you can put your files in assets/ folder and get InputStream like this:
assets.open("yourfile.txt")
I'm unable to successfully get the contents of a public key file that I have stored in my res/raw directory.
My objective is to read the contents of this file into a File object that I will then pass along to another function that requires a File object as one of its parameters.
It's important to note that this file contains an X509 key, which I believe means that I need to read its contents as bytes (not text).
This is in the onCreate method of my main activity:
String filename = context.getResources().getResourceName(R.raw.public_key);
Log.d(TAG, filename);
File publicKeyFile = new File(filename);
if (!publicKeyFile.exists()) Log.d(TAG, "no public key file");
Which yields this output:
raw/public_keyno public key file
What am I doing wrong? Thanks!
My objective is to read the contents of this file into a File object that I will then pass along to another function that requires a File object as one of its parameters.
That is not possible, unless you make your own local copy of that file.
What am I doing wrong?
Raw resources are not files. They are entries in the ZIP archive that is the APK file. If you want a File, open an InputStream on the raw resource, copy its contents out to a local file, then use the local file.
I have text files in the raw folder that I would like to use in my app. I am using ListFragments and depending on what item is selected I want to load that files text in a new layout.
I can properly load the text fine but I would like to be able to scan the file previously so that I can have the first line of the file be the title that is displayed in the ListView. I have a method that determines the number of files in the raw folder and adds the names to the ArrayList.
Here is my method:
private void listRaw() {
Field[] fields = R.raw.class.getFields();
for (int count = 0; count < fields.length; count++) {
myArrayList.add(fields[count].getName());
}
}
The problem i am having is setting up a scanner. I do not know how to tell the scanner to scan this specific file. I tried making a new File object and used the Scanner(File) but I do not think I am declaring the file name properly nor if this is even the best way to get this done. Normally if you know the file name you can just simply do:
Scanner fileReader = new Scanner("filename");
but in this loop I never actually have the file name set.
I have text files in the raw folder that I would like to use in my app
I am assuming that "the raw folder" means the res/raw/ resource directory in your project.
I do not know how to tell the scanner to scan this specific file
That is because there is no file. The file exists on your development machine. The representation of the raw resource at runtime is just that: a raw resource. Under the covers, it's effectively an entry in a ZIP file.
You need to get the R.raw value for a raw resource, then use getResources().openRawResource() to get an InputStream that you can pass to a Scanner.
Unless you have different editions of these raw resources for different configurations (e.g., language), you might find it easier to work with the assets/ directory and AssetManager for packaging text files with your app.
In my app am creating some files and storing them in cache dir. now from app only I want to access this files so i get them in a ListView by giving the path getExternalCacheDir().getAbsolutePath(). Here, I get a ListView with the whole path of the files. Instead I just want the file name to be displayed in list.
Can someone help?
Simply call list() in the diretory to return an array of the file names in that directory:
File directory = getExternalCacheDir();
String[] filenames = directory.list();
Alternatively, you can call listFiles() to get back a File[] if that if more useful to you.
HTH
I am looking for a way to store configuration setting on an external file on the sd card. The app must look into this external file and then retrieve some kind of settings. At the moment i am trying to get it to look into a file and get a name. I know you can store things in shared preferences but they are internally accessed.
Anybody know an external way? Thanks
The idea is to have a simple text file or xml file on the sd card. So when a configuration needs changing it is done thru that file?
EDIT
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,"/Config.txt");
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line="";
int c;
while ((c = br.read()) != -1) {
line+=(char)c;
if(String.valueOf(c) == ";" & line =="name;"){
String name ="";
}
}
}
I have attempted to read in a config.txt file and to separate each value with a ;
But i cant seem to understand exactly how i am going to attach what comes after name; to the variable String name.
the config.txt file has the following:
name;fred
somethingelse;test
The program should know when it has got to name and then set the name variable to fred??
Hi
I had same kind of preferences reading in Blackberry with (Comma separated or) semicolon separated values. When same development came to android the developers (I was not on android at that time.) This is what they have done.
We had created a text file like this
"name";"value";"data_type"
For example
"application_runs";"1";"int"
Or
"trial_period_key";"01ab23cd";"string"
The data type is hardcoded so we can detect the datatypes. For variable names we also had hardcoded list of preferences. And values can be parsed accordingly. They have written public readable shared preference based on that txt files, to use default values, they have copied a "default.txt" in assets folder along with a copy in SD card.
The drawback of this procedure is
You have to be careful about file name and the data written in that file (this is the reason why we had put a default values file in assets so the app doesn't crash)
The text file on SD Card must be readable, you have to program it along with parsing.
Hope it helps.