Apache Commons IO - คู่มือฉบับย่อ
ไลบรารี Apache Commons IO จัดเตรียมคลาสยูทิลิตี้ต่างๆสำหรับการดำเนินการทั่วไปสำหรับ File IO ซึ่งครอบคลุมกรณีการใช้งานที่หลากหลาย ช่วยหลีกเลี่ยงการเขียนโค้ดสำเร็จรูป
Apache Commons IO library มีคลาสสำหรับหมวดหมู่ต่อไปนี้ -
คลาสยูทิลิตี้
ชั้นเรียนเหล่านี้ซึ่งอยู่ภายใต้ org.apache.commons.ioแพ็คเกจให้การเปรียบเทียบไฟล์และสตริง ต่อไปนี้เป็นตัวอย่างบางส่วน
IOUtils- จัดเตรียมวิธีการยูทิลิตี้สำหรับการอ่านเขียนและคัดลอกไฟล์ วิธีการทำงานกับ InputStream, OutputStream, Reader และ Writer
FilenameUtils- จัดเตรียมวิธีการทำงานกับชื่อไฟล์โดยไม่ต้องใช้ File Object ทำงานบนระบบปฏิบัติการที่แตกต่างกันในลักษณะเดียวกัน
FileUtils - จัดเตรียมวิธีการจัดการไฟล์เช่นการย้ายการเปิดการตรวจสอบการมีอยู่การอ่านไฟล์ ฯลฯ วิธีการเหล่านี้ใช้ File Object
IOCase - จัดเตรียมวิธีการจัดการและเปรียบเทียบสตริง
FileSystemUtils - ระบุวิธีการรับพื้นที่ว่างบนดิสก์ไดรฟ์
LineIterator - ให้วิธีที่ยืดหยุ่นในการทำงานกับไฟล์ตามบรรทัด
กรองคลาส
กรองคลาสที่อยู่ภายใต้แพ็กเกจ org.apache.commons.io.filefilter มีวิธีการกรองไฟล์ตามเกณฑ์ตรรกะแทนการเปรียบเทียบที่น่าเบื่อตามสตริง ต่อไปนี้เป็นตัวอย่างบางส่วน
NameFileFilter - กรองชื่อไฟล์สำหรับชื่อ
WildcardFileFilter - กรองไฟล์โดยใช้สัญลักษณ์ตัวแทนที่ให้มา
SuffixFileFilter- กรองไฟล์ตามคำต่อท้าย ใช้ในการดึงไฟล์ทั้งหมดในประเภทใดประเภทหนึ่ง
PrefixFileFilter - กรองไฟล์ตามคำนำหน้า
OrFileFilter- ให้เงื่อนไขหรือตรรกะในรายการตัวกรองไฟล์ ส่งคืนค่าจริงหากตัวกรองใด ๆ ในรายการส่งคืนจริง มิฉะนั้นจะส่งกลับเท็จ
AndFileFilter- จัดเตรียมเงื่อนไขและตรรกะในรายการตัวกรองไฟล์ ส่งคืนเท็จหากตัวกรองใด ๆ ในรายการส่งคืนเท็จ มิฉะนั้นจะส่งกลับจริง
คลาส File Monitor
คลาสการตรวจสอบไฟล์ที่อยู่ภายใต้ org.apache.commons.io.monitorแพคเกจให้การควบคุมเพื่อติดตามการเปลี่ยนแปลงในไฟล์หรือโฟลเดอร์เฉพาะและอนุญาตให้ดำเนินการตามการเปลี่ยนแปลง ต่อไปนี้เป็นตัวอย่างบางส่วน
FileEntry- ระบุสถานะของไฟล์หรือไดเร็กทอรี แอตทริบิวต์ของไฟล์ในช่วงเวลาหนึ่ง
FileAlterationObserver - แสดงสถานะของไฟล์ด้านล่างไดเร็กทอรีรูทตรวจสอบระบบไฟล์และแจ้งให้ผู้ฟังทราบถึงการสร้างเปลี่ยนแปลงหรือลบเหตุการณ์
FileAlterationMonitor - แสดงถึงเธรดที่สร้างเธรดการตรวจสอบที่ทริกเกอร์ FileAlterationObserver ที่ลงทะเบียนในช่วงเวลาที่กำหนด
คลาสเปรียบเทียบ
คลาสการตรวจสอบไฟล์ภายใต้ org.apache.commons.io.comparator แพ็คเกจช่วยให้เปรียบเทียบและจัดเรียงไฟล์และไดเรกทอรีได้อย่างง่ายดาย
NameFileComparator - เปรียบเทียบชื่อของสองไฟล์
SizeFileComparator - เปรียบเทียบขนาดของสองไฟล์
LastModifiedFileComparator - เปรียบเทียบวันที่แก้ไขล่าสุดของสองไฟล์
สตรีมคลาส
มีการใช้งาน InputStream หลายรายการภายใต้แพ็คเกจ org.apache.commons.io.input และของ OutputStream ภายใต้แพ็คเกจ org.apache.commons.io.output เพื่อทำงานที่มีประโยชน์บนสตรีม ต่อไปนี้เป็นตัวอย่างบางส่วน
NullOutputStream - ดูดซับข้อมูลทั้งหมดที่ส่งมาพร้อมกับข้อผิดพลาดใด ๆ
TeeOutputStream - ส่งเอาต์พุตไปยังสองสตรีม
ByteArrayOutputStream - คลาส JDK เวอร์ชันที่เร็วกว่า
CountingOutputStream - นับจำนวนไบต์ที่ส่งผ่านสตรีม
ProxyOutputStream - เปลี่ยนการโทรเป็นสตรีมพร็อกซี
LockableFileWriter - FileWriter เพื่อสร้างไฟล์ล็อคและอนุญาตให้จัดการการล็อกไฟล์ข้ามเธรดได้อย่างง่ายดาย
ในบทนี้เราจะเรียนรู้เกี่ยวกับการตั้งค่าสภาพแวดล้อมภายในของ Apache Commons IO และวิธีตั้งค่าเส้นทางของ Commons IO สำหรับ Windows 2000 / XP, Windows 95/98 / ME เป็นต้นนอกจากนี้เรายังจะเข้าใจเกี่ยวกับตัวแก้ไขจาวายอดนิยมและ วิธีดาวน์โหลดไฟล์ Commons IO archive
การตั้งค่าสภาพแวดล้อมท้องถิ่น
หากคุณยังคงเต็มใจที่จะตั้งค่าสภาพแวดล้อมของคุณสำหรับภาษาการเขียนโปรแกรม Java ส่วนนี้จะแนะนำคุณเกี่ยวกับวิธีดาวน์โหลดและตั้งค่า Java บนเครื่องของคุณ โปรดทำตามขั้นตอนด้านล่างเพื่อตั้งค่าสภาพแวดล้อม
Java SE มีให้ดาวน์โหลดฟรีที่ลิงค์ https://www.oracle.comคุณจึงดาวน์โหลดเวอร์ชันที่อิงตามระบบปฏิบัติการของคุณ
ทำตามคำแนะนำเพื่อดาวน์โหลด Java และเรียกใช้ไฟล์ .exeเพื่อติดตั้ง Java บนเครื่องของคุณ เมื่อคุณติดตั้ง Java บนเครื่องของคุณแล้วคุณจะต้องตั้งค่าตัวแปรสภาพแวดล้อมให้ชี้ไปที่ไดเร็กทอรีการติดตั้งที่ถูกต้อง
เส้นทางสำหรับ Windows 2000 / XP
ให้เราเข้าใจวิธีตั้งค่าเส้นทางสำหรับ Windows 2000 / XP เราสมมติว่าคุณได้ติดตั้ง Java ในc:\Program Files\java\jdk ไดเรกทอรี
คลิกขวาที่ 'My Computer' และเลือก 'Properties'
คลิกที่ปุ่ม "ตัวแปรสภาพแวดล้อม" ใต้แท็บ "ขั้นสูง"
ตอนนี้เปลี่ยนตัวแปร 'Path' เพื่อให้มันมีพา ธ ไปยังไฟล์ปฏิบัติการ Java ตัวอย่างหากเส้นทางถูกตั้งค่าเป็น 'C: \ WINDOWS \ SYSTEM32' ให้เปลี่ยนเส้นทางของคุณเพื่ออ่าน'C:\WINDOWS\SYSTEM32;c:\Program Files\java\jdk\bin'.
เส้นทางสำหรับ Windows 95/98 / ME
ให้เราเข้าใจวิธีตั้งค่าเส้นทางสำหรับ Windows 95/98 / ME เราสมมติว่าคุณได้ติดตั้ง Java ในc:\Program Files\java\jdk ไดเรกทอรี
แก้ไขไฟล์ 'C: \ autoexec.bat' และเพิ่มบรรทัดต่อไปนี้ในตอนท้าย - 'SET PATH =% PATH%; C: \ Program Files \ java \ jdk \ bin'
เส้นทางสำหรับ Linux, UNIX, Solaris, FreeBSD
ควรตั้งค่า PATH ตัวแปรสภาพแวดล้อมให้ชี้ไปที่ตำแหน่งที่ติดตั้งไบนารี Java โปรดดูเอกสารประกอบเชลล์ของคุณหากคุณมีปัญหาในการดำเนินการนี้
ตัวอย่างเช่นหากคุณใช้ bash เป็นเชลล์ของคุณคุณจะต้องเพิ่มบรรทัดต่อไปนี้ต่อท้าย '.bashrc: export PATH = / path / to / java: $ PATH'
บรรณาธิการ Java ยอดนิยม
ในการเขียนโปรแกรม Java คุณต้องมีโปรแกรมแก้ไขข้อความ มี IDE ที่ซับซ้อนมากมายในตลาด แต่ในตอนนี้คุณสามารถพิจารณาข้อใดข้อหนึ่งต่อไปนี้ -
Notepad - บนเครื่อง Windows คุณสามารถใช้โปรแกรมแก้ไขข้อความง่ายๆเช่น Notepad (แนะนำสำหรับบทช่วยสอนนี้), TextPad
Netbeans - เป็น Java IDE ที่เป็นโอเพ่นซอร์สและฟรีและสามารถดาวน์โหลดได้จาก https://www.netbeans.org/index.html.
Eclipse - นอกจากนี้ยังเป็น Java IDE ที่พัฒนาโดยชุมชนโอเพนซอร์ส eclipse และสามารถดาวน์โหลดได้จาก https://www.eclipse.org/.
ดาวน์โหลด Common IO Archive
ดาวน์โหลดไฟล์ Apache Common IO jar เวอร์ชันล่าสุดจาก commons-io-2.6- bin.zip ซึ่งมีอยู่ที่download_io.cgiในขณะที่เขียนบทช่วยสอนนี้เราได้ดาวน์โหลด commons-io-2.6-bin.zip และคัดลอก ลงในโฟลเดอร์ C: \> Apache
| ระบบปฏิบัติการ | ชื่อที่เก็บถาวร | 
|---|---|
| Windows | commons-io-2.6-bin.zip | 
| ลินุกซ์ | commons-io-2.6-bin.tar.gz | 
| Mac | commons-io-2.6-bin.tar.gz | 
Apache Common IO Environment
ตั้งค่า APACHE_HOMEตัวแปรสภาพแวดล้อมเพื่อชี้ไปยังตำแหน่งไดเร็กทอรีฐานโดยที่ Apache jar จะถูกเก็บไว้ในเครื่องของคุณ สมมติว่าเราได้แยก commons-io-2.6-bin.zip ในโฟลเดอร์ Apache บนระบบปฏิบัติการต่างๆดังนี้
| ระบบปฏิบัติการ | เอาต์พุต | 
|---|---|
| Windows | ตั้งค่าตัวแปรสภาพแวดล้อม APACHE_HOME เป็น C: \ Apache | 
| ลินุกซ์ | ส่งออก APACHE_HOME = / usr / local / Apache | 
| Mac | ส่งออก APACHE_HOME = / Library / Apache | 
ตั้งค่าตัวแปร CLASSPATH
ตั้งค่า CLASSPATHตัวแปรสภาพแวดล้อมเพื่อชี้ไปยังตำแหน่งโถ IO ทั่วไป สมมติว่าคุณเก็บ commons-io-2.6-bin.zip ไว้ในโฟลเดอร์ Apache บนระบบปฏิบัติการต่างๆดังต่อไปนี้
| ระบบปฏิบัติการ | เอาต์พุต | 
|---|---|
| Windows | ตั้งค่าตัวแปรสภาพแวดล้อม CLASSPATH เป็น% CLASSPATH%;% APACHE_HOME% \ commons-io-2.6.jar; .; | 
| ลินุกซ์ | ส่งออก CLASSPATH = $ CLASSPATH: $ APACHE_HOME / commons-io-2.6.jar:. | 
| Mac | ส่งออก CLASSPATH = $ CLASSPATH: $ APACHE_HOME / commons-io-2.6.jar:. | 
IOUtils มีวิธีการยูทิลิตี้สำหรับการอ่านเขียนและคัดลอกไฟล์ วิธีการทำงานกับ InputStream, OutputStream, Reader และ Writer
การประกาศคลาส
ต่อไปนี้เป็นคำประกาศสำหรับ org.apache.commons.io.IOUtils คลาส -
public class IOUtils
   extends Object 
    คุณสมบัติของ IOUtils
คุณสมบัติของ IOUtils มีดังต่อไปนี้ -
จัดเตรียมวิธียูทิลิตี้แบบคงที่สำหรับการดำเนินการอินพุต / เอาต์พุต
toXXX () - อ่านข้อมูลจากสตรีม
write () - เขียนข้อมูลไปยังสตรีม
copy () - คัดลอกข้อมูลทั้งหมดไปยังสตรีมไปยังสตรีมอื่น
contentEquals - เปรียบเทียบเนื้อหาของสองสตรีม
ตัวอย่าง IOUtils Class
นี่คือไฟล์อินพุตที่เราต้องแยกวิเคราะห์ -
Welcome to TutorialsPoint. Simply Easy Learning. 
    IOTester.java
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.commons.io.IOUtils;
public class IOTester {
   public static void main(String[] args) {
      try {
         //Using BufferedReader
         readUsingTraditionalWay();
         //Using IOUtils
         readUsingIOUtils();
      } catch(IOException e) {
         System.out.println(e.getMessage());
      }
   }
   //reading a file using buffered reader line by line
   public static void readUsingTraditionalWay() throws IOException {
      try(BufferedReader bufferReader = new BufferedReader( new InputStreamReader(
      new FileInputStream("input.txt") ) )) {
         String line;
         while( ( line = bufferReader.readLine() ) != null ) {
            System.out.println( line );
         }
      }
   }
   //reading a file using IOUtils in one go
   public static void readUsingIOUtils() throws  IOException {
      try(InputStream in = new FileInputStream("input.txt")) {
         System.out.println( IOUtils.toString( in , "UTF-8") );
      }
   }
} 
    เอาต์พุต
มันจะพิมพ์ผลลัพธ์ต่อไปนี้ -
Welcome to TutorialsPoint. Simply Easy Learning.
Welcome to TutorialsPoint. Simply Easy Learning. 
    FileUtils มีวิธีการจัดการไฟล์เช่นการย้ายการเปิดการตรวจสอบการมีอยู่การอ่านไฟล์ ฯลฯ วิธีการเหล่านี้ใช้ File Object
การประกาศคลาส
ต่อไปนี้เป็นการประกาศสำหรับ org.apache.commons.io.FileUtils Class -
public class FileUtils
   extends Object 
    คุณสมบัติของ FileUtils
คุณสมบัติของ FileUtils มีดังต่อไปนี้ -
วิธีการเขียนลงไฟล์
วิธีการอ่านจากไฟล์
วิธีการสร้างไดเร็กทอรีรวมถึงไดเร็กทอรีหลัก
วิธีการคัดลอกไฟล์และไดเร็กทอรี
วิธีการลบไฟล์และไดเร็กทอรี
วิธีการแปลงเป็นและจาก URL
วิธีการแสดงรายการไฟล์และไดเร็กทอรีตามตัวกรองและส่วนขยาย
วิธีเปรียบเทียบเนื้อหาไฟล์
วิธีการยื่นวันที่เปลี่ยนแปลงล่าสุด
วิธีการคำนวณการตรวจสอบ
ตัวอย่างคลาส FileUtils
นี่คือไฟล์อินพุตที่เราต้องแยกวิเคราะห์ -
Welcome to TutorialsPoint. Simply Easy Learning. 
    IOTester.java
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import org.apache.commons.io.FileUtils;
public class IOTester {
   public static void main(String[] args) {
      try {
         //Using FileUtils
         usingFileUtils();
      } catch(IOException e) {
         System.out.println(e.getMessage());
      }
   }
   public static void usingFileUtils() throws IOException {
      //get the file object
      File file = FileUtils.getFile("input.txt");
      //get the temp directory
      File tmpDir = FileUtils.getTempDirectory();
      System.out.println(tmpDir.getName());
      //copy file to temp directory
      FileUtils.copyFileToDirectory(file, tmpDir);
      //create a new file
      File newTempFile = FileUtils.getFile(tmpDir, file.getName());
      //get the content
      String data = FileUtils.readFileToString(newTempFile, Charset.defaultCharset());
      //print the content
      System.out.println(data);
   }
} 
    เอาต์พุต
ผลลัพธ์จะกล่าวถึงด้านล่าง -
Temp
Welcome to TutorialsPoint. Simply Easy Learning. 
    FilenameUtils จัดเตรียมเมธอดในการทำงานกับชื่อไฟล์โดยไม่ต้องใช้ File Object ทำงานบนระบบปฏิบัติการที่แตกต่างกันในลักษณะเดียวกัน คลาสนี้แก้ไขปัญหาเมื่อย้ายจากเครื่องพัฒนาที่ใช้ Windows ไปยังเครื่องผลิตที่ใช้ Unix
การประกาศคลาส
ต่อไปนี้เป็นคำประกาศสำหรับ org.apache.commons.io.FilenameUtils คลาส -
public class FilenameUtils
   extends Object 
    คุณสมบัติของ FileNameUtils
คลาสนี้กำหนดส่วนประกอบหกส่วนภายในชื่อไฟล์ พิจารณาตำแหน่งตัวอย่างเช่นC:\dev\project\file.txt. จากนั้นส่วนประกอบมีดังนี้:
คำนำหน้า - C: \
เส้นทางสัมพัทธ์ - dev \ project \
เส้นทางสัมบูรณ์ - C: \ dev \ project \
ชื่อ - file.txt
ชื่อฐาน - ไฟล์
ส่วนขยาย - txt
ในการระบุไดเร็กทอรีให้เพิ่มตัวคั่นให้กับชื่อไฟล์
ตัวอย่างคลาส FilenameUtils
ตัวอย่างของ FilenameUtils Class มีให้ด้านล่าง -
IOTester.java
import java.io.IOException;
import org.apache.commons.io.FilenameUtils;
public class IOTester {
   public static void main(String[] args) {
      try {
         //Using FilenameUtils
         usingFilenameUtils();
      } catch(IOException e) {
         System.out.println(e.getMessage());
      }
   }
   public static void usingFilenameUtils() throws IOException {
      String path = "C:\\dev\\project\\file.txt";
      System.out.println("Full Path: " +FilenameUtils.getFullPath(path));
      System.out.println("Relative Path: " +FilenameUtils.getPath(path));
      System.out.println("Prefix: " +FilenameUtils.getPrefix(path));
      System.out.println("Extension: " + FilenameUtils.getExtension(path));
      System.out.println("Base: " + FilenameUtils.getBaseName(path));
      System.out.println("Name: " + FilenameUtils.getName(path));
      String filename = "C:/commons/io/../lang/project.xml";
      System.out.println("Normalized Path: " + FilenameUtils.normalize(filename));
   }
} 
    เอาต์พุต
ผลลัพธ์จะได้รับด้านล่าง -
Full Path: C:\dev\project\
Relative Path: dev\project\
Prefix: C:\
Extension: txt
Base: file
Name: file.txt
Normalized Path: C:\commons\lang\project.xml 
    FileSystemUtils จัดเตรียมเมธอดในการรับพื้นที่ว่างบนดิสก์ไดรฟ์
การประกาศคลาส
ต่อไปนี้เป็นคำประกาศสำหรับ org.apache.commons.io.FileSystemUtils คลาส -
public class FileSystemUtils
   extends Object 
    ตัวอย่างของคลาส FileSystemUtils
