X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.utils%2Fsrc%2Forg%2Fsimantics%2Futils%2Fstrings%2FFileNameUtils.java;h=d85c250ee01bdc61d6d5208616d1bbdc4a22216b;hb=e0f95bc4e0fa1797b9b97aec3b3329364ca634cf;hp=a9c53a4b6070774318303aee6497ee946b9d60ea;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.utils/src/org/simantics/utils/strings/FileNameUtils.java b/bundles/org.simantics.utils/src/org/simantics/utils/strings/FileNameUtils.java index a9c53a4b6..d85c250ee 100644 --- a/bundles/org.simantics.utils/src/org/simantics/utils/strings/FileNameUtils.java +++ b/bundles/org.simantics.utils/src/org/simantics/utils/strings/FileNameUtils.java @@ -1,138 +1,138 @@ -/******************************************************************************* - * Copyright (c) 2007- VTT Technical Research Centre of Finland. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -/* - * Created on Jan 21, 2005 - * - * Copyright Toni Kalajainen - * - * 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. - */ -package org.simantics.utils.strings; - -import java.io.File; - -/** - * @author Toni Kalajainen - * - */ -public class FileNameUtils { - - /** - * Creates directories - * @param path - */ - public static void forcePath(String path) { - // Create directories - (new File(path)).mkdirs(); - } - - public static void deleteFile(String filename) { - (new File(filename)).delete(); - } - - /** - * Extracts file's directory Excludes last / \ - * - * @param filename - * the file - * @return the path - */ - public static String extractFileDir(String filename) { - // construct directory name - String splitted[] = filename.replaceAll("\\\\", "/").split("/"); - if (splitted.length > 1) - return filename.substring(0, filename.length() - - splitted[splitted.length - 1].length() - 1); - return ""; - } - - /** - * Extracts file's name - * - * @param path - * the path - * @return the filename - */ - public static String extractFileName(String path) { - // construct directory name - String splitted[] = path.replaceAll("\\\\", "/").split("/"); - if (splitted.length > 1) - return splitted[splitted.length-1]; - return path; - } - - /** - * Extracts file's name before dot - * - * @param filename - * the file name - * @return the name - */ - public static String extractFilePreDot(String filename) { - // construct directory name - String splitted[] = filename.split("\\."); - if (splitted.length > 1) - return filename.substring(0, filename.length() - - splitted[splitted.length - 1].length() - 1); - return filename; - } - - - /** - * Extracts file's extension - * - * @param filename - * the file name - * @return the name - */ - public static String extractFileExt(String filename) { - // construct directory name - String splitted[] = filename.split("\\."); - if (splitted.length > 1) - return splitted[splitted.length-1]; - return ""; - } - - /** - * Extracts file's directory Ixcludes last / \ - * - * @param filename - * the file - * @return the path - */ - public static String extractFilePath(String filename) { - // construct directory name - String splitted[] = filename.replaceAll("\\\\", "/").split("/"); - if (splitted.length > 1) - return filename.substring(0, filename.length() - - splitted[splitted.length - 1].length()); - return ""; - } - - public static void main(String[] args) { - System.out.println(extractFileDir("c:\\jees/pox/file.dat")); - System.out.println(extractFilePath("c:\\jees/pox/file.dat")); - System.out.println(extractFileName("c:\\jees/pox/file.dat")); - System.out.println(extractFilePreDot("c:\\jees/pox/file.dat")); - System.out.println(extractFileExt("c:\\jees/pox/file.dat")); - } - -} +/******************************************************************************* + * Copyright (c) 2007- VTT Technical Research Centre of Finland. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +/* + * Created on Jan 21, 2005 + * + * Copyright Toni Kalajainen + * + * 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. + */ +package org.simantics.utils.strings; + +import java.io.File; + +/** + * @author Toni Kalajainen + * + */ +public class FileNameUtils { + + /** + * Creates directories + * @param path + */ + public static void forcePath(String path) { + // Create directories + (new File(path)).mkdirs(); + } + + public static void deleteFile(String filename) { + (new File(filename)).delete(); + } + + /** + * Extracts file's directory Excludes last / \ + * + * @param filename + * the file + * @return the path + */ + public static String extractFileDir(String filename) { + // construct directory name + String splitted[] = filename.replaceAll("\\\\", "/").split("/"); + if (splitted.length > 1) + return filename.substring(0, filename.length() + - splitted[splitted.length - 1].length() - 1); + return ""; + } + + /** + * Extracts file's name + * + * @param path + * the path + * @return the filename + */ + public static String extractFileName(String path) { + // construct directory name + String splitted[] = path.replaceAll("\\\\", "/").split("/"); + if (splitted.length > 1) + return splitted[splitted.length-1]; + return path; + } + + /** + * Extracts file's name before dot + * + * @param filename + * the file name + * @return the name + */ + public static String extractFilePreDot(String filename) { + // construct directory name + String splitted[] = filename.split("\\."); + if (splitted.length > 1) + return filename.substring(0, filename.length() + - splitted[splitted.length - 1].length() - 1); + return filename; + } + + + /** + * Extracts file's extension + * + * @param filename + * the file name + * @return the name + */ + public static String extractFileExt(String filename) { + // construct directory name + String splitted[] = filename.split("\\."); + if (splitted.length > 1) + return splitted[splitted.length-1]; + return ""; + } + + /** + * Extracts file's directory Ixcludes last / \ + * + * @param filename + * the file + * @return the path + */ + public static String extractFilePath(String filename) { + // construct directory name + String splitted[] = filename.replaceAll("\\\\", "/").split("/"); + if (splitted.length > 1) + return filename.substring(0, filename.length() + - splitted[splitted.length - 1].length()); + return ""; + } + + public static void main(String[] args) { + System.out.println(extractFileDir("c:\\jees/pox/file.dat")); + System.out.println(extractFilePath("c:\\jees/pox/file.dat")); + System.out.println(extractFileName("c:\\jees/pox/file.dat")); + System.out.println(extractFilePreDot("c:\\jees/pox/file.dat")); + System.out.println(extractFileExt("c:\\jees/pox/file.dat")); + } + +}