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