ด้านล่างเป็นตัวอย่างของ FileSystemUtils Class -
IOTester.java
import java.io.IOException;
import org.apache.commons.io.FileSystemUtils;
public class IOTester {
   public static void main(String[] args) {
      try {
         System.out.println("Free Space " + FileSystemUtils.freeSpaceKb("C:/") + " Bytes");
      } catch(IOException e) {
         System.out.println(e.getMessage());
      }
   }
} 
    เอาต์พุต
ด้านล่างคือผลลัพธ์ -
Free Space 61355640 kb 
    การแจกแจงความไวของตัวพิมพ์ IO ระบบปฏิบัติการที่แตกต่างกันมีกฎที่แตกต่างกันสำหรับกรณีไวสำหรับชื่อไฟล์ ตัวอย่างเช่น Windows ไม่คำนึงถึงตัวพิมพ์เล็กและใหญ่สำหรับการตั้งชื่อไฟล์ในขณะที่ Unix นั้นคำนึงถึงตัวพิมพ์เล็กและใหญ่ IOCase จับความแตกต่างดังกล่าวจัดเตรียมการแจงนับเพื่อควบคุมวิธีการเปรียบเทียบชื่อไฟล์ที่ควรดำเนินการ นอกจากนี้ยังมีวิธีการใช้การแจงนับเพื่อทำการเปรียบเทียบ
ประกาศ Enum
ต่อไปนี้คือคำประกาศสำหรับ org.apache.commons.io.IOCase Enum -
public enum IOCase
   extends Enum<IOCase> implements Serializable 
    ตัวอย่าง IOCase Enum
ตัวอย่างของ IOCase Enum แสดงไว้ด้านล่าง -
IOTester.java
import java.io.IOException;
import org.apache.commons.io.IOCase;
public class IOTester {
   public static void main(String[] args) {
      try {
         usingIOCase();
      } catch(IOException e) {
         System.out.println(e.getMessage());
      }
   }
   public static void usingIOCase() throws IOException {
      String text = "Welcome to TutorialsPoint. Simply Easy Learning.";
      String text1 = "WELCOME TO TUTORIALSPOINT. SIMPLY EASY LEARNING.";
      System.out.println("Ends with Learning (case sensitive): " + IOCase.SENSITIVE.checkEndsWith(text1, "Learning."));
      System.out.println("Ends with Learning (case insensitive): " + IOCase.INSENSITIVE.checkEndsWith(text1, "Learning."));
      System.out.println("Equality Check (case sensitive): " + IOCase.SENSITIVE.checkEquals(text, text1));
      System.out.println("Equality Check (case insensitive): " + IOCase.INSENSITIVE.checkEquals(text, text1));
   }
} 
    เอาต์พุต
มันจะพิมพ์ผลลัพธ์ต่อไปนี้ -
Ends with Learning (case sensitive): false
Ends with Learning (case insensitive): true
Equality Check (case sensitive): false
Equality Check (case insensitive): true 
    LineIterator มีวิธีที่ยืดหยุ่นในการทำงานกับไฟล์แบบบรรทัด ให้เราเรียนรู้เรื่องเดียวกันในบทนี้
การประกาศคลาส
ต่อไปนี้เป็นคำประกาศสำหรับ org.apache.commons.io.LineIterator คลาส -
public class LineIterator
   extends Object implements Iterator<String>, Closeable 
    ตัวอย่าง LineIterator Class
นี่คือไฟล์อินพุตที่เราต้องแยกวิเคราะห์ -
Welcome to TutorialsPoint. Simply Easy Learning.
Learn web technologies,
prepare exams,
code online,
all at one place. 
    IOTester.java
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
public class IOTester {
   public static void main(String[] args) {
      try {
         usingLineIterator();
      } catch(IOException e) {
         System.out.println(e.getMessage());
      }
   }
   public static void usingLineIterator() throws IOException {
      //get the file object
      File file = FileUtils.getFile("input.txt");
      try(LineIterator lineIterator = FileUtils.lineIterator(file)) {
         System.out.println("Contents of input.txt");
         while(lineIterator.hasNext()) {
            System.out.println(lineIterator.next());
         }
      }
   }
} 
    เอาต์พุต
มันจะพิมพ์ผลลัพธ์ต่อไปนี้ -
Contents of input.txt
Welcome to TutorialsPoint. Simply Easy Learning.
Learn web technologies,
prepare exams,
code online,
all at one place. 
    NameFileFilter ใน Commons IO จะกรองชื่อไฟล์สำหรับชื่อ
การประกาศคลาส
ต่อไปนี้เป็นคำประกาศสำหรับ org.apache.commons.io.filefilter.NameFileFilter ชั้น:
public class NameFileFilter
   extends AbstractFileFilter implements Serializable 
    ตัวอย่างคลาส NameFileFilter
นี่คือไฟล์อินพุตที่เราต้องแยกวิเคราะห์ -
Welcome to TutorialsPoint. Simply Easy Learning. 
    มาพิมพ์ไฟล์และไดเร็กทอรีทั้งหมดในไดเร็กทอรีปัจจุบันจากนั้นกรองไฟล์ที่มีชื่อ Input.txt
IOTester.java
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.IOCase;
import org.apache.commons.io.filefilter.NameFileFilter;
public class IOTester {
   public static void main(String[] args) {
      try {
         usingNameFileFilter();
      } catch(IOException e) {
         System.out.println(e.getMessage());
      }
   }
   public static void usingNameFileFilter() throws IOException {
      //get the current directory
      File currentDirectory = new File(".");
      //get names of all files and directory in current directory
      String[] files = currentDirectory.list();
      System.out.println("All files and Folders.\n");
      for( int i = 0; i < files.length; i++ ) {
         System.out.println(files[i]);
      }
      System.out.println("\nFile with name input.txt\n");
      String[] acceptedNames = {"input", "input.txt"};
      String[] filesNames = currentDirectory.list( new NameFileFilter(acceptedNames, IOCase.INSENSITIVE) );
      for( int i = 0; i < filesNames.length; i++ ) {
         System.out.println(filesNames[i]);
      }
   }
} 
    เอาต์พุต
มันจะพิมพ์ผลลัพธ์ต่อไปนี้ -
All files and Folders.
.classpath
.project
.settings
bin
input.txt
src
File with name input.txt
input.txt 
    WildcardFileFilter ใน Commons IO จะกรองไฟล์โดยใช้สัญลักษณ์แทนที่ให้มา
การประกาศคลาส
ต่อไปนี้เป็นคำประกาศสำหรับ org.apache.commons.io.filefilter.WildcardFileFilter คลาส -
public class WildcardFileFilter
   extends AbstractFileFilter implements Serializable 
    ตัวอย่างคลาส WildcardFileFilter
