Android Path Separator Get fixFileSeparators(String filename)

Here you can find the source of fixFileSeparators(String filename)

Description

Fix up filename separators for platform independence.

License

Apache License

Parameter

Parameter Description
filename a filename potentially with non platform specific file separator characters ("/" or "\")

Return

the same filename but with only File.separatorChar as the separator

Declaration

public static String fixFileSeparators(String filename) 

Method Source Code

/*/*from w  w  w  .  j av  a  2 s  . c  o m*/
    Copyright 1996-2008 Ariba, Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

    $Id: //ariba/platform/util/core/ariba/util/core/FileUtil.java#24 $
 */

import ariba.util.log.Log;
import java.io.File;
import java.io.IOException;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.util.Random;
import java.util.List;

public class Main{
    /**
        Fix up file separators for platform independence. Converts
        <B>file</B> to use the appropriate separator character for the
        current platform.

        @param file a file potentially with non platform
        specific file separator characters ("/" or "\")

        @return the same <b>file</b> but with only
        File.separatorChar as the separator
        @aribaapi documented
     */
    public static File fixFileSeparators(File file) {
        if (file == null) {
            return null;
        }
        return new File(fixFileSeparators(file.getPath()));
    }
    /**
        Fix up filename separators for platform independence.
        Converts <B>filename</B> to use the appropriate separator
        character for the current platform.

        @param filename a filename potentially with non platform
        specific file separator characters ("/" or "\")

        @return the same <b>filename</b> but with only
        File.separatorChar as the separator
        @aribaapi documented
     */
    public static String fixFileSeparators(String filename) {
        if (filename == null) {
            return null;
        }
        switch (File.separatorChar) {
        case '/':
            return filename.replace('\\', File.separatorChar);
        case '\\':
            return filename.replace('/', File.separatorChar);
        default:
            Log.util.warning(2812, File.separator);
            filename = filename.replace('/', File.separatorChar);
            filename = filename.replace('\\', File.separatorChar);
            return filename;
        }
    }
}

Related

  1. getPathSeparator()
  2. getSeparator()
  3. isWindowsPathSeparator(String p_filename)
  4. fixFileSeparators(File file)