Wednesday, March 16, 2016

Java I/O Examples : How To Display Directory Contents

Intro


This examples, will demonstrates how to display directory contents using Java program.

Examples


package com.freesamplecode.java.io;

import java.io.File;
import java.io.IOException;

public class DisplayDirectoryContentsDemo {
 public static void main(String[] args) {

  try {
   File dir = new File("D:/temp");

   File[] listOfFile = dir.listFiles();

   for (File file : listOfFile) {
    if (file.isDirectory()) {
     System.out.print("Directory ==> ");
    } else {
     System.out.print("File ==> ");
    }

    System.out.println(file.getCanonicalPath());

   }

  } catch (IOException e) {
   e.printStackTrace();
  }

 }
}


Output


File ==> D:\temp\All.log
File ==> D:\temp\Angger Tulus Abdillah.zip
File ==> D:\temp\BrpUpdateHosts.bat
File ==> D:\temp\buat patria.zip
File ==> D:\temp\cap-server.jar
File ==> D:\temp\console_log.html
File ==> D:\temp\console_log.html.bak
File ==> D:\temp\contoh_heading.html
File ==> D:\temp\contoh_heading.html.bak
File ==> D:\temp\core-engine.jar
File ==> D:\temp\corp.war
File ==> D:\temp\create_table.sql
Directory ==> D:\temp\Curanmor
Directory ==> D:\temp\data
File ==> D:\temp\data.sql
File ==> D:\temp\data.zip
File ==> D:\temp\debugApplet.txt
File ==> D:\temp\document_write.html
File ==> D:\temp\Draft BRP Production Deployment Procedure.doc
File ==> D:\temp\error_log_cif.txt
File ==> D:\temp\file-writer.txt
Directory ==> D:\temp\hazelcast-all
File ==> D:\temp\hazelcast-all.jar
File ==> D:\temp\inner_html.html
Directory ==> D:\temp\IT Article
Directory ==> D:\temp\kaskus
File ==> D:\temp\log4j.properties
File ==> D:\temp\logging.jar
File ==> D:\temp\loggingviewer-server.jar
File ==> D:\temp\logo_BII_2.bmp
File ==> D:\temp\Menu Mobile Token.docx
File ==> D:\temp\PrimeCashApp01.out
File ==> D:\temp\PrismaGatewayUI.jar
File ==> D:\temp\Production Deployment Checklist.docx
File ==> D:\temp\REC NOT FND IMTBL .txt
File ==> D:\temp\SchedulerServlet.class
File ==> D:\temp\slf4j-log4j12-1.5.10.jar
File ==> D:\temp\testfile.txt
File ==> D:\temp\testing_user_id.csv
File ==> D:\temp\unnamed.jpg
File ==> D:\temp\wllog4j.jar


Screenshot

How To Display Directory Contents In Java

Related Posts:

0 comments: