Filter the files by file extensions and
show the file names
package com.java2novice.files;
import java.io.File;
import java.io.FilenameFilter;
public class MyFileFilter
{
public static void
main(String a[])
{
File
file = new File("C:/MyFolder/");
String[]
files = file.list(new FilenameFilter()
{
public
boolean accept(File dir, String name)
{
if(name.toLowerCase().endsWith(".csv"))
{
return
true;
}
else
{
return
false;
}
}
});
for(String
f:files)
{
System.out.println(f);
}
}
}
Get list of all file objects from a
folder in java
package com.java2novice.files;
import java.io.File;
public class FilesListFromFolder
{
public static void
main(String a[])
{
File
file = new File("C:/MyFolder/");
File[]
files = file.listFiles();
for(File
f: files)
{
System.out.println(f.getName());
}
}
}
Get list of all file names from a
folder in java
package com.java2novice.files;
import java.io.File;
public class FileListFromFolder
{
public static void main(String
a[])
{
File
file = new File("C:/MyFolder/");
String[]
fileList = file.list();
for(String
name:fileList)
{
System.out.println(name);
}
}
}
All file operations
package com.java2novice.files;
import java.io.File;
public class MyFileOperations
{
public static void main(String[] a)
{
try
{
File
file = new File("fileName");
System.out.println(file.canRead());
System.out.println(file.canWrite());
System.out.println(file.createNewFile());
System.out.println(file.delete());
System.out.println(file.exists());
System.out.println(file.getAbsolutePath());
System.out.println(file.isDirectory());
System.out.println(file.isHidden());
System.out.println(file.list());
}
catch(Exception ex){}
}
}
No comments:
Post a Comment