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