สองวิธี:
1. คัดลอกไฟล์และโฟลเดอร์ทั้งหมดภายใต้ไดเรกทอรี
2. ผสานไฟล์ข้อความทั้งหมดภายใต้ไดเรกทอรีไฟล์ลงในไฟล์เดียวกัน
การคัดลอกรหัสมีดังนี้:
แพ็คเกจ com.firewolf.test;
นำเข้า Java.io.File;
นำเข้า Java.io.FileInputStream;
นำเข้า Java.io.FileOutputStream;
นำเข้า java.io.ioException;
ชั้นเรียนสาธารณะ filereaderutil {
โมฆะคงที่สาธารณะหลัก (สตริง [] args) {
พยายาม {
// MergeFile (ไฟล์ใหม่ ("C:/เอกสารและการตั้งค่า/liuxing0/เดสก์ท็อป/โฟลเดอร์/สคริปต์ใหม่"), ไฟล์ใหม่ ("d: /all.sql"));
Copyfiles (ไฟล์ใหม่ ("G:/วัสดุการศึกษา/หมายเหตุ"), ไฟล์ใหม่ ("G:/test"));
} catch (ioexception e) {
E.PrintStackTrace ();
-
-
-
* คัดลอกไฟล์ทั้งหมดในไดเรกทอรีไฟล์
* @Param SourcePath ไดเร็กทอรีไฟล์ต้นฉบับ
* @Param Despath Desition File Directory
-
Void Private Static Void Copyfiles (File SourceFile, ไฟล์ desfile) พ่น IOException {
if (sourcefile.isfile ()) {
ไฟล์ไฟล์ = ไฟล์ใหม่ (desfile.getPath ()+"/"+sourcefile.getName ());
FileInputStream FIS = ใหม่ FileInputStream (SourceFile);
fileOutputStream fos = new fileOutputStream (ไฟล์);
int len = 0;
ไบต์ [] buf = ไบต์ใหม่ [1024];
ในขณะที่ ((len = fis.read (buf))! = -1)
fos.write (buf, 0, len);
}อื่น{
ไฟล์ dir = ไฟล์ใหม่ (desfile.getPath ()+"/"+sourcefile.getName ());
ถ้า (! dir.exists ())
dir.mkdir ();
สตริง [] ชื่อ = sourceFile.list ();
สำหรับ (int i = 0; i <names.length; i ++) {
CopyFiles (ไฟล์ใหม่ (SourceFile.getPath ()+"/"+ชื่อ [i]), dir);
-
-
-
-
* วิธีการรวมไฟล์ทั้งหมดภายใต้ไดเรกทอรีไฟล์เป็นไฟล์เดียว (ส่วนใหญ่ใช้เพื่อรวมไฟล์ข้อความจำนวนมากเข้าด้วยกัน)
* @param SourceFile
* @param decfile
* @กลับ
* @throws ioexception
-
ไฟล์คงที่ส่วนตัว mergefile (ไฟล์ SourceFile, ไฟล์ decfile) พ่น ioexception {
String [] fileList = sourceFile.list ();
สำหรับ (สตริงสตริง: FileList) {
ไฟล์ไฟล์ = ไฟล์ใหม่ (sourceFile.getPath ()+"/"+สตริง);
if (! file.isdirectory ()) {
FileInputStream FIS = ใหม่ FileInputStream (ไฟล์);
fileOutputStream fos = new fileOutputStream (decfile, true);
ไบต์ [] บัฟเฟอร์ = ไบต์ใหม่ [1024];
int len = 0;
ในขณะที่ ((len = fis.read (บัฟเฟอร์))! = -1)
fos.write (บัฟเฟอร์, 0, len);
-
อื่น {
decfile = mergefile (ไฟล์, decfile);
-
-
กลับ decfile;
-
-