How to read files from the sub folder of resource folder - java

How to read files from the sub folder of resource folder.
I have some json file in resources folder like :
src
main
resources
jsonData
d1.json
d2.json
d3.json
Now I want to read this in my class which is
src
main
java
com
myFile
classes
here is what I am trying but getting failed.
File folder = new File("./src/main/java/resources/jsonData/");
File[] listOfFiles = folder.listFiles();
for (File file : listOfFiles) {
if (file.isFile()) {
// my operation of Data.
}
}
Not working then I am trying :
Resource resource = new ClassPathResource("classpath:jsonData/data1.json");
File folder = new File(resource.getURI());
File[] listOfFiles = folder.listFiles();
both of things are not working.

You should be able to achieve this using getClass.GetResource()
File[] fileList = (new File(getClass().getResource("/exampleFolder").toURI())).listFiles();

Related

How to delete file in android?

I have used the android internal storage to save a file for my application.
Ex
File rootFolder = context.getFilesDir();
File albumIdFolder = new File(rootFolder,getAlbumId());
Basicaly i want to delete the folder
So i tried with
File rootFolder = context.getFilesDir();
File albumIdFolder = new File(rootFolder,getAlbumId());
albumIdFolder.delete()
but this not working not deleting folder
I readed this answares but not worked in my case please help me solve this issue i am not getting where i am going wong.
Delete file from internal storage
How to delete internal storage file in android?
Edit 2
Ex ^(folder hierarchy)
data
user
0
packageName
files
155775346846131
otherData
i want to delete 155775346846131 folder
You can delete files with folder like as below,
void deleteFiles(Context context) {
File rootFolder = context.getFilesDir();
File fileDir = new File( rootFolder,getAlbumId());
if (fileDir.exists()) {
File[] listFiles = fileDir.listFiles();
for (File listFile : listFiles) {
if (!listFile.delete()) {
System.err.println( "Unable to delete file: " + listFile );
}
}
}
rootFolder.delete();
}
Source : How to delete a whole folder and content?
Don't forgot to give Storage permission.
You can delete files and folders recursively like this:
public void deleteFolderRecursive(File fileOrDirectory) {
if (fileOrDirectory.isDirectory()) {
for (File child : fileOrDirectory.listFiles()) {
deleteFolderRecursive(child);
}
}
fileOrDirectory.delete();
}

Not able to access file in eclipse gives : FileNotFoundException

In eclipse i am trying to give path of one configuration.json file but it is not taking it .
System.getProperty("user.dir")
gives C:\Users\cmalik\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Eclipse
and
try {
File f = new File("."); // current directory
File[] files = f.listFiles();
for (File file : files) {
if (file.isDirectory()) {
System.out.print("directory:");
} else {
System.out.print(" file:");
}
System.out.println(file.getCanonicalPath());
}
gives
file:C:\Users\cm\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Eclipse\Eclipse Jee Neon.lnk
file:C:\Users\cm\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Eclipse\hs_err_pid10180.log
file:C:\Users\cm\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Eclipse\hs_err_pid9728.log
And my project structure is
ProjectName
---src
---- com.abc.def.ghi.jkl.WebServices
------ myService.java
---configuration.json
I tried code for accessing file is :
FileReader file = new FileReader("configuration.json");
or
FileReader file = new FileReader("projectName\\configuration.json");
It is not able to access as output for System.getProperty("user.dir") is eclipse folder not my current projectName folder.
How can i get my files please let me know.
You can use either of the below two:
String location=System.getProperty("user.dir");
or
String location= new java.io.File(".").getCanonicalPath();
And then try to access the file using:
FileReader file = new FileReader(location+"\\configuration.json");
Hope it helps.

Android Studio listFiles returns null with assets folder

In the assets folder I have another folder called songs with .txt files. I've been trying to put all .txt files in a File[ ] but I get a NullPointer on folder.listFiles().
Here's the code :
File folder = new File("assets/songs");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
File file = listOfFiles[i];
if (file.isFile() && file.getName().endsWith(".txt")) {
String content = FileUtils.readFileToString(file);
this.list.add(content);
System.out.println(content);
}
}
return this.list;
Assets are not files on the device. They are files on the development machine. They are entries inside the APK file on the device.
Use AssetManager to work with assets, including its list() method.
As sir #CommonsWare mentioned, you can use AssetManager like this:
AssetManager assetManager = getAssets();
String[] files = assetManager.list("");
Note that this file is String array. So don't forget to initialize new file for each element of the array before iterating over it.

Cannot Access Folder in java

I need my program to be able to read the files inside a folder, however when I try to compile I'm getting the following error: java.io.FileNotFoundException: (Access is denied)
Here is a section of my code:
String filename="FileCount"; //Replace this with file containing names of files to be processed
File file=new File(filename);
Here's a simple example. I hope this is what you're looking for
import java.io.File;
public class Files {
public void listFiles(){
String folderPath = "C:/Users/*****/Desktop"; // or as required
File file = new File(folderPath);
File[] files = file.listFiles();
for (File fileName : files) {
System.out.println(fileName); //Do what you need here... I'm just printing to console.
}
}
}

Java - Read many txt files on a folder and process them

I followed this question:
Now in my case i have 720 files named in this way: "dom 24 mar 2013_00.50.35_128.txt", every file has a different date and time. In testing phase i used Scanner, with a specific txt file, to do some operations on it:
Scanner s = new Scanner(new File("stuff.txt"));
My question is:
How can i reuse scanner and read all 720 files without having to set the precise name on scanner?
Thanks
Assuming you have all the files in one place:
File dir = new File("path/to/files/");
for (File file : dir.listFiles()) {
Scanner s = new Scanner(file);
...
s.close();
}
Note that if you have any files that you don't want to include, you can give listFiles() a FileFilter argument to filter them out.
Yes, create your file object by pointing it to a directory and then list the files of that directory.
File dir = new File("Dir/ToYour/Files");
if(dir.isDir()) {
for(File file : dir.listFiles()) {
if(file.isFile()) {
//do stuff on a file
}
}
} else {
//do stuff on a file
}
You can try this in this way
File folder = new File("D:\\DestFile");
File[] listOfFiles = folder.listFiles();
for (File file : listOfFiles) {
if (file.isFile()&&(file.getName().substring(file.getName().lastIndexOf('.')+1).equals("txt"))) {
// Scanner
}
}
File file = new File(folderNameFromWhereToRead);
if(file!=null && file.exists()){
File[] listOfFiles = file.listFiles();
if(listOfFiles!=null){
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
// DO work
}
}
}
}

Categories