นี่คือไฟล์อินพุตที่เราต้องแยกวิเคราะห์
Welcome to TutorialsPoint. Simply Easy Learning. 
    มาพิมพ์ไฟล์และไดเร็กทอรีทั้งหมดในไดเร็กทอรีปัจจุบันจากนั้นกรองไฟล์ที่มีชื่อลงท้ายด้วย t.
IOTester.java
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.filefilter.WildcardFileFilter;
public class IOTester {
   public static void main(String[] args) {
      try {
         usingWildcardFileFilter();
      } catch(IOException e) {
         System.out.println(e.getMessage());
      }
   }
   public static void usingWildcardFileFilter() throws IOException {
      //get the current directory
      File currentDirectory = new File(".");
      //get names of all files and directory in current directory
      String[] files = currentDirectory.list();
      System.out.println("All files and Folders.\n");
      for( int i = 0; i < files.length; i++ ) {
        System.out.println(files[i]);
      }
      System.out.println("\nFile name ending with t.\n");
      String[] filesNames = currentDirectory.list( new  WildcardFileFilter("*t"));
      for( int i = 0; i < filesNames.length; i++ ) {
        System.out.println(filesNames[i]);
      }
   }
} 
    เอาต์พุต
มันจะพิมพ์ผลลัพธ์ต่อไปนี้
All files and Folders.
.classpath
.project
.settings
bin
input.txt
src
File name ending with t
.project
input.txt 
    SuffixFileFilter กรองไฟล์ที่ขึ้นอยู่กับคำต่อท้าย ใช้ในการดึงไฟล์ทั้งหมดในประเภทใดประเภทหนึ่ง
การประกาศคลาส
ต่อไปนี้เป็นคำประกาศสำหรับ org.apache.commons.io.filefilter.SuffixFileFilter คลาส -
public class SuffixFileFilter
   extends AbstractFileFilter implements Serializable 
    ตัวอย่างคลาส SuffixFileFilter
นี่คือไฟล์อินพุตที่เราต้องแยกวิเคราะห์ -
Welcome to TutorialsPoint. Simply Easy Learning. 
    มาพิมพ์ไฟล์และไดเร็กทอรีทั้งหมดในไดเร็กทอรีปัจจุบันจากนั้นกรองไฟล์ที่มีนามสกุล txt
IOTester.java
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.filefilter.SuffixFileFilter;
public class IOTester {
   public static void main(String[] args) {
      try {
         usingSuffixFileFilter();
      } catch(IOException e) {
         System.out.println(e.getMessage());
      }
   }
   public static void usingSuffixFileFilter() throws IOException {
      //get the current directory
      File currentDirectory = new File(".");
      //get names of all files and directory in current directory
      String[] files = currentDirectory.list();
      System.out.println("All files and Folders.\n");
      for( int i = 0; i < files.length; i++ ) {
         System.out.println(files[i]);
      }
      System.out.println("\nFile with extenstion txt\n");
      String[] filesNames = currentDirectory.list( new SuffixFileFilter("txt"));
      for( int i = 0; i < filesNames.length; i++ ) {
         System.out.println(filesNames[i]);
      }
   }
} 
    เอาต์พุต
มันจะพิมพ์ผลลัพธ์ต่อไปนี้
All files and Folders.
.classpath
.project
.settings
bin
input.txt
src
File with extenstion txt
input.txt 
    PrefixFileFilter กรองไฟล์ที่ขึ้นอยู่กับคำนำหน้า
การประกาศคลาส
ต่อไปนี้เป็นคำประกาศสำหรับ org.apache.commons.io.filefilter.PrefixFileFilter คลาส -
public class PrefixFileFilter
   extends AbstractFileFilter implements Serializable 
    ตัวอย่างคลาส PrefixFileFilter
นี่คือไฟล์อินพุตที่เราต้องแยกวิเคราะห์ -
Welcome to TutorialsPoint. Simply Easy Learning. 
    มาพิมพ์ไฟล์และไดเร็กทอรีทั้งหมดในไดเร็กทอรีปัจจุบันจากนั้นกรองไฟล์ที่มีชื่อขึ้นต้นด้วยอินพุต
IOTester.java
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.filefilter.PrefixFileFilter;
public class IOTester {
   public static void main(String[] args) {
      try {
         usingPrefixFileFilter();
      } catch(IOException e) {
         System.out.println(e.getMessage());
      }
   }
   public static void usingPrefixFileFilter() throws IOException {
      //get the current directory
      File currentDirectory = new File(".");
      //get names of all files and directory in current directory
      String[] files = currentDirectory.list();
      System.out.println("All files and Folders.\n");
      for( int i = 0; i < files.length; i++ ) {
         System.out.println(files[i]);
      }
      System.out.println("\nFile starting with input\n");
      String[] filesNames = currentDirectory.list( new PrefixFileFilter("input") );
      for( int i = 0; i < filesNames.length; i++ ) {
         System.out.println(filesNames[i]);
      }
   }
} 
    เอาต์พุต
มันจะพิมพ์ผลลัพธ์ต่อไปนี้ -
All files and Folders.
.classpath
.project
.settings
bin
input.txt
src
File with extenstion txt
input.txt 
    OrFileFilter จัดเตรียมตรรกะตามเงื่อนไขหรือในรายการตัวกรองไฟล์ จะคืนค่าเป็นจริงหากตัวกรองใด ๆ ในรายการส่งคืนจริง มิฉะนั้นจะส่งกลับเท็จ
การประกาศคลาส
ต่อไปนี้เป็นคำประกาศสำหรับ org.apache.commons.io.filefilter.OrFileFilter คลาส -
public class OrFileFilter
   extends AbstractFileFilter implements ConditionalFileFilter, Serializable 
    ตัวอย่างคลาส OrFileFilter
นี่คือไฟล์อินพุตที่เราต้องแยกวิเคราะห์ -
Welcome to TutorialsPoint. Simply Easy Learning. 
    มาพิมพ์ไฟล์และไดเร็กทอรีทั้งหมดในไดเร็กทอรีปัจจุบันจากนั้นกรองไฟล์ที่มีชื่อขึ้นต้นด้วย หรือลงท้ายด้วย t.
IOTester.java
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.filefilter.OrFileFilter;
import org.apache.commons.io.filefilter.PrefixFileFilter;
import org.apache.commons.io.filefilter.WildcardFileFilter;
public class IOTester {
   public static void main(String[] args) {
      try {
         usingOrFileFilter();
      } catch(IOException e) {
         System.out.println(e.getMessage());
      }
   }
   public static void usingOrFileFilter() throws IOException {
      //get the current directory
      File currentDirectory = new File(".");
      //get names of all files and directory in current directory
      String[] files = currentDirectory.list();
      System.out.println("All files and Folders.\n");
      for( int i = 0; i < files.length; i++ ) {
         System.out.println(files[i]);
      }
      System.out.println("\nFile starting with . or ends with t\n");
      String[] filesNames = currentDirectory.list( new OrFileFilter(new PrefixFileFilter("."), new WildcardFileFilter("*t")));
      for( int i = 0; i < filesNames.length; i++ ) {
         System.out.println(filesNames[i]);
      }
   }
} 
    เอาต์พุต
