How to get all text files from one folder using Java? - java

I need to read all ".txt" files from folder (user needs to select this folder).
Please advise how to do it?

you can use filenamefilter class it is pretty simple usage
public static void main(String[] args) throws IOException {
File f = new File("c:\\mydirectory");
FilenameFilter textFilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".txt");
}
};
File[] files = f.listFiles(textFilter);
for (File file : files) {
if (file.isDirectory()) {
System.out.print("directory:");
} else {
System.out.print(" file:");
}
System.out.println(file.getCanonicalPath());
}
}
just create an filenamefilter instance an override accept method how you want

Assuming you already have the directory, you can do something like this:
File directory= new File("user submits directory");
for (File file : directory.listFiles())
{
if (FileNameUtils.getExtension(file.getName()).equals("txt"))
{
//dom something here.
}
}
The FileNameUtils.getExtension() can be found here.
Edit: What you seem to want to do is to access the file structure from the web browser. According to this previous SO post, what you want to do is not possible due to security reasons.

You need to read the directory and iterate inside it.
it is more a question on Java access to file systems than about MVC

I wrote the following function that will search for all the text files inside a directory.
public static void parseDir(File dirPath)
{
File files[] = null;
if(dirPath.isDirectory())
{
files = dirPath.listFiles();
for(File dirFiles:files)
{
if(dirFiles.isDirectory())
{
parseDir(dirFiles);
}
else
{
if(dirFiles.getName().endsWith(".txt"))
{
//do your processing here....
}
}
}
}
else
{
if(dirPath.getName().endsWith(".txt"))
{
//do your processing here....
}
}
}
see if this helps.

provide a text box to user to enter the path of directory.
File userDir=new File("userEnteredDir");
File[] allfiles=useDir.listFiles();
Iterate allFiles to filter .txt files using getExtension() method

Related

Efficient way of crawling file system using threads Java

I am currently working on a java project that does OCR in PDFs from the file system for searching its content.
In this project I am searching in a folder that the user specifies. I am taking PDFs content by OCR and checking them whether the keywords provided by the user are included in them.
I am trying to make sure when an OCR is done on a PDF, the crawling or the traversal to continue (necessarily on another thread or few threads), so that the performance of the system is not reduced dramatically.
Is there a way to accomplish this? I've included the traversing code I am using below..
public void traverseDirectory(File[] files) {
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
traverseDirectory(file.listFiles());
} else {
String[] type = file.getName().toString().split("\\.(?=[^\\.]+$)");
if (type.length > 1) {
if (type[1].equals("pdf")) {
//checking content goes here
}
}
}
}
}
}
You can just use Files.walkFileTree:
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
PdfOcrService service = ...
Path rootPath = Paths.get("/path/to/your/directory");
Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() {
public void visitFile(Path path, BasicFileAttributes attrs) {
executor.submit(() -> {
service.performOcrOnFile(path);
});
}
});

How to list all text files in a directory [duplicate]

I need to read all ".txt" files from folder (user needs to select this folder).
Please advise how to do it?
you can use filenamefilter class it is pretty simple usage
public static void main(String[] args) throws IOException {
File f = new File("c:\\mydirectory");
FilenameFilter textFilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".txt");
}
};
File[] files = f.listFiles(textFilter);
for (File file : files) {
if (file.isDirectory()) {
System.out.print("directory:");
} else {
System.out.print(" file:");
}
System.out.println(file.getCanonicalPath());
}
}
just create an filenamefilter instance an override accept method how you want
Assuming you already have the directory, you can do something like this:
File directory= new File("user submits directory");
for (File file : directory.listFiles())
{
if (FileNameUtils.getExtension(file.getName()).equals("txt"))
{
//dom something here.
}
}
The FileNameUtils.getExtension() can be found here.
Edit: What you seem to want to do is to access the file structure from the web browser. According to this previous SO post, what you want to do is not possible due to security reasons.
You need to read the directory and iterate inside it.
it is more a question on Java access to file systems than about MVC
I wrote the following function that will search for all the text files inside a directory.
public static void parseDir(File dirPath)
{
File files[] = null;
if(dirPath.isDirectory())
{
files = dirPath.listFiles();
for(File dirFiles:files)
{
if(dirFiles.isDirectory())
{
parseDir(dirFiles);
}
else
{
if(dirFiles.getName().endsWith(".txt"))
{
//do your processing here....
}
}
}
}
else
{
if(dirPath.getName().endsWith(".txt"))
{
//do your processing here....
}
}
}
see if this helps.
provide a text box to user to enter the path of directory.
File userDir=new File("userEnteredDir");
File[] allfiles=useDir.listFiles();
Iterate allFiles to filter .txt files using getExtension() method

