Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
swdev:java:files:file_routines [2017/09/29 16:42] smayr created |
swdev:java:files:file_routines [2018/03/12 08:16] (current) smayr |
||
---|---|---|---|
Line 1: | Line 1: | ||
= File Routines = | = File Routines = | ||
+ | == Read File List == | ||
+ | === Method 1 === | ||
<code java> | <code java> | ||
+ | import java.io.File; | ||
+ | |||
public void listFilesForFolder(final File folder) { | public void listFilesForFolder(final File folder) { | ||
for (final File fileEntry : folder.listFiles()) { | for (final File fileEntry : folder.listFiles()) { | ||
Line 13: | Line 17: | ||
</ | </ | ||
+ | === Method 2 === | ||
+ | In Java 8, using the '' | ||
<code java> | <code java> | ||
final File folder = new File("/ | final File folder = new File("/ | ||
Line 25: | Line 31: | ||
</ | </ | ||
+ | === Method 3 === | ||
<code java> | <code java> | ||
+ | import java.io.File; | ||
+ | |||
File folder = new File("/ | File folder = new File("/ | ||
File[] listOfFiles = folder.listFiles(); | File[] listOfFiles = folder.listFiles(); | ||
Line 38: | Line 47: | ||
References: | References: | ||
* [[https:// | * [[https:// | ||
+ | |||
+ | === Method 4 === | ||
+ | <code java> | ||
+ | private void mnuOpenActionPerformed(java.awt.event.ActionEvent evt) { | ||
+ | try { | ||
+ | // Read text file | ||
+ | Path pth = Paths.get(System.getProperty(" | ||
+ | | ||
+ | Stream< | ||
+ | List< | ||
+ | System.out.println(" | ||
+ | System.out.println(asList); | ||
+ | | ||
+ | DefaultListModel< | ||
+ | //for(int i=0; i< asList.size(); | ||
+ | // String str = asList.get(i); | ||
+ | // lst.addElement(str); | ||
+ | //} | ||
+ | for(String str: asList) { | ||
+ | lst.addElement(str); | ||
+ | } | ||
+ | lstAdminRegions.setModel(lst); | ||
+ | } catch (IOException ex) { | ||
+ | Logger.getLogger(frmMain.class.getName()).log(Level.SEVERE, | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | == Example == | ||
+ | <code java> | ||
+ | /* | ||
+ | * To change this license header, choose License Headers in Project Properties. | ||
+ | * To change this template file, choose Tools | Templates | ||
+ | * and open the template in the editor. | ||
+ | */ | ||
+ | package com.voirtech.sdayearbook; | ||
+ | |||
+ | import java.io.File; | ||
+ | import javax.swing.JTree; | ||
+ | import javax.swing.tree.DefaultMutableTreeNode; | ||
+ | import javax.swing.tree.DefaultTreeModel; | ||
+ | |||
+ | /** | ||
+ | * | ||
+ | * @author mayr | ||
+ | */ | ||
+ | public class FileUtils | ||
+ | { | ||
+ | /* | ||
+ | * Usage: | ||
+ | try { | ||
+ | treeOrgTypes.setModel(new DefaultTreeModel(new DefaultMutableTreeNode(" | ||
+ | FileUtils.scanAndLoadDirectory(" | ||
+ | } catch (Exception exc) { | ||
+ | // Handle exception | ||
+ | } | ||
+ | */ | ||
+ | public static void scanAndLoadDirectory(String location, JTree tree) throws InterruptedException | ||
+ | { | ||
+ | // Creates a file with the location filename | ||
+ | //String location =" | ||
+ | File currentDir = new File(location); | ||
+ | |||
+ | // Get root of tree model | ||
+ | DefaultTreeModel modelTree | ||
+ | DefaultMutableTreeNode root = (DefaultMutableTreeNode) modelTree.getRoot(); | ||
+ | | ||
+ | displayDirectoryContents(currentDir, | ||
+ | } | ||
+ | | ||
+ | public static void displayDirectoryContents(File dir, DefaultTreeModel model, DefaultMutableTreeNode curRoot) throws InterruptedException | ||
+ | { | ||
+ | File[] files = dir.listFiles(); | ||
+ | |||
+ | for (File file : files) { | ||
+ | if (file == null) { | ||
+ | System.out.println(" | ||
+ | continue; | ||
+ | } | ||
+ | if (file.isDirectory()) { | ||
+ | // | ||
+ | // File is a Directory | ||
+ | // | ||
+ | if (file.listFiles() == null) { | ||
+ | // Skip null files | ||
+ | continue; | ||
+ | } | ||
+ | |||
+ | // Adds a new Directory node to tree root | ||
+ | DefaultMutableTreeNode newdir = new DefaultMutableTreeNode(file.getName()); | ||
+ | curRoot.add(newdir); | ||
+ | |||
+ | // Refresh the model to show changes | ||
+ | model.reload(); | ||
+ | |||
+ | // Recursively calls the function again to explore the contents folder | ||
+ | displayDirectoryContents(file, | ||
+ | | ||
+ | } else { | ||
+ | // | ||
+ | // File is not a directory | ||
+ | // | ||
+ | // Inserts a node newfile under selected node which is the root | ||
+ | DefaultMutableTreeNode selectednode = curRoot; | ||
+ | DefaultMutableTreeNode newfile = new DefaultMutableTreeNode(file.getName()); | ||
+ | model.insertNodeInto(newfile, | ||
+ | |||
+ | // Refresh the model to show the changes | ||
+ | model.reload(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ |