FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Lessons4JavaSE/src/unit06/FixedLengthStringIO.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
/
FixedLengthStringIO.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
32 lines (24 loc) · 1 KB
Breadcrumbs
Lessons4JavaSE
/
src
/
unit06
/
FixedLengthStringIO.java
Copy path
File metadata and controls
32 lines (24 loc) · 1 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
package
unit06
;
import
java
.
io
.*;
public
class
FixedLengthStringIO
{
/** Read fixed number of characters from a DataInput stream */
public
static
String
readFixedLengthString
(
int
size
,
DataInput
in
)
throws
IOException
{
// Declare an array of characters
char
[]
chars
=
new
char
[
size
];
// Read fixed number of characters to the array
for
(
int
i
=
0
;
i
<
size
;
i
++)
chars
[
i
] =
in
.
readChar
();
return
new
String
(
chars
);
}
/** Write fixed number of characters to a DataOutput stream */
public
static
void
writeFixedLengthString
(
String
s
,
int
size
,
DataOutput
out
)
throws
IOException
{
char
[]
chars
=
new
char
[
size
];
// Fill in string with characters
s
.
getChars
(
0
,
s
.
length
(),
chars
,
0
);
// Fill in blank characters in the rest of the array
for
(
int
i
=
Math
.
min
(
s
.
length
(),
size
);
i
<
chars
.
length
;
i
++)
chars
[
i
] =
' '
;
// Create and write a new string padded with blank characters
out
.
writeChars
(
new
String
(
chars
));
}
}
Back
|
FazBrowse Home
|
New Git URL