File[] can not handle multiple files - java

I want to read multiple files from multiple directories. So far my program can read multiple files only until certain limit.
public void filesTobeCollected(String validpath) throws Exception {
this.folder = new File(validpath);
// this.file = new ArrayList<>();
File[] fileEntry = folder.listFiles();
System.out.println("directory path" + fileEntry.length);
/*****File upload read****/
for (int i = 0; i < fileEntry.length; i++) {
/*this line weeds out other directories/folders*/
if (fileEntry[i].exists()) {
file.add(String.valueOf(fileEntry[i]));
}
}
}
and I send value directories through this function,
public List<String> filesTobeTested(ArrayList<String> file) throws
Exception {
Set<String> test = new HashSet<>();
test.add("src/test/resources/gfd");
test.add("src/test/resources/rds");
test.add("src/test/resources/oiu");
//test.add("src/test/resources/pol");
System.out.println("directory path 2" + test);
for (int i = 0; i < 1; i++) {
for (Iterator<String> it = test.iterator(); it.hasNext(); ) {
String f = it.next();
filesTobeCollected(f);
System.out.println("Found" + f);
}
Here File[] fileEntry seems to have some limit as I push more directories using test.add("src/test/resources/obj") File[] fileEntry can't handle any more files. Is any alternative for File[] fileEntry incase of unlimited or unknown number of files?

Is any alternative for File[] fileEntry incase of unlimited or unknown number of files?
Yes, use ArrayList<File> wich will handle size automatically for you.
But this does not seem to be the problem because you know all the files you need to handle in the array creation so size is accurate File[] fileEntry = folder.listFiles();.
Remove this line
for (int i = 0; i < 1; i++) {
which seems useles... If problem remains, post your stacktrace

As deeply I recognized you, you want to collect those files in one.
In my case first I would recommend to collect the file(s) path or file(s) and add those in an ArrayList<File>, secondary read those file(s) and collect in ArrayList<byte[]>, later if you want to make it a single file, Surely you can use an ObjectOutputStream class to write the ArrayList<byte[]> Object and certainly create a single File
During this take care of File reading & writing bytes sizes don't crosses your Java Heap Space boundary.

Related

Take input files same in the order as in directory

I'm writing a code where in there a bunch of files that have to be taken as input from a directory.
The program works fine, but the problem comes up in the way the files are picked. In my directory when I do a sort the first file shown is file5521.3, but in my program the first file that is picked up is file5521.100. THis is pretty confusing.
I've also tried using Arrays.sort(list, NameFileComparator.NAME_COMPARATOR), but it also gives the same result as previous.
Below is my code.
void countFilesInDirectory(File directory, String inputPath) throws IOException {
File[] list = directory.listFiles();
Arrays.sort(list, NameFileComparator.NAME_COMPARATOR);
for (int i = 0; i < list.length; i++) {
System.out.println(list[i]);
}
tempPath = inputPath.substring(0, inputPath.lastIndexOf("\\") + 1) + "OP\\";
File outPath = new File(tempPath);
if (!outPath.exists()) {
outPath.mkdir();
}
File temp = new File(tempPath + "temp.txt");
FileOutputStream fos = new FileOutputStream(temp);
if (!temp.exists()) {
temp.createNewFile();
}
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
for (int i = 0; i < list.length; i++) {
System.out.println(list[i]);
setStatusText(i);
GenerateFiles(list[i].getAbsoluteFile().toString(), bw);
}
bw.write("</body>");
bw.close();
File newFile = new File(temp.getParent(), "Index.html");
Files.move(temp.toPath(), newFile.toPath());
}
please let me know how can I do this.
Working Solution with Last Modified Date
Arrays.sort(list, new Comparator<File>() {
public int compare(File f1, File f2) {
return Long.compare(f1.lastModified(), f2.lastModified());
}
});
The Comparator works fine with last modified date, but when I try it with below code. The result is same as previous.
Arrays.sort(list, new Comparator<File>() {
#Override
public int compare(File o1, File o2) {
return o1.getName().compareTo(o2.getName());
}
});
In my Windows Explorer It looks like below. I've sorted the filenames.
And my console output shows the below.
Thanks
If your OS is Windows XP or later, your files will be sorted using Numerical File Name Sorting.
Since you want to match the file order both in the program and in your File Explorer, you can either:
Follow the steps in the link to use Classical Literal Sorting which will change the way File Explorer displays files.
Create a custom Comparator that you pass into sort that will sort based on the actual VALUE of the number (e.g. 3 would come before 10 because 3 < 10)
You can use Apache Commons IO to sort files using many predefined Comparators or even combine these using CompositeFileComparator
Here is an example:
import org.apache.commons.io.comparator.*;
File dir = new File(".");
File[] files = dir.listFiles();
File[] filesSorted = DefaultFileComparator.DEFAULT_COMPARATOR.sort(files);
File[] filesReversed = DefaultFileComparator.DEFAULT_REVERSE.sort(files);
... and here is the list of available comparators

How to make Java list only writable files

I need to know how to know how to make Java's File.list(); method list only the files I am able to access or how to filter them out, cause now it lists files like $RecycleBin or Documents and Settings (I am on Win7) basically files I am unable to see in Win Explorer. Many Thanks.
The following will do it:
File[] subDirs = dir.listFiles( new FileFilter() {
#Override public boolean accept( File f ) {
return f.canWrite();
} } );
You could also use CanWriteFileFilter of apache.commons.io (http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/filefilter/CanWriteFileFilter.html)
Example, showing how to print out a list of the current directory's writable files:
File dir = new File(".");
String[] files = dir.list( CanWriteFileFilter.CAN_WRITE );
for ( int i = 0; i < files.length; i++ ) {
System.out.println(files[i]);
}

Scan all the files and Display the name and path of the file with the maximum size

It is a program (Java) that uses system calls to extract basic information related to the system.
Scan all the files and Display the name and path of the file with the maximum size
Can someone please help i am confused with the system calls.
THanks
Start by taking a look at java.io.File
Take a look at:
File#listFiles
File#length
The for statement
If you're very brave and have the time, you could also take a look at the File I/O API available in Java 7
This is what tou need. But you should read documentation about FILE API . http://docs.oracle.com/javase/7/docs/api/java/io/File.html
public class ListFiles
{
public static void main(String[] args)
{
// Directory path here
String path = ".";
String files="";
double maxBytes = 0;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++)
{
if (listOfFiles[i].isFile() && listOfFiles[i].length()>maxBytes)
{
maxBytes = listOfFiles[i].length()
files = listOfFiles[i].getName();
}
}
System.out.println(files);
}
}

sampling files from a folder

I have a folder containing 100000 files, and need to get 1000 files from this folder through random sampling. Are there any sample functions that I can use to sample from folder? In addition, how to copy the sampled files to another folder?
Random selection could follow along the following lines
File files[] = new File("/path/to/files").listFiles();
Map<Integer, File> selection = new HashMap<Integer, File>(1000);
while (selection.size() < 1000) {
int value = (int)Math.round(Math.random() * files.length);
if (!selection.containsKey(value)) {
selection.put(value, files[value]);
}
}
for (File file : selection.values()) {
System.out.println(file);
}
Essentially, you need to grab a list of the available files and the randomly pick through the list until you have enough of a sample. Check out java.io.File
There are plenty of examples of file copying over the net (and SO). If you're really stuck you could have a look the IO Trail or Apache Commons IO which I believe has a utility class capable of coping files
UPDATED
As suggested by Andrew, you could simply shuffle the file list and pull the first 1000 elements...
File files[] = new File("/path/to/files").listFiles();
List<File> selection = null;
List<File> fileList = new ArrayList<File>(Arrays.asList(files));
Collections.shuffle(fileList);
selection = fileList.subList(0, Math.min(1000, fileList.size()));
for (File file : selection) {
System.out.println(file);
}
Please try this
public static void main(String args[]) throws Exception
{
File f= new File("E:/Eclipse-Leo/Test/src/test/Desktop1");
List<File> randomFiles = new ArrayList<File>();
List<Integer> randNumber = new ArrayList<Integer>();
if(f != null && f.isDirectory()){
File[] files = f.listFiles();
Random randomGenerator = new Random();
int idx = 1;
while(idx <101 && idx >= 1)
{
int randTemp = randomGenerator.nextInt(1000);
if(!randNumber.contains(randTemp))
{
randNumber.add(randTemp);
randomFiles.add(files[randTemp]);
idx++;
}
}
}
}
File[] files = dir.listFiles();
Then just use files.length and a random number generator to index into the array.

How do i incorporate these code below into my current existing codes?

I was task to allocate only 1GB of space to store my videos in a particular file directory where it is going to auto-delete the oldest video file in that directory once its about to reach/hit 1GB?
And i eventually found these code but i was left with a problem on how to incorporate these example 1/2 codes into my current existing mainActivity.java file because of the differences in names like "dirlist,tempFile" compared with other examples 1/2 given to perform the task of size checking and deleting.
Sorry i'm kinna new in android/java therefore i don't really know what "fields" to change to suit my current coding needs? Can someone help on how am i going to complie these set of codes into a single set of code which perform the above mention functions??
My Current existing mainActivity.java
File dirlist = new File(Environment.getExternalStorageDirectory() + "/VideoList");
if(!(dirlist.exists()))
dirlist.mkdir();
File TempFile = new File(Environment.getExternalStorageDirectory()
+ "/VideoList", dateFormat.format(date) + fileFormat);
mediaRecorder.setOutputFile(TempFile.getPath());
(Example 1) code for summing up directory file size in a given folder..
private static long dirSize(File dir) {
long result = 0;
Stack<File> dirlist= new Stack<File>();
dirlist.clear();
dirlist.push(dir);
while(!dirlist.isEmpty())
{
File dirCurrent = dirlist.pop();
File[] fileList = dirCurrent.listFiles();
for (int i = 0; i < fileList.length; i++) {
if(fileList[i].isDirectory())
dirlist.push(fileList[i]);
else
result += fileList[i].length();
}
}
return result;
}
(Example 2) set of code for getting all the files in an array, and sorts them depending on their modified/created date. Then the first file in your array is your oldest file and delete it.
// no idea what are the parameters i should enter
// here for my case in mainActivity??
File directory = new File((**String for absolute path to directory**);
File[] files = directory.listFiles();
Arrays.sort(files, new Comparator<File>() {
#Override
public int compare(File f1, File f2)
{
return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
}});
file[0].delete();
This is a reference to your previous question: How do I put a capped maximum directory storage space in SD?. In the future, you should keep discussions about the same topic in the same question, rather than create 2 new identical questions.
In your Activity class lets say you define these 2 methods:
private void deleteOldestFile(File directory)
{
File[] files = directory.listFiles();
Arrays.sort(files, new Comparator<File>() {
#Override
public int compare(File f1, File f2)
{
return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
}});
files[0].delete();
}
private static long dirSize(File dir) {
long result = 0;
File[] fileList = dir.listFiles();
for(int i = 0; i < fileList.length; i++) {
if(fileList[i].isDirectory()) {
result += dirSize(fileList [i]);
} else {
// Sum the file size in bytes
result += fileList[i].length();
}
}
return result;
}
You can now do this with your code:
File dirlist = new File(Environment.getExternalStorageDirectory() + "/VideoList");
if(!(dirlist.exists()))
dirlist.mkdir();
Long directorySize = dirSize(dirlist);
if (directorySize > 1073741824) // this is 1GB in bytes
{
deleteOldestFile(dirlist);
}
File TempFile = new File(Environment.getExternalStorageDirectory()
+ "/VideoList", dateFormat.format(date) + fileFormat);
mediaRecorder.setOutputFile(TempFile.getPath());
So before setting the output file in that folder, it checks if the folder is > 1GB, and if so, deletes the oldest file first.
To be honest though, deleting the oldest file may not necessarily make the directory size < 1GB, so i would use a while loop to ensure that it is < 1GB like so:
while (directorySize > 1073741824)
{
deleteOldestFile(dirlist);
direcotrySize = dirSize(dirlist);
}

Categories

Resources