มันจะพิมพ์ผลลัพธ์ต่อไปนี้
All files and Folders.
.classpath
.project
.settings
bin
input.txt
src
File starting with . or ends with t
.classpath
.project
.settings
input.txt 
    AndFileFilter จัดเตรียมเงื่อนไขและตรรกะในรายการตัวกรองไฟล์ จะคืนค่าเป็นจริงหากตัวกรองทั้งหมดในรายการส่งคืนจริง มิฉะนั้นจะส่งกลับเท็จ
การประกาศคลาส
ต่อไปนี้เป็นคำประกาศสำหรับ org.apache.commons.io.filefilter.AndFileFilter คลาส -
public class AndFileFilter
   extends AbstractFileFilter implements ConditionalFileFilter, Serializable 
    ตัวอย่างคลาส AndFileFilter
นี่คือไฟล์อินพุตที่เราต้องแยกวิเคราะห์ -
Welcome to TutorialsPoint. Simply Easy Learning. 
    มาพิมพ์ไฟล์และไดเร็กทอรีทั้งหมดในไดเร็กทอรีปัจจุบันจากนั้นกรองไฟล์ที่มีชื่อขึ้นต้นด้วย และลงท้ายด้วย t.
IOTester.java
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.filefilter.AndFileFilter;
import org.apache.commons.io.filefilter.PrefixFileFilter;
import org.apache.commons.io.filefilter.WildcardFileFilter;
public class IOTester {
   public static void main(String[] args) {
      try {
         usingAndFileFilter();
      } catch(IOException e) {
         System.out.println(e.getMessage());
      }
   }
   public static void usingAndFileFilter() throws IOException {
      //get the current directory
      File currentDirectory = new File(".");
      //get names of all files and directory in current directory
      String[] files = currentDirectory.list();
      System.out.println("All files and Folders.\n");
      for( int i = 0; i < files.length; i++ ) {
         System.out.println(files[i]);
      }
      System.out.println("\nFile starting with . and ends with t\n");
      String[] filesNames = currentDirectory.list(new AndFileFilter(
         new PrefixFileFilter("."), new WildcardFileFilter("*t")));
      for( int i = 0; i < filesNames.length; i++ ) {
        System.out.println(filesNames[i]);
      }
   }
} 
    เอาต์พุต
มันจะพิมพ์ผลลัพธ์ต่อไปนี้
All files and Folders.
.classpath
.project
.settings
bin
input.txt
src
File starting with . or ends with t
.project 
    FileEntry จัดเตรียมสถานะของไฟล์หรือไดเร็กทอรี แอตทริบิวต์ของไฟล์ในช่วงเวลาหนึ่ง
การประกาศคลาส
ต่อไปนี้เป็นคำประกาศสำหรับ org.apache.commons.io.monitor.FileEntry คลาส -
public class FileEntry
   extends Object implements Serializable 
    คุณสมบัติของ FileEntry
อ็อบเจ็กต์คลาส FileEntry จัดเตรียมแอ็ตทริบิวต์ไฟล์ต่อไปนี้ในช่วงเวลาหนึ่ง
getName () - ชื่อไฟล์
มีอยู่ () - ตรวจสอบว่ามีไฟล์อยู่หรือไม่
isDirectory () - ตรวจสอบว่าไฟล์เป็นไดเร็กทอรีหรือไม่
lastModified () - ให้วันที่เวลาที่แก้ไขล่าสุด
listFiles () - ให้เนื้อหาของไดเร็กทอรี
ตัวอย่างคลาส FileEntry
นี่คือไฟล์อินพุตที่เราต้องแยกวิเคราะห์ -
Welcome to TutorialsPoint. Simply Easy Learning. 
    IOTester.java
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.monitor.FileEntry;
public class IOTester {
   public static void main(String[] args) {
      try {
         usingFileEntry();
      } catch(IOException e) {
         System.out.println(e.getMessage());
      }
   }
   public static void usingFileEntry() throws IOException {
      //get the file object
      File file = FileUtils.getFile("input.txt");
      FileEntry fileEntry = new FileEntry(file);
      System.out.println("Monitored File: " + fileEntry.getFile());
      System.out.println("File name: " + fileEntry.getName());
      System.out.println("Is Directory: " + 	fileEntry.isDirectory());
   }
} 
    เอาต์พุต
มันจะพิมพ์ผลลัพธ์ต่อไปนี้
Monitored File: input.txt
File name: input.txt
Is Directory: false 
    FileAlterationObserver แสดงถึงสถานะของไฟล์ที่อยู่ด้านล่างไดเร็กทอรีรูทตรวจสอบระบบไฟล์และแจ้งให้ผู้ฟังทราบถึงการสร้างเปลี่ยนแปลงหรือลบเหตุการณ์
การประกาศคลาส
ต่อไปนี้เป็นคำประกาศสำหรับ
org.apache.commons.io.monitor.FileAlterationObserver คลาส -
public class FileAlterationObserver
   extends Object implements Serializable 
    ตัวอย่างคลาส FileAlterationObserver
นี่คือไฟล์อินพุตที่เราต้องแยกวิเคราะห์ -
Welcome to TutorialsPoint. Simply Easy Learning. 
    IOTester.java
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileDeleteStrategy;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
public class IOTester {
   public static void main(String[] args) {
      try {
         usingFileAlterationObserver();
      } catch(IOException e) {
         System.out.println(e.getMessage());
      }
   }
   public static void usingFileAlterationObserver() throws IOException {
      //get the file object
      File inputFile = FileUtils.getFile("input.txt");
      String absolutePath = inputFile.getAbsolutePath();
      String parent = absolutePath.substring(0,absolutePath.indexOf("input.txt"));
      File parentDirectory = FileUtils.getFile(parent);
      FileAlterationObserver observer = new
      FileAlterationObserver(parentDirectory);
      observer.addListener(new FileAlterationListenerAdaptor() {
         @Override
         public void onDirectoryCreate(File file) {
            System.out.println("Folder created: " + file.getName());
         }
         @Override
            public void onDirectoryDelete(File file) {
               System.out.println("Folder deleted: " + file.getName());
            }
         @Override
            public void onFileCreate(File file) {
               System.out.println("File created: " + file.getName());
            }
         @Override
            public void onFileDelete(File file) {
               Syst em.out.println("File deleted: " + file.getName());
            }
        });
       //create a monitor to check changes after every 500 ms
       FileAlterationMonitor monitor = new FileAlterationMonitor(500, observer);
       try {
          monitor.start();
         //create a new directory
         File newFolder = new File("test");
         File newFile = new File("test1");
         newFolder.mkdirs();
         Thread.sleep(1000);
         newFile.createNewFile();
         Thread.sleep(1000);
         FileDeleteStrategy.NORMAL.delete(newFolder);
         Thread.sleep(1000);
         FileDeleteStrategy.NORMAL.delete(newFile);
         Thread.sleep(1000);
         monitor.stop(10000);
      } catch(IOException e) {
         System.out.println(e.getMessage());
      } catch(InterruptedException e) {
         System.out.println(e.getMessage());
      } catch (Exception e) {
         System.out.println(e.getMessage());
      }
   }
} 
    เอาต์พุต
