I would like to read a file from a directory. In this directory there are eight additional files with the same extension (.csv). Likewise, the file name is not directly known.
The name of the file looks like this:
test_file_1_2017_06_24.csv
It can change however, if I call the directory the next day . Then the file name is:
test_file_2_2017_06_25.csv or test_file_1_2017_06_25.
The name of the file and the date change.
Is there a way to read the file "variable" in Java or to read the file but donĀ“t know the exactly name? The directory is always the same ("H:/) (After the file read, then the file respectively the resulting string is furtherprocessed with split() ).
Thanks for helpfull answers!
Edit: Read the directory and shows only csv-Files
File dir = new File("H:/");
File[] fileArray = dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".csv");
}
});
for(File f: fileArray){
System.out.println(f.getName());
}
File directory = new File("H:/");
File[] allFilesInDir = directory.listFiles();
See https://docs.oracle.com/javase/7/docs/api/java/io/File.html#listFiles().
You can also provide a FilenameFilter to get only .csv files:
File[] allCsvFiles = directory.listFiles( new FilenameFilter() {
public boolean accept(File dir, String name) {
if ( name.toUpperCase().endsWith(".CSV") ) {
return new File(dir,name).isFile(); // Make sure we don't accept sub-directories ending in .csv
}
}
});
You can use the following code to go through a folder, check for csv files and work with it. Hope it helps.
File folder = new File("H:/"); /*path to your folder*/
String[] filesPresent = folder.list();
if(filesPresent.length==0){
System.out.println("Nothing to delete");
}else{
for(String fileName : filesPresent){ // looping through files in the directory
if(fileName.toLowerCase().endsWith(".csv") && (new File(fileName).isFile())){
//this is a csv file.
//you can do your operations here
File file = new File(fileName);
//now you can do any file operations required with the file object
}
}
}
Related
I'd like recursively search a directory, which might have folders and files inside, for a certain file extension (e.g. .7z).
I use an array in case of a folder, and want to add whatever is a match to an ArrayList.
I add the file matches directly to ArrayList.
Unfortunately, the logic with directory doesn't work right.
Could you help further?
p.s. I'm aware there is an elite solution with Path Filtering with Java 8 but unfortunately can't use it for my project.
//2) go through the extracted directory and look for .7z recursively
File dir = new File(destDir_PATH);
File[] dirFiles = dir.listFiles();
ArrayList<File> matches2 = new ArrayList<File>();
for (File file : dirFiles) {
if (file.isDirectory()) {
File[] matches = dir.listFiles(new FilenameFilter()
{
public boolean accept(File dir, String name)
{
return name.endsWith(".7z");
}
});
matches2.addAll(Arrays.asList(matches));
}
else if (file.isFile()) {
if (file.getName().endsWith(".7z")){
matches2.add(file);
};
}
};
if (file.isDirectory()) {
File[] matches = dir.listFiles(new FilenameFilter()
here, instead of listing the files of the directory you encountered, you again filter the files of your initial ("root") directory. You should change this to
if (file.isDirectory()) {
File[] matches = file.listFiles(new FilenameFilter()
I need to read an xml-file that has as dynamic name. File name is something like "1234_employees.xml" in which the digits change daily. There is only one .xml in the folder.
Doesn't regex this work for files/paths?
Something like:
File myxml = new File("*employees.xml");
You can use WildcardFileFilter:
File dir = new File(".");
FileFilter fileFilter = new WildcardFileFilter("*employees.xml");
File employeesXml = dir.listFiles(fileFilter)[0];
You will need to install this library:
https://mvnrepository.com/artifact/org.apache.commons/commons-io/1.3.2
You can use a FilenameFilter as below
for (File inputFile : yourDirectory.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith("employees.xml");
}
})) {
// process inputFile here
}
I try to make a java code that will search for every directory that is starting with "test-" and then will copy files to it.
Currently i have this as code
File dir = new File("C:\\Users\\test\\Desktop\\test");
File[] foundFiles = dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith("test-");
But, i can't figure out how to let it now copy files to the directories with that as prefix..
You would probably want to do something like:
for(File cpy:listOfFilesToCopy){
for(File dir:foundFiles){
Files.copy(cpy,dir);
}
}
I want to figure it out whether a csv file present in a particular directory or not
Something along the lines of
public boolean containsCSVFiles( File aDirectory ){
return !aDirectory.listFiles( new FilenameFilter(){
public boolean accept(File dir, String name){
return name.endsWith( ".csv" );
}
}).isEmpty();
}
should do it (not tested in my IDE, so I hope I did not make to much typos).
Simply parse the directory to look for .csv files....
File f = new File("D:/Shashank/");
if(f.isDirectory())
{
File[] file= f.listFiles();
for(File f1 :file)
{
if(f1.getName().endsWith(".csv"))
{
System.out.println("File Found"+f1.getName());
}
}
}
You can make it recursive too, to look into the sub directories.
You don't really explain what you want to do, but between the listFiles() and exists() methods of the java.io.File class, you should be able to get it done.
Hi i'm working on a simple program and for the set up of the program i need the program to check a directory for zip files and any zip files in there need to be moved into another folder.
Lets say i have folder1 and it contains 6 zip files and then i have another folder called folder2 that i need all the zips and only the zips in folder1 moved to folder2
Thank you for any help one this problem.
Btw i'm a noob so any code samples would be greatly appreciated
For each file in folder1, use String#endsWith() to see if the file name ends with ".zip". If it does, move it to folder2. FilenameFilter provides a nice way to do this (though it's not strictly necessary).
It would look something like this (not tested):
File f1 = new File("/path/to/folder1");
File f2 = new File("/path/to/folder2");
FilenameFilter filter = new FilenameFilter()
{
#Override public boolean accept(File dir, String name)
{
return name.endsWith(".zip");
}
};
for (File f : f1.listFiles(filter))
{
// TODO move to folder2
}
The pattern of matching "*.zip" in a filesystem is called "file globbing." You can easily select all of these files with a ".zip" file glob using this documentation:
Finding Files. java.nio.file.PathMatcher is what you want. Alternatively, you can list directories as normal and use the name of the file and the ".endsWith()" method of String which will do something similar.
Use a FilenameFilter
String pathToDir = "/some/directory/path";
File myDir = new File(pathToDir);
File[] zipFiles = myDir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".zip")
}
});
List all file in baseDir, if it ends with '.zip' moves it to destDir
// baseDir = folder1
// destDir = folder2
File[] files = baseDir.listFiles();
for (int i=0; i<files.length; i++){
if (files[i].endsWith(".zip")){
files[i].renameTo(new File(destDir, files[i].getName()));
}
}
API of renameTo
My solution:
import java.io.*;
import javax.swing.*;
public class MovingFile
{
public static void copyStreamToFile() throws IOException
{
FileOutputStream foutOutput = null;
String oldDir = "F:/CAF_UPLOAD_04052011.TXT.zip";
System.out.println(oldDir);
String newDir = "F:/New/CAF_UPLOAD_04052011.TXT.zip.zip"; // name the file in destination
File f = new File(oldDir);
f.renameTo(new File(newDir));
}
public static void main(String[] args) throws IOException
{
copyStreamToFile();
}
}