This question already has an answer here:
output files from the folder with no extension
(1 answer)
Closed 8 years ago.
Decided to put the question differently, suppose we have a file-test, there is a lot of different files, some of this type indeks.html, kiki.tht, lololo.bin and so on, to get the names of all files in a folder, you can use this code:
File folder = new File("C:\\test\\");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
System.out.println(listOfFiles[i].getName());
}
}
But how to display only the file name without the extension? Indeks.html looolo.tht not like (just remember files in the lot, there is no duplicate names), and the index and looolo)
String fileName = listOfFiles[i].getName();
int index = fileName.lastIndexOf('.');
if (index >= 0) {
fileName = fileName.substring(0, index);
}
System.out.println(fileName);
String fullName = file.getName();
String nameWithoutExtension = fullName();
int lastDot = fullName.lastIndexOf(".");
if (lastDot >= 0) {
nameWithoutExtension = nameWithoutExtension.substring(0, lastDot);
}
You can get the name of the file and Split the String with
String[] file = filename.Split("\\.");'
file[0] holds the basename, whereas file[1] holds the extension.
Have you tried:
listOfFiles.get(index).getName()
Cheers
Apache Commons IO has a class called FilenameUtils, which offers the method getBaseName(String). Just hand in your file's path for the argument.
Related
I have a scenario where around 5600 files are present.
I am able to retrieve the file names by using the below code:
String path = "D:\\Projects worked upon\\ANZ\\Anz new\\Files\\329703588_20160328124733595\\Output"; String files;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++)
{
if (listOfFiles[i].isFile())
{
files = listOfFiles[i].getName();
if (files.toLowerCase().endsWith(".xml"))
{
System.out.println(files);
}
, but i need only the first part For Eg:if the file name in folder is "abc_Transformed.xml" , i require only abc .. How to get it ?
You can use the substring method to find first string.
if (files.toLowerCase().endsWith(".xml"))
{
String result = files.substring(0, files.indexOf("_"));
System.out.println(result);
}
your whole code
String path = "D:\\Projects worked upon\\ANZ\\Anz new\\Files\\329703588_20160328124733595\\Output"; String files;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++)
{
if (listOfFiles[i].isFile())
{
files = listOfFiles[i].getName();
if (files.toLowerCase().endsWith(".xml"))
{
String result = files.substring(0, files.indexOf("_"));
System.out.println(result);
}
The information about the files is basically irrelevant. You are after some basic String manipulation functions.
You could try something using String.split() like:
String[] pieces = files.split("_");
String first = pieces[0]; // should be equal to "abc"
Or something using String.indexOf() and String.substr() like:
int indexOfUnderscore = files.indexOf("_");
String first = files.substr(0, indexOfUnderscore); // should be equal to "abc"
If you're new to Java, it's worth spending the time to review all the String functions.
I need some help.
I use this code to get the files in a folder as an array .
String fileDir = Directorty;
File dir = new File(fileDir);
FileFilter fileFilter = new WildcardFileFilter("*.html");
files = dir.listFiles(fileFilter);
But I want to write a file with only the files in that folder and not the path.
The result is:
[C:\Askeladden-17-12-2014\.html, C:\Askeladden-17-12-2014\barnetv.html, C:\Askeladden-17-12-2014\britiskebiler.html, C:\Askeladden-17-12-2014\danser.html, C:\Askeladden-17-12-2014\disipler.html, C:\Askeladden-17-12-2014\donald.html, C:\Askeladden-17-12-2014\ekvator.html, C:\Askeladden-17-12-2014\engelskspraak.html]
But I want to have it without the path
C:\Askeladden-17-12-2014\
I have been looking around the webs to find some answers, but no luck.
Using this:
strFiles = Arrays.toString(files);
Gives a string presented as an array with [] in each end, and I am not able to get
strFiles.replace("C:\\Askleladden" + date +"\\", "");
to work.
You have to iterate the files array and call getName() for each file:
String[] names = new String[files.length];
for (int i = 0; i < files.length; i++) {
names[i] = files[i].getName();
}
Java 1.8, if you want get as List, just remove cast and to array
String[] files = (String[])Arrays.asList(dir.listFiles(filefilter))
.stream().map(x->x.getName())
.collect(Collectors.toList())
.toArray();
Please find the solution below with proper comments.
import java.io.File;
import java.io.FileFilter;
public class fileNames {
public static void main(String args[]){
//Get the Directory of the FOLDER
String fileDir = "/MyData/StudyDocs/";
// Save it in a File object
File dir = new File(fileDir);
//FileFilter fileFilter = new WildcardFileFilter("*.html");
//Capture the list of Files in the Array
File[] files = dir.listFiles();
for(int i = 0; i < files.length; i++){
System.out.println(files[i].getName());
}
}
}
Use Files getName() method:
File file = new File("myFolder/myFile.png");
System.out.println(file.getName()); //Prints out myFile.png
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);
}
}
I am using SWT's FileDialog to let user select several files:
FileDialog dlg = new FileDialog(s, SWT.MULTI);
dlg.setFilterPath(somePath);
String fn = dlg.open();
if (fn != null)
String [] files = dlg.getFileNames()
While fn returns the absolute path to the directory, the files array contains relative paths. I would like to get an absolute path for each file. Is there a way of doing this in Java that works across platforms (Win, Linux, MacOS)?
You need to append the filename to the given filter path. To avoid worrying about path separators and the like, you can just use the File class. For example:
String[] filenames = dialog.getFileNames();
String filterPath = dialog.getFilterPath();
File[] selectedFiles = new File[filenames.length];
for(int i = 0; i < filenames.length; i++)
{
if(filterPath != null && filterPath.trim().length() > 0)
{
selectedFiles[i] = new File(filterPath, filenames[i]);
}
else
{
selectedFiles[i] = new File(filenames[i]);
}
}
If you need the path as a String, you can of course use the getAbsolutePath() method on the resultant Files.
I have a method for saving a File, but I don't know how to save files with consecutive names such as file001.txt, file002.txt, file003.txt, filennn.text
How can I achieve this?
You can use the following line of code to create the filenames.
String filename = String.format("file%03d.txt", fileNumber);
Then you will just use that string to create new files:
File file = new File(filename);
The following code will create files numbered 1 - 100:
for (int fileNumber = 1; fileNumber <= 100; fileNumber++) {
String filename = String.format("file%03d.txt", fileNumber);
File file = new File(filename);
}
Or, you will need to have a static variable that you increment every time you create a new file.
private static int fileNumber = 0;
public void createNewFile(){
String filename = String.format("file%03d.txt", fileNumber++);
File file = new File(filename);
}
It may be desirable for you to skip over writing to a file if it already exists.
This could be done easily by placing the following at the beginning of the for loop proposed by Justin 'jjnguy' Nelson, for example:
if(new File(fileName).exists())
{
continue;
}