01: /*
02: * WbFile.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.util;
13:
14: import java.io.File;
15: import java.io.FileOutputStream;
16: import java.io.IOException;
17:
18: /**
19: * @author support@sql-workbench.net
20: */
21: public class WbFile extends File {
22:
23: public WbFile(String parent, String filename) {
24: super (parent, filename);
25: }
26:
27: public WbFile(File parent, String filename) {
28: super (parent, filename);
29: }
30:
31: public WbFile(File f) {
32: super (f.getAbsolutePath());
33: }
34:
35: public WbFile(String filename) {
36: super (filename);
37: }
38:
39: /**
40: * Returns the filename without an extension
41: */
42: public String getFileName() {
43: String name = getName();
44: int pos = name.lastIndexOf('.');
45: if (pos == -1)
46: return name;
47: return name.substring(0, pos);
48: }
49:
50: public String getExtension() {
51: String name = getName();
52: int pos = name.lastIndexOf('.');
53: if (pos == -1)
54: return null;
55: return name.substring(pos + 1);
56: }
57:
58: public boolean isWriteable() {
59: if (exists())
60: return canWrite();
61: return canCreate();
62: }
63:
64: public boolean canCreate() {
65: try {
66: tryCreate();
67: return true;
68: } catch (IOException e) {
69: return false;
70: }
71: }
72:
73: public void tryCreate() throws IOException {
74: FileOutputStream out = null;
75: try {
76: out = new FileOutputStream(this );
77: } catch (IOException e) {
78: throw e;
79: } finally {
80: try {
81: out.close();
82: } catch (Throwable th) {
83: }
84: this .delete();
85: }
86: }
87:
88: /**
89: * Returns the canoncial name for this file
90: * @return the canonical filename or the absolute filename if getCanonicalPath threw an Exception
91: */
92: public String getFullPath() {
93: try {
94: return this .getCanonicalPath();
95: } catch (Throwable th) {
96: return this.getAbsolutePath();
97: }
98: }
99: }
|