FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaRepository/Boris/src/laba32/BinaryFile.java at master · karzhouAndrew/JavaRepository · GitHub
karzhouAndrew
/
JavaRepository
Public
Notifications
You must be signed in to change notification settings
Fork
16
Star
0
Code
Issues
0
Pull requests
96
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaRepository
/
Boris
/
src
/
laba32
/
BinaryFile.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
114 lines (101 loc) · 3.41 KB
Breadcrumbs
JavaRepository
/
Boris
/
src
/
laba32
/
BinaryFile.java
Copy path
File metadata and controls
114 lines (101 loc) · 3.41 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
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
package
laba32
;
//Записать в двоичный файл 20 случайных чисел. Прочитать записан-ный файл,
// распечатать числа и их среднее арифметическое.
import
java
.
io
.*;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
List
;
import
java
.
util
.
Random
;
public
class
BinaryFile
{
private
File
file
;
public
BinaryFile
(
File
file
) {
this
.
file
=
file
;
}
public
BinaryFile
(
String
str
) {
this
(
new
File
(
str
));
}
public
void
writeNumbersToBinaryFile
(
int
quantity
) {
DataOutputStream
outputFile
=
null
;
try
{
outputFile
=
new
DataOutputStream
(
new
BufferedOutputStream
(
new
FileOutputStream
(
file
)));
writeToFile
(
addRandomNumbers
(
quantity
),
outputFile
);
}
catch
(
FileNotFoundException
e
) {
System
.
out
.
println
(
"Exception opened file."
);
}
catch
(
IOException
e
) {
System
.
out
.
println
(
"Exception add numbers."
);
}
finally
{
if
(
outputFile
!=
null
) {
try
{
outputFile
.
close
();
}
catch
(
IOException
e
) {
System
.
out
.
println
(
"Exception closed file."
);
}
}
}
}
private
void
writeToFile
(
List
<
Integer
>
numbers
,
DataOutputStream
outputFile
)
throws
IOException
{
for
(
Integer
number
:
numbers
) {
outputFile
.
writeInt
(
number
);
}
}
private
List
<
Integer
>
addRandomNumbers
(
int
quantity
) {
List
<
Integer
>
numbers
=
new
ArrayList
<>();
if
(
quantity
>
0
) {
for
(
int
i
=
0
;
i
<
quantity
;
i
++) {
numbers
.
add
(
generateRandomNumber
());
}
}
else
{
for
(
int
i
=
quantity
;
i
<
0
;
i
++) {
numbers
.
add
(
i
);
}
}
return
numbers
;
}
private
Integer
generateRandomNumber
() {
return
new
Random
().
nextInt
(
10
);
}
public
List
<
Integer
>
getNumbersFromBinaryFile
() {
List
<
Integer
>
numbers
=
new
ArrayList
<>();
DataInputStream
reader
=
null
;
try
{
reader
=
new
DataInputStream
(
new
BufferedInputStream
(
new
FileInputStream
(
file
)));
getNumbers
(
reader
,
numbers
);
}
catch
(
FileNotFoundException
e
) {
System
.
out
.
println
(
"Exception opened file."
);
}
catch
(
IOException
e
) {
System
.
out
.
println
(
"Exception read file."
);
}
finally
{
if
(
reader
!=
null
) {
try
{
reader
.
close
();
}
catch
(
IOException
e
) {
System
.
out
.
println
(
"Exception closed file."
);
}
}
}
return
numbers
;
}
private
void
getNumbers
(
DataInputStream
reader
,
List
<
Integer
>
numbers
)
throws
IOException
{
while
(
true
) {
try
{
numbers
.
add
(
reader
.
readInt
());
}
catch
(
EOFException
e
) {
break
;
}
}
}
public
double
calculateAverage
() {
List
<
Integer
>
numbers
=
getNumbersFromBinaryFile
();
double
sum
=
0
;
if
(
numbers
.
isEmpty
()) {
return
0
;
}
else
{
return
calculateSum
(
numbers
,
sum
) /
numbers
.
size
();
}
}
private
double
calculateSum
(
List
<
Integer
>
numbers
,
double
sum
) {
for
(
Integer
number
:
numbers
) {
sum
+=
number
;
}
return
sum
;
}
}
Back
|
FazBrowse Home
|
New Git URL