เหมาะสำหรับมือใหม่มากกว่า ยังคงมีปัญหาด้านตรรกะอยู่บ้าง สามารถใช้เพื่อเรียนรู้การทำงานของไฟล์ Java
ดาวน์โหลดที่อยู่: http://yun.baidu.com/share/link?shareid=4184742416&uk=1312160419
ต่อไปนี้เป็นโค้ดการดำเนินการไฟล์ JAVA หลัก
FileHelp.java
แพ็คเกจ self.yy.filesystem.fileutil; นำเข้า android.content.Context; นำเข้า android.util.Log; นำเข้า android.widget.Toast; นำเข้า java.io.File; นำเข้า java.io.FileInputStream; นำเข้า java.io.FileNotFoundException ;นำเข้า java.io.FileOutputStream;นำเข้า java.io.IOException;นำเข้า java.net.URI;นำเข้า java.nio.channels.FileChannel;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.List; สตริงสุดท้ายแบบคงที่ส่วนตัว JPG = ".jpg"; สตริงสุดท้ายแบบคงที่สาธารณะ PNG = " .png"; Final String MP3 = ".mp3"; // บริบทบริบทคงที่สาธารณะ MP4 = ".mp4"; ISTXT = 0; สตริงคงที่ส่วนตัว TXT = ".txt"; /** * การลบไฟล์*/ การลบไฟล์บูลีนแบบสาธารณะ (ไฟล์ไฟล์) { if (file.isDirectory()) { if (file.listFiles().length > 0) { สำหรับ (ไฟล์ i : file.listFiles()) { deletfile(i); } } else { file.delete(); } } else { file.delete(); file.delete(); return true; } /** * สร้างโฟลเดอร์ใหม่* คืนจริง การสร้างไฟล์สำเร็จ* สร้างไฟล์เท็จ -> ไฟล์มีอยู่* สร้างไฟล์จริงสำเร็จ, คืนเท็จ การสร้างไฟล์ล้มเหลว (มีไฟล์อยู่ การอนุญาตไม่เพียงพอ) */ public static boolean creatFile (ชื่อไฟล์สตริง, พาธสตริง) { ไฟล์ไฟล์ = ไฟล์ใหม่ (พาธ + File.separator + ชื่อไฟล์); if (file.exists()) { return false ; } else { file.mkdir(); return true; สร้างไฟล์สำเร็จแล้วและส่งคืนค่าเท็จ การสร้างไฟล์ล้มเหลว (มีไฟล์อยู่และสิทธิ์ไม่เพียงพอ) * * */ public static boolean creatFile(ชื่อไฟล์ String, String path, int type) { String ptr = path + File. ตัวคั่น + ชื่อไฟล์ สวิตช์ (ประเภท) { กรณีที่ 0: ไฟล์ = ไฟล์ใหม่ (ptr + TXT); ค่าเริ่มต้น: ไฟล์ = ไฟล์ใหม่ (ptr); เท็จ; } อื่น ๆ { ลอง { file.createNewFile(); return true; } catch (IOException e) { return false; } } } /** * ชื่อไฟล์ที่ซ้ำกัน* * @param name ชื่อของไฟล์ที่สร้างขึ้นใหม่* @param file สถานที่ที่ ไฟล์ถูกสร้างขึ้น* / การเปลี่ยนชื่อบูลีนแบบสาธารณะ (ชื่อสตริง, ไฟล์ไฟล์) { String pathStr = file.getParent() + File.separator + name; return file.renameTo(new File(pathStr) } /** * การคัดลอกไฟล์* * @param oldFile ไฟล์ที่จะคัดลอก* @param toNewPath ตำแหน่งที่จะคัดลอกไปที่* @return boolean trun หากการคัดลอกสำเร็จ, false หากการคัดลอกล้มเหลว* * */ public static booleancopyyFile( ไฟล์ oldFile, สตริง toNewPath) { สตริง newfilepath = toNewPath + File.separator + oldFile.getName(); ไฟล์ temp = ไฟล์ใหม่ (newfilepath); // ตรวจสอบว่ามีไฟล์ที่เกี่ยวข้องในเส้นทางไฟล์ที่คัดลอกหรือไม่ หากมี ให้หยุดการดำเนินการ if (temp.exists()) { return false; } // ตรวจสอบว่าประเภทไฟล์ที่คัดลอกเป็นโฟลเดอร์ if (oldFile. isDirectory() ) { temp.mkdir(); for (File i : oldFile.listFiles()) { CopyyFile(i, temp.getPath() } } else { //หากเป็นไฟล์ ให้ลองไปป์คัดลอก { //สร้างไปป์จากไฟล์สตรีม FileInputStream fis = new FileInputStream(oldFile); FileChannel creatChannel = fis.getChannel(); //สร้างไปป์ที่เป้าหมายเอาต์พุตไฟล์ FileOutputStream fos = new FileOutputStream(newfilepath); .getChannel( ); //คัดลอกไฟล์ (การเชื่อมต่อไปป์ไลน์) getChannel.transferFrom(creatChannel, 0, creatChannel.size()); creatChannel.close(); fos.close(); fis.close(); "copey พ่ายแพ้ มีไฟล์ mebey"); e.printStackTrace(); return false; } } return true; } /** * File cut* * @param oldFile ไฟล์ที่จะตัด* @param newFilePath สถานที่ที่จะตัด* @return boolean trun หากการตัดสำเร็จ, false หากการตัดล้มเหลว*/ public static boolean cutFile(File oldFile, String newFilePath) { if (copeyFile(oldFile, newFilePath) ) { oldFile.delete(); return true; } else { return false; } } /** * รับชุดไฟล์ที่สอดคล้องกับประเภทไฟล์* * @param dir folder* ประเภทไฟล์ประเภท @param รูปแบบ ".xxx" * @return List<file> file set*/ public static List<File> getTheTypeFile(File dir, String ประเภท) { รายการ <ไฟล์> ไฟล์ = ใหม่ ArrayList<ไฟล์>(); สำหรับ (ไฟล์ i : dir.listFiles()) { String filesTyepe = getFileType(i); (type.equals(filesTyepe)) { files.add(i); } } return files; } /** * รับประเภทไฟล์* * ไฟล์ @param ไฟล์ที่ต้องตรวจสอบ * ประเภทไฟล์สตริง @return* ดังกล่าว เป็น: * ส่งผ่านไฟล์ชื่อ "test.txt" * ส่งคืน .txt * * */ public static String getFileType(File file) { String fileName = file.getName(); (fileName.contains(".")) { String fileType = fileName.substring(fileName.lastIndexOf("."), fileName.length()); return fileType; } อื่น ๆ { return null; ไฟล์คลาสเวลาดำเนินการล่าสุด * * ไฟล์ @param คลาสไฟล์ที่จะสอบถาม * @return "yy/MM/dd HH:mm:ss" สตริงข้อมูล * ตัวอย่างเช่น: * 14/07/01 01:02:03 */ สตริงคงที่สาธารณะ getCreatTime (ไฟล์ไฟล์) { เวลานาน = file.lastModified(); ปฏิทินปฏิทิน = Calendar.getInstance(); SimpleDateFormat dateFormat = new SimpleDateFormat("yy/MM/dd HH:mm: ss"); String date = dateFormat.format(calendar.getTime()); return date; } }
ข้างต้นคือเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์กับทุกคนที่เรียน Java