วิธีรับdddจากชื่อพา ธ ที่ test.java อยู่
File file = new File("C:/aaa/bbb/ccc/ddd/test.java");
วิธีรับdddจากชื่อพา ธ ที่ test.java อยู่
File file = new File("C:/aaa/bbb/ccc/ddd/test.java");
คำตอบ:
ใช้File's getParentFile()วิธีการและString.lastIndexOf()เพื่อดึงเพียงไดเรกทอรีหลักทันที
ความคิดเห็นของ Mark เป็นทางออกที่ดีกว่าlastIndexOf():
file.getParentFile().getName();
โซลูชันเหล่านี้ใช้งานได้ก็ต่อเมื่อไฟล์นั้นมีไฟล์พาเรนต์ (เช่นสร้างผ่านตัวสร้างไฟล์ตัวใดตัวหนึ่งที่ใช้พาเรนต์File) เมื่อgetParentFile()ใดที่เป็นโมฆะคุณจะต้องหันไปใช้lastIndexOfหรือใช้บางอย่างเช่นApache Commons 'FileNameUtils.getFullPath() :
FilenameUtils.getFullPathNoEndSeparator(file.getAbsolutePath());
=> C:/aaa/bbb/ccc/ddd
มีหลายตัวแปรในการเก็บ / วางคำนำหน้าและตัวคั่นต่อท้าย คุณสามารถใช้FilenameUtilsคลาสเดียวกันเพื่อคว้าชื่อจากผลลัพธ์ใช้lastIndexOfฯลฯ
lastIndexOf file.getParentFile().getName()
null(ถ้าคุณสร้างFileอินสแตนซ์มีเส้นทางญาติ) - file.getAbsoluteFile().getParentFile().getName()ลอง
File f = new File("C:/aaa/bbb/ccc/ddd/test.java");
System.out.println(f.getParentFile().getName())
f.getParentFile() สามารถเป็นค่าว่างได้ดังนั้นคุณควรตรวจสอบ
ใช้ด้านล่าง
File file = new File("file/path");
String parentPath = file.getAbsoluteFile().getParent();
ใน Java 7 คุณมีPaths apiใหม่ วิธีแก้ปัญหาที่ทันสมัยและสะอาดที่สุดคือ:
Paths.get("C:/aaa/bbb/ccc/ddd/test.java").getParent().getFileName();
ผลลัพธ์จะเป็น:
C:/aaa/bbb/ccc/ddd
หากคุณมีแค่ String path และไม่ต้องการสร้าง File object ใหม่คุณสามารถใช้สิ่งต่อไปนี้:
public static String getParentDirPath(String fileOrDirPath) {
boolean endsWithSlash = fileOrDirPath.endsWith(File.separator);
return fileOrDirPath.substring(0, fileOrDirPath.lastIndexOf(File.separatorChar,
endsWithSlash ? fileOrDirPath.length() - 2 : fileOrDirPath.length() - 1));
}
File file = new File("C:/aaa/bbb/ccc/ddd/test.java");
File curentPath = new File(file.getParent());
//get current path "C:/aaa/bbb/ccc/ddd/"
String currentFolder= currentPath.getName().toString();
//get name of file to string "ddd"
หากคุณต้องการต่อท้ายโฟลเดอร์ "ddd" โดยใช้เส้นทางอื่น
String currentFolder= "/" + currentPath.getName().toString();
จาก java 7 ฉันต้องการใช้ Path คุณจะต้องใส่เส้นทางลงใน:
Path dddDirectoryPath = Paths.get("C:/aaa/bbb/ccc/ddd/test.java");
และสร้างวิธีการรับ:
public String getLastDirectoryName(Path directoryPath) {
int nameCount = directoryPath.getNameCount();
return directoryPath.getName(nameCount - 1);
}
ใน Groovy:
ไม่จำเป็นต้องสร้างFileอินสแตนซ์เพื่อแยกวิเคราะห์สตริงให้ยุ่งยาก สามารถทำได้ดังนี้:
String path = "C:/aaa/bbb/ccc/ddd/test.java"
path.split('/')[-2] // this will return ddd
การแบ่งจะสร้างอาร์เรย์[C:, aaa, bbb, ccc, ddd, test.java]และดัชนี-2จะชี้ไปที่รายการก่อนรายการสุดท้ายซึ่งในกรณีนี้คือddd
//get the parentfolder name
File file = new File( System.getProperty("user.dir") + "/.");
String parentPath = file.getParentFile().getName();
สำหรับ Kotlin:
fun getFolderName() {
val uri: Uri
val cursor: Cursor?
uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
val projection = arrayOf(MediaStore.Audio.AudioColumns.DATA)
cursor = requireActivity().contentResolver.query(uri, projection, null, null, null)
if (cursor != null) {
column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.DATA)
}
while (cursor!!.moveToNext()) {
absolutePathOfImage = cursor.getString(column_index_data)
val fileName: String = File(absolutePathOfImage).parentFile.name
}
}
test.javaอาจไม่มีอยู่ในคอมพิวเตอร์ที่กำลังเรียกใช้โปรแกรม เป็น.classไฟล์คอมไพล์ที่รัน ดังนั้นสิ่งนี้จะใช้ได้ก็ต่อเมื่อคุณรู้ว่าอยู่ที่ไหนdddในกรณีนี้จะไม่มีจุดใดที่จะค้นหาได้โดยใช้โปรแกรม ยากแค่รหัสมัน