1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
<html>
<head>
<title>
Handbuch der Java-Programmierung, 5. Auflage
</title>
</head>
<body>
<a name="startofbody"></a>
<script language="JavaScript" src="hjp4lib.js">
</script>
<script language="JavaScript">
installKbdHandler("97,#startofbody;101,#endofbody;116,cover.html;122,k100003.html;115,search.html;105,index.html;100,JDKDOCS;112,APIDOCS;104,k100136.html;106,k100138.html;107,k100140.html;108,k100143.html");
</script>
<table border=0 cellpadding=0 cellspacing=1 width="100%">
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="cover.html"> Titel </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html"> Inhalt </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html"> Suchen </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html"> Index </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()"> DOC </a>
<td align="right">Handbuch der Java-Programmierung, 5. Auflage
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100136.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100138.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100140.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100143.html"> >> </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()"> API </a>
<td align="right">Kapitel 21 - Datei- und Verzeichnis-Handling
</table>
<hr>
<!-- Section -->
<a name="sectlevel2id021003"></a>
<h2>21.3 Informationen über die Datei </h2>
<hr>
<ul>
<li><a href="k100139.html#sectlevel2id021003">21.3 Informationen über die Datei</a>
</ul>
<hr>
<p>
Die Klasse <a href="index_f.html#ixb100973"><font color=#000080><tt>File</tt></font></a>
besitzt eine ganze Reihe von Methoden, um Informationen über
die durch das <a href="index_f.html#ixb100973"><font color=#000080><tt>File</tt></font></a>-Objekt
bezeichnete Datei oder das Verzeichnis zu gewinnen:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public boolean exists()
public boolean canWrite()
public boolean canRead()
public boolean isHidden()
public boolean isFile()
public boolean isDirectory()
public boolean isAbsolute()
public long lastModified()
public long length()
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/io/File.html" onClick="this.href=getApiDoc('java.io.File')"><font color="#660066" size=-1>java.io.File</font></a></td>
</tr>
</table>
<p>
Mit <a name="ixa101437"><a href="index_e.html#ixb101085"><font color=#000080><tt>exists</tt></font></a></a>
kann getestet werden, ob die Datei oder das Verzeichnis überhaupt
existiert. Die Methoden <a name="ixa101438"><a href="index_c.html#ixb100670"><font color=#000080><tt>canWrite</tt></font></a></a>
und <a name="ixa101439"><a href="index_c.html#ixb100669"><font color=#000080><tt>canRead</tt></font></a></a>
ermitteln, ob ein lesender bzw. schreibender Zugriff möglich
ist, mit <a name="ixa101440"><a href="index_i.html#ixb101086"><font color=#000080><tt>isHidden</tt></font></a></a>
kann festgestellt werden, ob die Datei versteckt ist. Mit <a name="ixa101441"><a href="index_i.html#ixb101087"><font color=#000080><tt>isFile</tt></font></a></a>
und <a name="ixa101442"><a href="index_i.html#ixb101088"><font color=#000080><tt>isDirectory</tt></font></a></a>
kann unterschieden werden, ob es sich um eine Datei oder ein Verzeichnis
handelt. <a name="ixa101443"><a href="index_i.html#ixb101089"><font color=#000080><tt>isAbsolute</tt></font></a></a>
gibt an, ob das Objekt mit Hilfe einer absoluten Pfadbezeichnung konstruiert
wurde.
<p>
<a name="ixa101444"><a href="index_l.html#ixb101090"><font color=#000080><tt>lastModified</tt></font></a></a>
liefert den Zeitpunkt der letzten Änderung der Datei. Der Rückgabewert
liefert die Anzahl der Millisekunden seit dem 1.1.1970 und kann entweder
direkt (etwa in arithmetischen Vergleichen) oder zur Konstruktion
eines <a href="index_d.html#ixb100558"><font color=#000080><tt>Date</tt></font></a>-Objekts
verwendet werden. Das <a href="index_d.html#ixb100558"><font color=#000080><tt>Date</tt></font></a>-Objekt
kann dann an die Methode <a name="ixa101445"><a href="index_s.html#ixb100819"><font color=#000080><tt>setTime</tt></font></a></a>
der Klasse <a href="index_g.html#ixb100793"><font color=#000080><tt>GregorianCalendar</tt></font></a>
übergeben werden, um die einzelnen Uhrzeitkomponenten zu extrahieren.
Das folgende Beispiel illustriert diese Vorgehensweise:
<a name="listingid021001"></a>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#00AA00">/* Listing2101.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">import</font> java.io.*;
<font color="#555555">004 </font><font color="#0000AA">import</font> java.util.*;
<font color="#555555">005 </font>
<font color="#555555">006 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing2101
<font color="#555555">007 </font>{
<font color="#555555">008 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">009 </font> {
<font color="#555555">010 </font> File file = <font color="#0000AA">new</font> File(args[0]);
<font color="#555555">011 </font> GregorianCalendar cal = <font color="#0000AA">new</font> GregorianCalendar();
<font color="#555555">012 </font> cal.setTime(<font color="#0000AA">new</font> Date(file.lastModified()));
<font color="#555555">013 </font> System.out.print(<font color="#0000FF">"Letzte Änderung: "</font>);
<font color="#555555">014 </font> System.out.println(
<font color="#555555">015 </font> cal.get(Calendar.DATE) + <font color="#0000FF">"."</font> +
<font color="#555555">016 </font> (cal.get(Calendar.MONTH)+1) + <font color="#0000FF">"."</font> +
<font color="#555555">017 </font> cal.get(Calendar.YEAR) + <font color="#0000FF">" "</font> +
<font color="#555555">018 </font> cal.get(Calendar.HOUR_OF_DAY) + <font color="#0000FF">":"</font> +
<font color="#555555">019 </font> cal.get(Calendar.MINUTE) + <font color="#0000FF">":"</font> +
<font color="#555555">020 </font> cal.get(Calendar.SECOND)
<font color="#555555">021 </font> );
<font color="#555555">022 </font> }
<font color="#555555">023 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing2101.java"><font color="#000055" size=-1>Listing2101.java</font></a></td>
</tr>
</table>
<i>
Listing 21.1: Modifikationsdatum einer Datei ausgeben</i></p>
<hr>
<table border=0 cellpadding=0 cellspacing=1 width="100%">
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="cover.html"> Titel </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html"> Inhalt </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html"> Suchen </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html"> Index </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()"> DOC </a>
<td align="right">Handbuch der Java-Programmierung, 5. Auflage, Addison
Wesley, Version 5.0.1
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100136.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100138.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100140.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100143.html"> >> </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()"> API </a>
<td align="right">© 1998, 2007 Guido Krüger & Thomas
Stark, <a href="http://www.javabuch.de">http://www.javabuch.de</a>
</table>
<a name="endofbody"></a>
</body>
</html>
|