Take input files same in the order as in directory - java

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

Related

File[] can not handle multiple files

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.

How can move file from one directory to another

I am sorry,I modified my entire question.
I want to move some specific files(based on fileExtension) from one directory to another.
For this, I plan to write 2 functions names are ListOfFileNames and MovingFiles.
If you pass directorypath and filetype as a arguments toListOfFileNames(),it returns List<File>data.this list have entire file path.
MovingFiles() move files from source Directory to destination.
I tried MovingFiles function, in the following way.It's not working.
public void MovingFiles(String SourcePath,String directoryPath,String fileType) throws IOException
{
ExternalFileExecutions ExternalFileExecutionsObject=new ExternalFileExecutions();
List<File> fileNames=ExternalFileExecutionsObject.ListOfFileNames(SourcePath, fileType);
for (int fileIndex = 0; fileIndex < fileNames.size(); fileIndex++)
{
fileNames[fileIndex].renameTo(new File(directoryPath+"/" + fileNames[fileIndex].getName()))
}
}
I think, I need to convert List<File> to List<String>.that's why Previously I asked like that.
#tbodt replyed for my question.I failed to integrate his answer in my function.so I modified my question.
Again sorry, for modifying my entire question.
Thanks.
If you want to get a List<String> with each element the canonical path of the corresponding file, this is how:
List<String> list = new ArrayList<String>();
for (File file : finalListNames)
list.add(file.getCanonicalPath());
If that's not what you want, please clarify.
If understand you, using guava, I would write it in this way:
public static List<File> listOfFiles(){
return Lists.newArrayList(new File("source.txt"), new File("test.txt"));
}
public static List<String> listOfFileNames(){
return Lists.transform(listOfFiles(), new Function<File, String>() {
public String apply(File input) {
return input.getAbsolutePath();
}
});
}
You cant convert it. But what you can is that you can retrieve the name (what ever it is) while iterating through File list. A very handy solution would be LINQ, and select
This my answer. I used #tbodt code in my code.
public void MovingFiles(String SourcePath,String directoryPath,String fileType) throws IOException
{
ExternalFileExecutions ExternalFileExecutionsObject=new ExternalFileExecutions();
List<File> fileNames=ExternalFileExecutionsObject.ListOfFileNames(SourcePath, fileType);
List<String> list = new ArrayList<String>();
for (File file : fileNames)
list.add(file.getCanonicalPath());
for (int fileIndex = 0; fileIndex < list.size(); fileIndex++)
{
File file=new File(list.get(fileIndex));
file.renameTo(new File(directoryPath+"/",file.getName()));
}
}
you move a file from one directory to another directory with the same file name
try{
File afile =new File("C:\\folder1\\file1.txt");
if(afile.renameTo(new File("C:\\folder2\\" + afile.getName()))){
System.out.println("File is moved successful!");
}else{
System.out.println("File is failed to move!");
}
}catch(Exception e){
e.printStackTrace();
}

output files from the folder with no extension

help make this, the following code finds the txt files in the folder, and how to make it lists the names of files without extensions. Here's the code:
String list[] = new File("C:\\Users\\Eric\\Desktop\\txt\\").list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".txt");
}
});
for (int i = 0; i < list.length; i++){
if(myName.equals(list[i])) {
InputStreamReader reader = null;
try{
File file = new File("C:\\Users\\Eric\\Desktop\\txt\\" + list[i]);
reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
As you can see, right here:
if(myName.equals(list[i]))
Here is the list of files is compared to the value of myname, so could you help give to the comparison sheet and myName and list[i], the file extension was not considered)
such as the program displays that folder has the following layout: indeks.html hi.html, user entered indeks.html, compare and good, but how to create one so that the user has entered the code, and the program still has compared it with the list of files in directory?
If I understand correctly, you want to list all files in a folder, regardless of their extension. In this case, use:
String list[] = new File("C:\\Users\\Eric\\Desktop\\txt\\").list(new FileFilter() {
public boolean accept(File file) {
return file.isFile();
}
});
Edit: Ok, I did not understand you correctly. You want to strip the extension from the file name in your if statement. You can use substring combined with lastIndexOf:
String name = list[i];
String nameWithoutExtension = name.substring(0, name.lastIndexOf("."));
or, since all your extensions are ".txt", the following will work too:
String nameWithoutExtension = name.substring(0, name.lastIndexOf(".txt"));

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