Generating URI's from files in a File class in Java FX?

I'm writing code for a music player in Java FX, I use the MediaPlayer class, which is initialized by a Media class. So far I think that the sources for the Media constructors must be URI in Strings, so I've writen this code for adding a list of song files to a playlist and so playing such list:
public void setPlaylist (List<File> lista) {
songsList.clear();
for (File s : lista) {
songsList.add(s.toURI());
}
}
This works fine. However, when I want to get a File containing the path of a folder, and inputing each file's name in URI format I get some trouble, this is what I've tried so far:
public void setPlaylist (File folder) {
songsList.clear();
for (String s : folder.list()) {
try {
songsList.add(new URI("file:///" + (folder + "\\" + s).replace("\\", "/").replaceAll(" ", "%20")));
} catch (URISyntaxException ex) {
Logger.getLogger(PlayList.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
I'm getting error logs like this:
SEVERE: null java.net.URISyntaxException: Illegal character in path at
index 78:
file:///C:/Users/Diego%20Aguilar/Music/3%20Grandes%20de%20la%20Banda/AlbumArt_{9AEABE24-F5A2-441C-A71A-D061E000A9BA}_Large.jpg
Use File#toURI() as you were using before to avoid running into encoding issues and make use of a FilenameFilter to restrict the list to media files only. Here's how the code would look then.
public void setPlaylist (File folder) {
songsList.clear();
File[] musicFiles = folder.listFiles(new FilenameFilter() {
#Override
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3") || name.endsWith(".m4a"));
}
});
for (File file : musicFiles) {
songsList.add(file.toURI());
}
}
See JavaDocs: FilenameFilter, File#toURI()
Instead of using String s : folder.list() use File s : folder.listFiles() ... then use the URL from the files.
Your file URI contains an angular bracket {, which is causing SEVERE: null java.net.URISyntaxException
You need to have a valid file path to create a proper URI.
Here is the link to URI RFC for referring what is allowed and what is not allowed in a URL.

How do I automatically convert all javadoc package.html files into package-info.java files?

We use a lot of legacy package.html files in our project and we want to convert them to package-info.java files. Doing that manually isn't an option (way too many files). Is there a good way to automate that?
We want to convert them for a couple of reasons:
From the javadoc specs: This file is new in JDK 5.0, and is preferred over package.html.
To not mix both types of files in the same codebase
To avoid that Intellij/Eclipse builds put those *.html files in our classes dirs (and possibly in a release binary jars) so they behave like our other normal html resources.
You may need to change the directory separator if you're not running windows. Also, the conversion is a bit of a hack, but it should work. Out of curiosity, how many packages do you have that manual isn't an option?
public class Converter {
public static void main(String[] args) {
File rootDir = new File(".");
renamePackageToPackageInfo(rootDir);
}
private static void renamePackageToPackageInfo(File dir) {
File[] files = dir.listFiles(new FilenameFilter() {
#Override
public boolean accept(File dir, String name) {
return "package.html".equals(name);
}
});
for (File file : files) {
convertFile(file);
}
// now recursively rename all the child directories.
File[] dirs = dir.listFiles(new FileFilter() {
#Override
public boolean accept(File pathname) {
return pathname.isDirectory();
}
});
for (File subdir : dirs) {
renamePackageToPackageInfo(subdir);
}
}
private static void convertFile(File html) {
// determine the FQN package name
String fqpn = getPackageName(html);
// check if package-info.java already exists
File packageInfo = new File(html.getParent(), "package-info.java");
if (packageInfo.exists()) {
System.out.println("package-info.java already exists for package: "+fqpn);
return;
}
// create the i/o streams, and start pumping the data
try {
PrintWriter out = new PrintWriter(packageInfo);
BufferedReader in = new BufferedReader(new FileReader(html));
out.println("/**");
// skip over the headers
while (true) {
String line = in.readLine();
if (line.equalsIgnoreCase("<BODY>"))
break;
}
// now pump the file into the package-info.java file
while (true) {
String line = in.readLine();
if (line.equalsIgnoreCase("</BODY>"))
break;
out.println(" * " + line);
}
out.println("*/");
out.println("package "+fqpn+";");
out.close();
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// queue the package.html file for deletion
//html.deleteOnExit();
}
private static String getPackageName(File file) {
StringBuilder path = new StringBuilder(file.getParent());
// trim the first two characters (./ or .\)
path.delete(0, 2);
// then convert all separators into . (HACK: should use directory separator property)
return path.toString().replaceAll("\\\\", ".");
}
}
The IntelliJ guys have made an intention to do this for all files. It's been resolved and will probably be released in the next IntelliJ release.
To do this in batch mode in IDEA:
In settings, activate the inspection gadget "'package.html' may be converted to 'package-info.java' inspection"
Open a package.html file
You see a banner fix the inspection on top the file
Click on the settings icon at the right on the banner
Select "Run inspection on" >> "Whole project"
Click on "Convert to package-info.java" >> OK
Optionally remove the inappropriate lines (sed -i "/Put #see and #since/d" `find . -name "package-info.java"`)

How do I restrict JFileChooser to a directory?

I want to limit my users to a directory and its sub directories but the "Parent Directory" button allows them to browse to an arbitrary directory.
How should I go about doing that?
Incase anyone else needs this in the future:
class DirectoryRestrictedFileSystemView extends FileSystemView
{
private final File[] rootDirectories;
DirectoryRestrictedFileSystemView(File rootDirectory)
{
this.rootDirectories = new File[] {rootDirectory};
}
DirectoryRestrictedFileSystemView(File[] rootDirectories)
{
this.rootDirectories = rootDirectories;
}
#Override
public File createNewFolder(File containingDir) throws IOException
{
throw new UnsupportedOperationException("Unable to create directory");
}
#Override
public File[] getRoots()
{
return rootDirectories;
}
#Override
public boolean isRoot(File file)
{
for (File root : rootDirectories) {
if (root.equals(file)) {
return true;
}
}
return false;
}
}
You'll obviously need to make a better "createNewFolder" method, but this does restrict the user to one of more directories.
And use it like this:
FileSystemView fsv = new DirectoryRestrictedFileSystemView(new File("X:\\"));
JFileChooser fileChooser = new JFileChooser(fsv);
or like this:
FileSystemView fsv = new DirectoryRestrictedFileSystemView( new File[] {
new File("X:\\"),
new File("Y:\\")
});
JFileChooser fileChooser = new JFileChooser(fsv);
You can probably do this by setting your own FileSystemView.
The solution of Allain is almost complete. Three problems are open to solve:
Clicking the "Home"-Button kicks the user out of restrictions
DirectoryRestrictedFileSystemView is not accessible outside the package
Starting point is not Root
Append #Override to DirectoryRestrictedFileSystemView
public TFile getHomeDirectory()
{
return rootDirectories[0];
}
set class and constructor public
Change JFileChooser fileChooser = new JFileChooser(fsv); into JFileChooser fileChooser = new JFileChooser(fsv.getHomeDirectory(),fsv);
I use it for restricting users to stay in a zip-file via TrueZips TFileChooser and with slight modifications to the above code, this works perfectly. Thanks a lot.
No need to be that complicated. You can easily set selection mode of a JFileChooser like this
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fc.setMultiSelectionEnabled(false);
You can read more reference here How to Use File Choosers

Categories