Question

I am trying to split a filepath using a StringTokenizer independent of the Platform (Windows/Solaris/Linux).

For example: c:\folder1\folder2\sample.xls would turn into folder1, folder2, sample.xls in the StringTokenizer

and /folder1/folder2/sample.xls would turn into folder1, folder2, sample.xls in the String Tokenizer

So far I have the file split working but I have the slash hard coded and it works on windows, but I would like to use File.seperator or something similar instead of hard coding the slash so that the code is platform independent. I appreciate any help/suggestions, thank you!

public static void main(String[] args)

{

File path = new File(C:\folder1\folder2\sample.xls);
// I do not want the slash below hard coded
StringTokenizer st = new StringTokenizer(suiteName, "/");
while(st.hasMoreElements())
{
    String item = (String)st.nextElement();
    if(st.countTokens() == 0)
    {
        //Now this is the excel file
        System.out.println("This is the excel file: " + item);
    }
    else
    {
        System.out.println("This is the folder: " + item);
    }
}

}

Was it helpful?

Solution

You can use File.separator to get a system-dependent file separator.

StringTokenizer st = new StringTokenizer(suiteName, File.separator);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top