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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
<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,k100130.html;106,k100132.html;107,k100134.html;108,k100136.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="k100130.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100132.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100134.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100136.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 20 - Random-Access-I/O
</table>
<hr>
<!-- Section -->
<a name="sectlevel2id020003"></a>
<h2>20.3 Lesezugriffe </h2>
<hr>
<ul>
<li><a href="k100133.html#sectlevel2id020003">20.3 Lesezugriffe</a>
</ul>
<hr>
<p>
Für die lesenden Zugriffe auf eine Random-Access-Datei stehen
die folgenden Methoden zur Verfügung: <a name="ixa101403"></a>
<a name="ixa101404"></a> <a name="ixa101405"></a> <a name="ixa101406"></a>
<a name="ixa101407"></a> <a name="ixa101408"></a> <a name="ixa101409"></a>
<a name="ixa101410"></a> <a name="ixa101411"></a> <a name="ixa101412"></a>
<a name="ixa101413"></a> <a name="ixa101414"></a> <a name="ixa101415"></a>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public final boolean readBoolean()
public final byte readByte()
public final char readChar()
public final double readDouble()
public final float readFloat()
public final int readInt()
public final long readLong()
public final short readShort()
public final String readUTF()
public final void readFully(byte[] b)
public final void readFully(byte[] b, int off, int len)
public final String readLine()
public final int readUnsignedByte()
public final int readUnsignedShort()
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/io/RandomAccessFile.html" onClick="this.href=getApiDoc('java.io.RandomAccessFile')"><font color="#660066" size=-1>java.io.RandomAccessFile</font></a></td>
</tr>
</table>
<p>
Sie lesen jeweils ein Element des angegebenen Typs und erwarten, dass
es in der Datei in dem durch die korrespondierende <a href="index_w.html#ixb100959"><font color=#000080><tt>write</tt></font></a>...-Methode
vorgegebenen binären Format vorliegt. <a href="index_r.html#ixb101043"><font color=#000080><tt>readFully</tt></font></a>
kann dazu verwendet werden, beliebig viele Datenbytes ungeachtet ihres
Datentyps einzulesen. <a href="index_r.html#ixb100999"><font color=#000080><tt>readLine</tt></font></a>
liest eine ganze Zeile Text aus und gibt sie als <a href="index_s.html#ixb100117"><font color=#000080><tt>String</tt></font></a>
an den Aufrufer zurück.
<p>
Darüber hinaus steht auch eine Reihe von <a href="index_r.html#ixb100984"><font color=#000080><tt>read</tt></font></a>-Methoden
<a name="ixa101416"></a> zur Verfügung, die zum Einlesen eines
einzelnen Bytes oder einer Menge von Bytes verwendet werden können:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public int read()
public int read(byte[] b)
public int read(byte[] b, int off, int len)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/io/RandomAccessFile.html" onClick="this.href=getApiDoc('java.io.RandomAccessFile')"><font color="#660066" size=-1>java.io.RandomAccessFile</font></a></td>
</tr>
</table>
<p>
Das folgende Listing zeigt die Verwendung der Klasse <a href="index_r.html#ixb101016"><font color=#000080><tt>RandomAccessFile</tt></font></a>
am Beispiel eines Programms, das die Signatur und Versionsnummer aus
einem <font color="#660099">.class</font>-File <a name="ixa101417"></a>
herausliest. Die Signatur einer Klassendatei ergibt das Wort »CAFEBABE«<a name="ixa101418"></a>,
wenn man die hexadezimale Darstellung der ersten vier Bytes ausgibt.
In den nächsten beiden Bytes folgt die Minor-Versionsnummer und
in den darauffolgenden zwei Bytes die Major-Versionsnummer. Die Versionsnummer
liegt bis zum JDK 1.3 bei 45.3 und wurde mit dem JDK 1.4 auf 46.0
geändert.
<p>
Das Programm implementiert eine Klasse <font color="#000077"><tt>ClassFileReader</tt></font>,
die den Zugriff auf die Klassendatei ermöglicht. Der Konstruktor
öffnet die Datei, und die Ausgabe der Signatur und Versionsinformation
erfolgt mit Hilfe der Methoden <font color="#000077"><tt>printSignature</tt></font>
und <font color="#000077"><tt>printVersion</tt></font>.
<p>
Das Einlesen der Signatur erfolgt durch Lesen der ersten 4 Byte der
Datei, die dann jeweils in High- und Lowbyte zerlegt und in ihre hexadezimale
Darstellung umgewandelt werden. Bei der Verwendung der Methode <a href="index_r.html#ixb100984"><font color=#000080><tt>read</tt></font></a>
zum Einlesen der Bytes ist zu beachten, dass der Rückgabewert
vom Typ <a href="index_i.html#ixb100244"><font color=#000080><tt>int</tt></font></a>
ist. Er darf auch nicht in ein <a href="index_b.html#ixb100242"><font color=#000080><tt>byte</tt></font></a>
konvertiert werden, weil es sonst einen Vorzeichenüberlauf geben
würde. Das Einlesen der Versionsnummern erfolgt mit der Methode
<a href="index_r.html#ixb101066"><font color=#000080><tt>readShort</tt></font></a>,
die einen vorzeichenlosen 16-Bit-Wert aus der Datei liest. Auch hier
ist der Rückgabewert vom Typ <a href="index_i.html#ixb100244"><font color=#000080><tt>int</tt></font></a>,
um den gesamten Wertebereich von 0 bis 65535 darstellen zu können.
<a name="listingid020001"></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">/* Listing2001.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="#555555">005 </font><font color="#0000AA">class</font> ClassFileReader
<font color="#555555">006 </font>{
<font color="#555555">007 </font> <font color="#0000AA">private</font> RandomAccessFile f;
<font color="#555555">008 </font>
<font color="#555555">009 </font> <font color="#0000AA">public</font> ClassFileReader(String name)
<font color="#555555">010 </font> <font color="#0000AA">throws</font> IOException
<font color="#555555">011 </font> {
<font color="#555555">012 </font> <font color="#0000AA">if</font> (!name.endsWith(<font color="#0000FF">".class"</font>)) {
<font color="#555555">013 </font> name += <font color="#0000FF">".class"</font>;
<font color="#555555">014 </font> }
<font color="#555555">015 </font> f = <font color="#0000AA">new</font> RandomAccessFile(name,<font color="#0000FF">"r"</font>);
<font color="#555555">016 </font> }
<font color="#555555">017 </font>
<font color="#555555">018 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> close()
<font color="#555555">019 </font> {
<font color="#555555">020 </font> <font color="#0000AA">if</font> (f != <font color="#006699">null</font>) {
<font color="#555555">021 </font> <font color="#0000AA">try</font> {
<font color="#555555">022 </font> f.close();
<font color="#555555">023 </font> } <font color="#0000AA">catch</font> (IOException e) {
<font color="#555555">024 </font> <font color="#00AA00">//nichts</font>
<font color="#555555">025 </font> }
<font color="#555555">026 </font> }
<font color="#555555">027 </font> }
<font color="#555555">028 </font>
<font color="#555555">029 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> printSignature()
<font color="#555555">030 </font> <font color="#0000AA">throws</font> IOException
<font color="#555555">031 </font> {
<font color="#555555">032 </font> String ret = <font color="#0000FF">""</font>;
<font color="#555555">033 </font> <font color="#006699">int</font> b;
<font color="#555555">034 </font>
<font color="#555555">035 </font> f.seek(0);
<font color="#555555">036 </font> <font color="#0000AA">for</font> (<font color="#006699">int</font> i=0; i<4; ++i) {
<font color="#555555">037 </font> b = f.read();
<font color="#555555">038 </font> ret += (<font color="#006699">char</font>)(b/16+<font color="#0000FF">'A'</font>-10);
<font color="#555555">039 </font> ret += (<font color="#006699">char</font>)(b%16+<font color="#0000FF">'A'</font>-10);
<font color="#555555">040 </font> }
<font color="#555555">041 </font> System.out.println(
<font color="#555555">042 </font> <font color="#0000FF">"Signatur...... "</font>+
<font color="#555555">043 </font> ret
<font color="#555555">044 </font> );
<font color="#555555">045 </font> }
<font color="#555555">046 </font>
<font color="#555555">047 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> printVersion()
<font color="#555555">048 </font> <font color="#0000AA">throws</font> IOException
<font color="#555555">049 </font> {
<font color="#555555">050 </font> <font color="#006699">int</font> minor, major;
<font color="#555555">051 </font>
<font color="#555555">052 </font> f.seek(4);
<font color="#555555">053 </font> minor = f.readShort();
<font color="#555555">054 </font> major = f.readShort();
<font color="#555555">055 </font> System.out.println(
<font color="#555555">056 </font> <font color="#0000FF">"Version....... "</font>+
<font color="#555555">057 </font> major+<font color="#0000FF">"."</font>+minor
<font color="#555555">058 </font> );
<font color="#555555">059 </font> }
<font color="#555555">060 </font>}
<font color="#555555">061 </font>
<font color="#555555">062 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing2001
<font color="#555555">063 </font>{
<font color="#555555">064 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">065 </font> {
<font color="#555555">066 </font> ClassFileReader f;
<font color="#555555">067 </font>
<font color="#555555">068 </font> <font color="#0000AA">try</font> {
<font color="#555555">069 </font> f = <font color="#0000AA">new</font> ClassFileReader(<font color="#0000FF">"Listing2001"</font>);
<font color="#555555">070 </font> f.printSignature();
<font color="#555555">071 </font> f.printVersion();
<font color="#555555">072 </font> } <font color="#0000AA">catch</font> (IOException e) {
<font color="#555555">073 </font> System.out.println(e.toString());
<font color="#555555">074 </font> }
<font color="#555555">075 </font> }
<font color="#555555">076 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing2001.java"><font color="#000055" size=-1>Listing2001.java</font></a></td>
</tr>
</table>
<i>
Listing 20.1: Lesen einer .class-Datei mit der Klasse RandomAccessFile</i></p>
<p>
Die Ausgabe des Programms ist:
<font color="#333300">
<pre>
Signatur...... CAFEBABE
Version....... 45.3
</pre>
</font>
<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="k100130.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100132.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100134.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100136.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>
|