]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils/src/org/simantics/utils/strings/FileNameUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / strings / FileNameUtils.java
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
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 /*\r
12  * Created on Jan 21, 2005\r
13  * \r
14  * Copyright Toni Kalajainen\r
15  * \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
19  *\r
20  *     http://www.apache.org/licenses/LICENSE-2.0\r
21  *\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
27  */\r
28 package org.simantics.utils.strings;\r
29 \r
30 import java.io.File;\r
31 \r
32 /**\r
33  * @author Toni Kalajainen\r
34  * \r
35  */\r
36 public class FileNameUtils {\r
37 \r
38     /**\r
39      * Creates directories\r
40      * @param path\r
41      */\r
42     public static void forcePath(String path) {\r
43         // Create directories\r
44         (new File(path)).mkdirs();\r
45     }\r
46 \r
47     public static void deleteFile(String filename) {\r
48         (new File(filename)).delete();\r
49     }\r
50 \r
51     /**\r
52      * Extracts file's directory Excludes last / \\r
53      * \r
54      * @param filename\r
55      *            the file\r
56      * @return the path\r
57      */\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
64         return "";\r
65     }\r
66 \r
67     /**\r
68      * Extracts file's name\r
69      * \r
70      * @param path\r
71      *            the path\r
72      * @return the filename\r
73      */\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
79         return path;\r
80     }\r
81 \r
82     /**\r
83      * Extracts file's name before dot\r
84      * \r
85      * @param filename\r
86      *            the file name\r
87      * @return the name\r
88      */\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
95         return filename;\r
96     }\r
97 \r
98 \r
99     /**\r
100      * Extracts file's extension\r
101      * \r
102      * @param filename\r
103      *            the file name\r
104      * @return the name\r
105      */\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
111         return "";\r
112     }\r
113 \r
114     /**\r
115      * Extracts file's directory Ixcludes last / \\r
116      * \r
117      * @param filename\r
118      *            the file\r
119      * @return the path\r
120      */\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
127         return "";\r
128     }\r
129 \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
136     }\r
137 \r
138 }\r