1 /*******************************************************************************
2 * Copyright (c) 2007- VTT Technical Research Centre of Finland.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * VTT Technical Research Centre of Finland - initial API and implementation
10 *******************************************************************************/
12 * Created on Jan 21, 2005
14 * Copyright Toni Kalajainen
16 * Licensed under the Apache License, Version 2.0 (the "License");
17 * you may not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
20 * http://www.apache.org/licenses/LICENSE-2.0
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS,
24 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
28 package org.simantics.utils.strings;
33 * @author Toni Kalajainen
36 public class FileNameUtils {
42 public static void forcePath(String path) {
44 (new File(path)).mkdirs();
47 public static void deleteFile(String filename) {
48 (new File(filename)).delete();
52 * Extracts file's directory Excludes last / \
58 public static String extractFileDir(String filename) {
59 // construct directory name
60 String splitted[] = filename.replaceAll("\\\\", "/").split("/");
61 if (splitted.length > 1)
62 return filename.substring(0, filename.length()
63 - splitted[splitted.length - 1].length() - 1);
68 * Extracts file's name
72 * @return the filename
74 public static String extractFileName(String path) {
75 // construct directory name
76 String splitted[] = path.replaceAll("\\\\", "/").split("/");
77 if (splitted.length > 1)
78 return splitted[splitted.length-1];
83 * Extracts file's name before dot
89 public static String extractFilePreDot(String filename) {
90 // construct directory name
91 String splitted[] = filename.split("\\.");
92 if (splitted.length > 1)
93 return filename.substring(0, filename.length()
94 - splitted[splitted.length - 1].length() - 1);
100 * Extracts file's extension
106 public static String extractFileExt(String filename) {
107 // construct directory name
108 String splitted[] = filename.split("\\.");
109 if (splitted.length > 1)
110 return splitted[splitted.length-1];
115 * Extracts file's directory Ixcludes last / \
121 public static String extractFilePath(String filename) {
122 // construct directory name
123 String splitted[] = filename.replaceAll("\\\\", "/").split("/");
124 if (splitted.length > 1)
125 return filename.substring(0, filename.length()
126 - splitted[splitted.length - 1].length());
130 public static void main(String[] args) {
131 System.out.println(extractFileDir("c:\\jees/pox/file.dat"));
132 System.out.println(extractFilePath("c:\\jees/pox/file.dat"));
133 System.out.println(extractFileName("c:\\jees/pox/file.dat"));
134 System.out.println(extractFilePreDot("c:\\jees/pox/file.dat"));
135 System.out.println(extractFileExt("c:\\jees/pox/file.dat"));