มันจะพิมพ์ผลลัพธ์ต่อไปนี้
Folder created: test
File created: test1
Folder deleted: test
File deleted: test1 
    FileAlterationMonitor แสดงถึงเธรดที่สร้างเธรดการมอนิเตอร์ที่ทริกเกอร์ FileAlterationObserver ที่ลงทะเบียนในช่วงเวลาที่กำหนด
การประกาศคลาส
ต่อไปนี้เป็นคำประกาศสำหรับ org.apache.commons.io.monitor.FileAlterationMonitor Class -
public final class FileAlterationMonitor
   extends Object implements Runnable 
    ตัวอย่างคลาส FileAlterationMonitor
นี่คือไฟล์อินพุตที่เราต้องแยกวิเคราะห์ -
Welcome to TutorialsPoint. Simply Easy Learning. 
    IOTester.java
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileDeleteStrategy;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
public class IOTester {
   public static void main(String[] args) {
      try {
         usingFileAlterationMonitor();
      } catch(IOException e) {
         System.out.println(e.getMessage());
      }
   }
   public static void usingFileAlterationMonitor() throws IOException {
      //get the file object
      File inputFile = FileUtils.getFile("input.txt");
      String absolutePath = inputFile.getAbsolutePath();
      String parent = absolutePath.substring(0,absolutePath.indexOf("input.txt"));
      File parentDirectory = FileUtils.getFile(parent);
      FileAlterationObserver observer = new
      FileAlterationObserver(parentDirectory);
      observer.addListener(new FileAlterationListenerAdaptor(){
         @Override
         public void onDirectoryCreate(File file) {
            System.out.println("Folder created: " + file.getName());
         }
         @Override
         public void onDirectoryDelete(File file) {
            System.out.println("Folder deleted: " + file.getName());
         }
         @Override
         public void onFileCreate(File file) {
            System.out.println("File created: " + file.getName());
         }
         @Override
         public void onFileDelete(File file) {
            System.out.println("File deleted: " + file.getName());
         }
      });
      //create a monitor to check changes after every 500 ms
      FileAlterationMonitor monitor = new FileAlterationMonitor(500, observer);
      try {
         monitor.start();
         //create a new directory
         File newFolder = new File("test");
         File newFile = new File("test1");
         newFolder.mkdirs();
         Thread.sleep(1000);
         newFile.createNewFile();
         Thread.sleep(1000);
         FileDeleteStrategy.NORMAL.delete(newFolder);
         Thread.sleep(1000);
         FileDeleteStrategy.NORMAL.delete(newFile);
         Thread.sleep(1000);
         monitor.stop(10000);
      } catch(IOException e) {
         System.out.println(e.getMessage());
      } catch(InterruptedException e) {
         System.out.println(e.getMessage());
      } catch (Exception e) {
         System.out.println(e.getMessage());
      }
   }
} 
    เอาต์พุต
มันจะพิมพ์ผลลัพธ์ต่อไปนี้
Folder created: test
File created: test1
Folder deleted: test
File deleted: test1 
    NameFileComparator เปรียบเทียบชื่อของสองไฟล์ สามารถใช้เพื่อจัดเรียงรายการหรืออาร์เรย์ของไฟล์โดยใช้ชื่อไฟล์ทั้งแบบคำนึงถึงขนาดตัวพิมพ์ไม่คำนึงถึงตัวพิมพ์เล็กหรือใหญ่หรือระบบ
การประกาศคลาส
ต่อไปนี้เป็นคำประกาศสำหรับ
org.apache.commons.io.comparator.NameFileComparator คลาส -
public class NameFileComparator
   extends Object implements Serializable 
    ตัวอย่างคลาส NameFileComparator
นี่คือไฟล์อินพุตที่เราต้องแยกวิเคราะห์ -
Welcome to TutorialsPoint. Simply Easy Learning. 
    IOTester.java
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import org.apache.commons.io.IOCase;
import org.apache.commons.io.comparator.NameFileComparator;
import org.apache.commons.io.filefilter.FileFileFilter;
public class IOTester {
   public static void main(String[] args) {
      try {
         usingNameFileComparator();
      } catch(IOException e) {
         System.out.println(e.getMessage());
      }
   }
   public static void usingNameFileComparator() throws IOException {
      //get the current directory
      File currentDirectory = new File(".");
      NameFileComparator comparator = new
      NameFileComparator(IOCase.INSENSITIVE);
      File[] sortedFiles = comparator.sort(currentDirectory.listFiles((FileFilter)FileFileFilter.FILE));
      System.out.println("Sorted By Name: ");
      for(File file:sortedFiles) {
         System.out.println(file.getName());
      }
   }
} 
    เอาต์พุต
มันจะพิมพ์ผลลัพธ์ต่อไปนี้
Sorted By Name:
.classpath
.project
input.txt 
    SizeFileComparator เปรียบเทียบขนาดของสองไฟล์ / ไดเร็กทอรี สามารถใช้เพื่อจัดเรียงรายการหรืออาร์เรย์ของไฟล์โดยใช้ขนาดหรือไดเร็กทอรีตามจำนวนลูก
การประกาศคลาส
ต่อไปนี้เป็นคำประกาศสำหรับ
org.apache.commons.io.comparator.SizeFileComparator คลาส -
public class SizeFileComparator
   extends Object implements Serializable 
    ตัวอย่างคลาส SizeFileComparator
นี่คือไฟล์อินพุตที่เราต้องแยกวิเคราะห์ -
Welcome to TutorialsPoint. Simply Easy Learning. 
    IOTester.java
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import org.apache.commons.io.comparator.SizeFileComparator;
import org.apache.commons.io.filefilter.FileFileFilter;
public class IOTester {
   public static void main(String[] args) {
      try {
         usingSizeFileComparator();
      } catch(IOException e) {
         System.out.println(e.getMessage());
      }
   } 
   public static void usingSizeFileComparator() throws IOException {
      //get the current directory
      File currentDirectory = new File(".");
      SizeFileComparator comparator = new SizeFileComparator();
      File[] sortedFiles = comparator.sort(currentDirectory.listFiles((FileFilter)FileFileFilter.FILE));
      System.out.println("Sorted By Size: ");
      for(File file:sortedFiles) {
         System.out.println(file.getName() + ", size(kb) :" + file.length());
      }
   }
} 
    เอาต์พุต
