Trying to get a list of files so that I can name the next file based on how many are already there. The idea is similar to downloading multiple files with the same name. Eg:
filename.txt
filename(1).txt
filename(2).txt
etc...
this is the current method I am using, which returns a list of strings.
private List<String> getFilenames(String path) {
File files = new File(path);
FileFilter filter = new FileFilter() {
private final List<String> exts = Arrays.asList("json");
@Override
public boolean accept(File pathname) {
String ext;
String path = pathname.getPath();
ext = path.substring(path.lastIndexOf(".") + 1);
return exts.contains(ext);
}
};
final File [] filesFound = files.listFiles(filter);
List<String> list = new ArrayList<String>();
if (filesFound != null && filesFound.length > 0){
for (File file : filesFound){
list.add(file.getName());
}
}
return list;
Once I have the list I'm thinking I can use the list.length method to figure out which number I need to put in the filename.
Here is where I am creating the file.
JSONObject jsonToSave = createJSONObject();
String filepath = "fileStorage";
List<String> filenames = getFilenames(filepath);
int filenameNumber = filenames.size() + 1;
ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
filename = "newAssessment(" + filenameNumber + ").json";
internalFile = new File(directory , filename);
try {
FileOutputStream fos = new FileOutputStream(internalFile);
OutputStreamWriter osw = new OutputStreamWriter(fos);
osw.write(jsonToSave.toString());
osw.close();
Context context = getApplicationContext();
CharSequence text = "Assessment saved!";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
catch (IOException e){
e.printStackTrace();
}
the createJSONObject just creates a json object with the data that needs to be saved.
I can confirm the the application is getting to the Toast that is in the try block after the OutputStreamWriter is closed, so the file is saving.
The problem that I am having is when trying to populate a ListView in another activity with some of the data from the files, nothing appears on listview. I have tested with success reading a single file.
I'm using the same getFilenames method as above, then using FileInputStream to read the data from the files, parse it into a json object, add some fields from each file to an array, and then update an android listview with the array.
Heres the code:
filenames = getFilenames(filepath);
ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
for (String filename : filenames) {
internalFile = new File(directory, filename);
try {
FileInputStream fis = new FileInputStream(internalFile);
DataInputStream in = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String assessmentDate = "";
String orchard = "";
String strLine;
int dataCounter = 0;
while ((strLine = br.readLine()) != null) {
myData = myData + strLine;
try {
JSONObject object = new JSONObject(myData);
assessmentDate = object.getString("assessmentDate");
orchard = object.getString("orchard");
} catch (JSONException e) {
e.printStackTrace();
}
data.add(dataCounter, assessmentDate + " : " + orchard);
dataCounter++;
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
LVAssessments.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, data));
Any help is much appreciated, thank you.