error=24, Too many open files. Deleted files are still open - java

I have a method that fetch folder file . I only read the filename of the file. At the end of my program I delete the file. The problem is there's a time that "Too many open files" error appear.
I found out that the deleted files are still open.
Here are my codes:
Getting file
private File getFile(String fileName,String filetype) {
File dir = new File("./");
File[] foundFiles = dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith(fileName) && name.endsWith(filetype);
}
});
if(foundFiles.length!=0) {
return foundFiles[0];
}
}
...
File tempFile = this.getFile("versions.sh_",".pid");
String fileName = tempFile.getName();
int startNo = fileName.indexOf("_") + 1;
int endNo = startNo + 5;
pid = fileName.substring(startNo, endNo);
//other logic
Deleting File
if (pidFile != null) {
logger.info("Deleting pidFile file = " + pidFile.toString());
Files.deleteIfExists(pidFile.toPath());
}
Know I want to get rid of the code that open the file but I dont know which part of my code.
Btw this program runs on linux.
----- update
The .pid file is created by script
#!/bin/bash
SCRIPT_PID=`echo $$`
ME=`basename "$0"`
SCRIPT_DIR=$(pwd)
RESULT_TXT=${SCRIPT_DIR}/${ME}_result.txt
PID_FILE=${SCRIPT_DIR}/${ME}_${SCRIPT_PID}.pid
echo $$ > $PID_FILE

The above code keeps the file/folder open in linux. So i used DirectoryStream and try-resources block to implement auto-closable to the used resources.
try (DirectoryStream<Path> paths = Files.newDirectoryStream(Paths.get(base),
path -> path.getFileName().toString().startsWith(fileName) && path.toString().endsWith(filetype))) {
paths.forEach(path -> pathList.add(path));
if (pathList.size() > 0) {
file = pathList.get(0).toFile();
}
}

Related

Unable to extract nested tar within zip file i.e. a .tar file inside a zip file and so on

