FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Lessons4JavaSE/src/unit06/TestRandomAccessFile.java at master · AlohaWorld/Lessons4JavaSE · GitHub
AlohaWorld
/
Lessons4JavaSE
Public
Notifications
You must be signed in to change notification settings
Fork
4
Star
19
Code
Issues
1
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Lessons4JavaSE
/
src
/
unit06
/
TestRandomAccessFile.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
49 lines (36 loc) · 1.59 KB
Breadcrumbs
Lessons4JavaSE
/
src
/
unit06
/
TestRandomAccessFile.java
Copy path
File metadata and controls
49 lines (36 loc) · 1.59 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
unit06
;
import
java
.
io
.*;
public
class
TestRandomAccessFile
{
public
static
void
main
(
String
[]
args
)
throws
IOException
{
// Create a random access file
// In "rw" mode, Reading and Writing share one file pointer
RandomAccessFile
inout
=
new
RandomAccessFile
(
"inout.dat"
,
"rw"
);
// Clear the file to destroy the old contents if exists
inout
.
setLength
(
0
);
// Write new integers to the file
for
(
int
i
=
0
;
i
<
200
;
i
++)
inout
.
writeInt
(
i
);
// Display the current length of the file
System
.
out
.
println
(
"Current file length is "
+
inout
.
length
());
// Retrieve the first number
inout
.
seek
(
0
);
// Move the file pointer to the beginning
System
.
out
.
println
(
"The first number is "
+
inout
.
readInt
());
// Retrieve the second number
inout
.
seek
(
1
*
4
);
// Move the file pointer to the second number
System
.
out
.
println
(
"The second number is "
+
inout
.
readInt
());
// Retrieve the tenth number
inout
.
seek
(
9
*
4
);
// Move the file pointer to the tenth number
System
.
out
.
println
(
"The tenth number is "
+
inout
.
readInt
());
// Modify the eleventh number
inout
.
writeInt
(
555
);
// Append a new number
inout
.
seek
(
inout
.
length
());
// Move the file pointer to the end
inout
.
writeInt
(
999
);
// Display the new length
System
.
out
.
println
(
"The new length is "
+
inout
.
length
());
// Retrieve the new eleventh number
inout
.
seek
(
10
*
4
);
// Move the file pointer to the eleventh number
System
.
out
.
println
(
"The eleventh number is "
+
inout
.
readInt
());
inout
.
close
();
}
}
Back
|
FazBrowse Home
|
New Git URL