Splitting filenames using system file separator symbol

前端 未结 4 1514
傲寒
傲寒 2021-01-01 15:31

I have a complete file path and I want to get the file name.

I am using the following instruction:

String[] splittedFileName = fileName.split(System.         


        
4条回答
  •  Happy的楠姐
    2021-01-01 16:04

    When you write a file name, you should use System.getProperty("file.separator").

    When you read a file name, you could possibly have either the forward slash or the backward slash as a file separator.

    You might want to try the following:

    fileName = fileName.replace("\\", "/");
    String[] splittedFileName = fileName.split("/"));
    String simpleFileName = splittedFileName[splittedFileName.length-1];
    

提交回复
热议问题