I have gone through the link of how to extract a .tar file and several link on SOF using Java.
However, I didnt find any which can relate to my concerns which is multilevel or nested .tar/.tgz/.zip file.
my concern is with something like below
Abc.tar.gz
--DEF.tar
--sample1.txt
--sample2.txt
--FGH.tgz
--sample3.txt
-sample4.txt
This is the simple one which I can give here . As it can be in any compressed combination with the folder like .tar inside .tar and .gz and again .tgz and so on....
My problem is I am able to extract till the first level using Apache Commons Compress library. that is if Abc.tar.gz gets extracted then in the destination/output folder its only DEF.tar available . beyond that my extraction is not working.
I tried to give the output of first to the input to the second on the fly but I got stuck with FileNotFoundException. As at that point of time output file would have not been in place and the second extraction not able to get the file.
Pseudocode:
public class CommonExtraction {
TarArchiveInputStream tar = null;
if((sourcePath.trim().toLowerCase.endsWith(".tar.gz")) || sourcePath.trim().toLowerCase.endsWith(".tgz")) {
try {
tar=new TarArchiveInputStream(new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(sourcePath))));
extractTar(tar,destPath)
} catch (Exception e) {
e.printStackTrace();
}
}
}
Public static void extractTar(TarArchiveInputStream tar, String outputFolder) {
try{
TarArchiveEntry entry;
while (null!=(entry=(TarArchiveEntry)tar.getNextTarEntry())) {
if(entry.getName().trim().toLowerCase.endsWith(".tar")){
final String path = outputFolder + entry.getName()
tar=new TarArchiveInputStream(new BufferedInputStream(new FileInputStream(path))) // failing as .tar folder after decompression from .gz not available at destination path
extractTar(tar,outputFolder)
}
extractEntry(entry,tar,outputFolder)
}
tar.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
Public static void extractEntry(TarArchiveEntry entry , InputStream tar, String outputFolder){
final String path = outputFolder + entry.getName()
if(entry.isDirectory()){
new File(path).mkdirs();
}else{
//create directory for the file if not exist
}
// code to read and write until last byte is encountered
}
}
Ps: please ignore the syntax and all in the code.
Try this
try (InputStream fi = file.getInputStream();
InputStream bi = new BufferedInputStream(fi);
InputStream gzi = new GzipCompressorInputStream(bi, false);
ArchiveInputStream archive = new TarArchiveInputStream(gzi)) {
withArchiveStream(archive, result::appendEntry);
}
As i see what .tar.gz and .tgz is same formats. And my method withArchiveEntry is:
private void withArchiveStream(ArchiveInputStream archInStream, BiConsumer<ArchiveInputStream, ArchiveEntry> entryConsumer) throws IOException {
ArchiveEntry entry;
while((entry = archInStream.getNextEntry()) != null) {
entryConsumer.accept(archInStream, entry);
}
}
private void appendEntry(ArchiveInputStream archive, ArchiveEntry entry) {
if (!archive.canReadEntryData(entry)) {
throw new IOException("Can`t read archive entry");
}
if (entry.isDirectory()) {
return;
}
// And for example
String content = new String(archive.readAllBytes(), StandardCharsets.UTF_8);
System.out.println(content);
}
You have a recursive problem, so you can use recursion to solve it. Here is some pseudocode to show how it can be done:
public class ArchiveExtractor
{
public void extract(File file)
{
List<File> files; // list of extracted files
if(isZip(file))
files = extractZip(file);
else if(isTGZ(file))
files = extractTGZ(file);
else if(isTar(file))
files = extractTar(file);
else if(isGZip(file))
files = extractGZip(file);
for(File f : files)
{
if(isArchive(f))
extract(f); // recursive call
}
}
private List<File> extractZip(File file)
{
// extract archive and return list of extracted files
}
private List<File> extractTGZ(File file)
{
// extract archive and return list of extracted files
}
private List<File> extractTar(File file)
{
// extract archive and return list of extracted files
}
private List<File> extractGZip(File file)
{
// extract archive and return list of extracted file
}
}
where:
isZip() tests if the file extension is zip
isTGZ() tests if the file extension is tgz
isTar() tests if the file extension is tar
isGZip() tests if the file extension is gz
isArchive() means isZip() || isTGZ() || isTar() || isGZip()
As for the directory where each archive is extracted: you are free to do as you want.
If you process test.zip for example, you may extract in the same directory as where the archive is,
or create the directory test and extract in it.

How to access files in a specified existing folder in src directory?

I have a folder called "all_users" in my java project under the src directory.How can I access the files(if there are any) in the all_users folder. I eventually want to loop through all the existing files in the "all_users" folder, comparing whether the file name is equal to a string i specify in the code.
Firstly, I tried File f = new File(System.getProperty("user.home")+File.pathSeparator + "all_users"); as the file object then later tried File dir = new File(TEST_PATH); Both returned false when i checked if it existed so i didn't set up the path correctly?
public class ValUtility {
static final String TEST_PATH = "./all_users/";
public static boolean validUsername(String user) {
File f = new File(System.getProperty("user.home") + File.pathSeparator + "all_users");
File dir = new File(TEST_PATH);
File[] directoryListing = f.listFiles();
System.out.println(f.exists());
System.out.println(directoryListing);
if (directoryListing != null) {
for (File child : directoryListing) {
// Do something with child
// think child is filename?
if (user.equals(child.getName())){
return false;
}
}
}
return true;
}
}
Please run...
System.out.println(System.getProperty("user.home"));
The above will inform you where you need to add a folder labeled 'all_users'. It is very unlikely that your 'user.home' property is set to your project's source file (src) folder.

listFiles does not return files but just top level folder

I am trying to iterate over a folder
My files a located in
D:\PROJECT_FOLDER\rootProject\semiRootProject\project\build\resources\main\com\xxxx\pack\file.xlsx
However when I try to iterate over it in console it shows
11:39:06.731 [main] INFO com.xxxx.util.KiePackageCreator - File found: D:\PROJECT_FOLDER\rootProject\semiRootProject\project\build\resources\main\com.
What's the problem? My search loop looks like this.
File fileFolder = new File(projectBuildDir + RESOURCE_SUBFOLDER);
for (File file : fileFolder.listFiles(new FilenameFilter() {
#Override
public boolean accept(File dir, String name) {
//if (name.endsWith(".xlsx")) {
return true;
//}
//return false;
}
})) {
LOGGER.info("File found: {}.", file.toPath());
if (file.isFile()) {
Resource fileResource = getClassPathResource(file.getName());
String filePath = file.getPath();
String rulePath = MAVEN_RESOURCE_PATH + filePath.substring(filePath.indexOf("com"));
LOGGER.info("Attempt to write into: {}.", rulePath);
kfs.write(rulePath, fileResource);
}
}
List files lists all files and directories in the directory you specify. It does not do so recursively.
Maybe walkFileTree suits you better.
You need to iterate through the sub folders in your code. You are just iterating over the files in the directory that fileFolder is pointing to.

Java - FileNotFoundException: /mnt/sdcard: open failed: EISDIR (Is a directory)

I'm trying to decompress RAR archive on my Android device from the sd card but I got an error:
java.io.FileNotFoundException: /mnt/sdcard: open failed: EISDIR (Is a directory)
I chose a rar-file and try to decompress it in my sd-card.
Error says that it's not directory but it is. I have no idea how I can fix it.
My code:
public static void unrar(File srcRarFile, String destPath, String password) throws IOException {
if (null == srcRarFile || !srcRarFile.exists()) {
throw new IOException(".");
}
if (!destPath.endsWith(SEPARATOR)) {
destPath += SEPARATOR;
}
Archive archive = null;
OutputStream unOut = null;
try {
archive = new Archive(srcRarFile, password, false);
FileHeader fileHeader = archive.nextFileHeader();
while(null != fileHeader) {
if (!fileHeader.isDirectory())
{
// 1 destDirName destFileName
String destFileName = "";
String destDirName = "";
destFileName = (destPath + fileHeader.getFileNameW()).replaceAll("/", "\\\\");
destDirName = destFileName.substring(0, destFileName.lastIndexOf("\\"));
// 2
File dir = new File(destDirName);
if (!dir.exists() || !dir.isDirectory()) {
dir.mkdirs();
}
//
// ERROR:
unOut = new FileOutputStream(dir);
archive.extractFile(fileHeader, unOut);
unOut.flush();
unOut.close();
}
fileHeader = archive.nextFileHeader();
}
archive.close();
} catch (RarException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(unOut);
}
}
Clear thing. You first create the directory /mnt/sdcard (variable dir is equal to destDirName in your code) and then you try to create a file with the same same. This will not work. Use a name like /mnt/sdcard/abc.rar and it might work. Here is how you create the corresponding File object:
File file = new File(dir, "abc.rar");
Btw: creating a directory called /mnt/sdcard will probably not work due to a lack of permissions. If it's an SD card, Android will do this job for you anyway. If it isn't an SD card, its not a good idea to create a directory with this name.
PS2: After further reviewing your code I see things which are not good style:
You are using the separater variable and indexOf/substring to find the parent directory.
You replace "/" (the android path separator) by "\" which is only used in Windows
You swap between File and string
You can simply get rid of all this by using File.getParent()/File.getParentFile()
Check if you have set permissions in your manifest file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Use Java to find a File within a directory using only a Name

I'm trying to write this script that takes an Excel sheet, gets all the names of files from the cells, and moves each of those files to a specific folder. I've already got most of the code done, I just need to be able to search for each file in the source directory using just its title. Another problem is that I'm searching for multiple file types (.txt, .repos, .xlsx, .xls, .pdf, and some files don't have extensions), I only can search by the file name without the extension.
In my findAndMoveFiles method, I've got an ArrayList of each File and a Guava Multimap of XSSFCells to Strings (a cell is one cell from the Excel file and a String is the name of the folder it needs to go into, one to many relationship) as parameters. What I've got right now for the method is this.
public static void findAndMoveFiles(List<File> files, Multimap<XSSFCell, String> innerCells) {
// For each file, get its values (folders), and put that file in each of those folders
for (XSSFCell cell : innerCells.keySet()) {
// find the file in the master directory
//Finder f = new Finder();
//if (f.canBeFound(FOLDER, cell.getStringCellValue())) {
File file = find(FOLDER, cell.getStringCellValue());
//System.out.println(file.getAbsolutePath());
//List<String> values = new ArrayList(innerCells.get(cell));
/*for (String folder : values) {
File copy = file;
if (copy != null) {
System.out.println(folder);
System.out.println(copy.getAbsolutePath());
if (copy.renameTo(new File("C:\\strobell\\" + folder + "\\" + copy.getAbsolutePath()))) {
System.out.println(copy.getName() + " has been moved successfully.");
} else {
System.out.println(copy.getName() + " has failed to move.");
}
}
}*/
//}
}
}
public static File find(File dir, String fileName) {
String files = "";
File[] listOfFiles = dir.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
files = listOfFiles[i].getAbsolutePath();
if (files.equals(fileName)) {
return listOfFiles[i];
}
}
}
return null;
}
I commented out parts because it wasn't working. I was getting NullPointerExceptions because some files were being returned as null. I know that it's returning null, but each file should be found.
If there are any 3rd party libraries that can do this, that would be amazing, I've been racking my brain on how to do this properly.
Instead of
File[] listOfFiles = dir.listFiles();
use
File[] listOfFiles = dir.list(new FileNameFilter() {
public boolean accept(File dir, String name) {
if( /* code to check if file name is ok */ ) {
return true;
}
return false;
}
}););
Then you can code your logic on the file names in the condition.

Categories