Android Path Normalize normalizeFile(String path)

Here you can find the source of normalizeFile(String path)

Description

normalize File

License

Open Source License

Declaration

public static File normalizeFile(String path) 

Method Source Code

//package com.java2s;
/*//  www .  jav  a 2  s  . c o  m
 * Copyright (c) 2002-2012 Alibaba Group Holding Limited.
 * All rights reserved.
 *
 * 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.
 */

import java.io.File;

public class Main {
    public static File normalizeFile(String path) {
        return normalizeFile(new File(path));
    }

    public static File normalizeFile(File file) {
        File basedir = file.getParentFile();
        String filename = file.getName();
        StringBuffer buffer = new StringBuffer(filename.length());

        char lastChar = '\0';

        for (int i = 0; i < filename.length(); i++) {
            char c = filename.charAt(i);

            if (c == '_' || c == '-' || c == '.' || c == ',') {
                if (lastChar != '\0') {
                    if (c != lastChar) {
                        buffer.append(c);
                    }

                    lastChar = c;
                }
            } else {
                buffer.append(c);
                lastChar = c;
            }
        }

        int index = buffer.length() - 1;

        for (; index > 0; index--) {
            if (buffer.charAt(index) == '.') {
                for (int i = index - 1; i >= 0; i--) {
                    char c = buffer.charAt(i);

                    if (c == '_' || c == '-' || c == '.' || c == ',') {
                        buffer.deleteCharAt(i);
                    } else {
                        break;
                    }
                }
            }
        }

        return new File(basedir, buffer.toString()).getAbsoluteFile();
    }
}

Related

  1. getCanonicalPath(File pFile)
  2. normalizePath(String path)
  3. normalizePath(String path, boolean isWindows)
  4. normalizeUnixPath(String path)
  5. normalizeWindowsPath(String path)