มันจะพิมพ์ผลลัพธ์ต่อไปนี้
Sorted By Size:
input.txt, size:124
.project, size:382
.classpath, size:441 
    LastModifiedFileComparator เปรียบเทียบวันที่แก้ไขล่าสุดของสองไฟล์ / ไดเร็กทอรี สามารถใช้เพื่อจัดเรียงรายการหรืออาร์เรย์ของไฟล์ / ไดเรกทอรีโดยใช้วันที่แก้ไขล่าสุด
การประกาศคลาส
ต่อไปนี้เป็นคำประกาศสำหรับ org.apache.commons.io.comparator.LastModifiedFileComparator คลาส -
public class LastModifiedFileComparator
   extends Object implements Serializable 
    ตัวอย่าง LastModifiedFileComparator Class
นี่คือไฟล์อินพุตที่เราต้องแยกวิเคราะห์ -
Welcome to TutorialsPoint. Simply Easy Learning. 
    IOTester.java
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Date;
import org.apache.commons.io.comparator.LastModifiedFileComparator;
import org.apache.commons.io.filefilter.FileFileFilter;
public class IOTester {
   public static void main(String[] args) {
      try {
         usingLastModifiedFileComparator();
      } catch(IOException e) {
         System.out.println(e.getMessage());
      }
   }
   public static void usingLastModifiedFileComparator() throws IOException {
      //get the current directory
      File currentDirectory = new File(".");
      LastModifiedFileComparator comparator = new LastModifiedFileComparator();
      File[] sortedFiles = comparator.sort(currentDirectory.listFiles((FileFilter)FileFileFilter.FILE));
      System.out.println("Sorted By Last Modified date: ");
      for(File file:sortedFiles) {
         System.out.println(file.getName() + ", Modified on: " + new Date(file.lastModified()));
      }
   }
} 
    เอาต์พุต
มันจะพิมพ์ผลลัพธ์ต่อไปนี้
Sorted By Last Modified date:
.project, Modified on: Thu Oct 12 19:06:45 IST 2017
.classpath, Modified on: Mon Nov 20 13:09:55 IST 2017
input.txt, Modified on: Mon Nov 20 19:27:55 IST 2017 
    เป็นพร็อกซี InputStream ที่เขียนสำเนาของไบต์ทั้งหมดอย่างโปร่งใสซึ่งอ่านจากสตรีมพร็อกซีไปยัง OutputStream ที่กำหนด สตรีมอินพุตของพร็อกซีถูกปิดเมื่อมีการเรียกเมธอด close () บนพร็อกซีนี้ สามารถใช้เพื่อดำเนินการสองสตรีมพร้อมกันได้
การประกาศคลาส
ต่อไปนี้เป็นคำประกาศสำหรับ org.apache.commons.io.input.TeeInputStream คลาส -
public class TeeInputStream
   extends ProxyInputStream 
    ตัวอย่างคลาส TeeInputStream
ในตัวอย่างนี้การปิด TeeInputStream จะปิด TeeInputStream เช่นเดียวกับวัตถุ TeeOutputStream
IOTester.java
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.apache.commons.io.input.TeeInputStream;
import org.apache.commons.io.output.TeeOutputStream;
public class IOTester {
   private static final String SAMPLE = "Welcome to TutorialsPoint. Simply Easy
   Learning.";
   public static void main(String[] args) {
      try {
         usingTeeInputStream();
      }catch(IOException e) {
         System.out.println(e.getMessage());
      }
   }
   public static void usingTeeInputStream() throws IOException {
      TeeInputStream teeInputStream = null;
      TeeOutputStream teeOutputStream = null;
      try {
         ByteArrayInputStream inputStream = new
         ByteArrayInputStream(SAMPLE.getBytes("US-ASCII"));
         ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream();
         ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream();
         teeOutputStream = new TeeOutputStream(outputStream1, outputStream2);
         teeInputStream = new TeeInputStream(inputStream, teeOutputStream, true);
         teeInputStream.read(new byte[SAMPLE.length()]);
         System.out.println("Output stream 1: " + outputStream1.toString());
         System.out.println("Output stream 2: " + outputStream2.toString());
      }catch (IOException e) {
         System.out.println(e.getMessage());
      } finally {
         //teeIn.close() closes teeIn and teeOut which in turn closes the out1 and out2.
         try {
            teeInputStream.close();
         } catch (IOException e) {
            System.out.println(e.getMessage());
         }
      }
   }
} 
    เอาต์พุต
มันจะพิมพ์ผลลัพธ์ต่อไปนี้
Output stream 1: Welcome to TutorialsPoint. Simply Easy Learning.
Output stream 2: Welcome to TutorialsPoint. Simply Easy Learning. 
    TeeOutputStream แยก OutputStream มันถูกตั้งชื่อตามคำสั่ง unix 'tee' อนุญาตให้สตรีมแยกเป็นสองสตรีม
การประกาศคลาส
ต่อไปนี้เป็นคำประกาศสำหรับ org.apache.commons.io.output.TeeOutputStream คลาส -
public class TeeOutputStream
   extends ProxyOutputStream 
    ตัวอย่างคลาส TeeOutputStream
ในตัวอย่างนี้ TeeOutputStream ยอมรับเอาต์พุตสตรีมสองรายการเป็นพารามิเตอร์และส่งข้อมูลไปยังข้อมูลชุด TeeOutputStream ไปยังเอาต์พุตสตรีมทั้งสอง
IOTester.java
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.apache.commons.io.input.TeeInputStream;
import org.apache.commons.io.output.TeeOutputStream;
public class IOTester {
   private static final String SAMPLE = "Welcome to TutorialsPoint. Simply Easy
   Learning.";
   public static void main(String[] args) {
      try {
         usingTeeInputStream();
      } catch(IOException e) {
         System.out.println(e.getMessage());
      }
   }
   public static void usingTeeInputStream() throws IOException {
      TeeInputStream teeInputStream = null;
      TeeOutputStream teeOutputStream = null;
      try {
         ByteArrayInputStream inputStream = new
         ByteArrayInputStream(SAMPLE.getBytes("US-ASCII"));
         ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream();
         ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream();
         teeOutputStream = new TeeOutputStream(outputStream1, outputStream2);
         teeInputStream = new TeeInputStream(inputStream, teeOutputStream, true);
         teeInputStream.read(new byte[SAMPLE.length()]);
         System.out.println("Output stream 1: " + outputStream1.toString());
         System.out.println("Output stream 2: " + outputStream2.toString());
      } catch (IOException e) {
         System.out.println(e.getMessage());
      } finally {
         //teeIn.close() closes teeIn and teeOut which in turn closes the out1 and out2.
         try {
            teeInputStream.close();
         } catch (IOException e) {
            System.out.println(e.getMessage());
         }
      }
   }
} 
    เอาต์พุต
มันจะพิมพ์ผลลัพธ์ต่อไปนี้
Output stream 1: Welcome to TutorialsPoint. Simply Easy Learning.
Output stream 2: Welcome to TutorialsPoint. Simply Easy Learning.