FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java-code-example/File/connect_to_internet.java at master · cherkavi/java-code-example · GitHub
cherkavi
/
java-code-example
Public
Notifications
You must be signed in to change notification settings
Fork
4
Star
0
Code
Issues
0
Pull requests
42
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
java-code-example
/
File
/
connect_to_internet.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
145 lines (139 loc) · 3.77 KB
Breadcrumbs
java-code-example
/
File
/
connect_to_internet.java
Copy path
File metadata and controls
executable file
·
145 lines (139 loc) · 3.77 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
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
import
java
.
net
.*;
import
java
.
io
.*;
import
java
.
util
.
Date
;
class
net
{
static
boolean
url_to_file
(
String
url_path
,
String
filename
){
InputStream
in
=
null
;
OutputStream
os
=
null
;
boolean
result
=
false
;
// ñîçäàòü îáúåêòû äëÿ ïîëó÷åíèÿ ïîòîêîâ
try
{
URL
url
=
new
URL
(
url_path
);
in
=
url
.
openStream
();
os
=
new
FileOutputStream
(
filename
);
byte
[]
buffer
=
new
byte
[
4096
];
int
read_bytes
=
0
;
while
((
read_bytes
=
in
.
read
(
buffer
))!=-
1
){
os
.
write
(
buffer
,
0
,
read_bytes
);
}
result
=
true
;
}
catch
(
Exception
e
){
System
.
out
.
println
(
"Detected Error:"
+
e
.
getMessage
());
}
finally
{
try
{
if
(
in
!=
null
){
in
.
close
();
}
if
(
os
!=
null
){
os
.
close
();
}
}
catch
(
Exception
e
){
System
.
out
.
println
(
"Detected Error when closing Input and Output Stream"
);
}
}
return
result
;
}
static
boolean
file_to_file
(
String
source
,
String
destination
){
InputStream
source_stream
=
null
;
OutputStream
destination_stream
=
null
;
boolean
result
=
false
;
try
{
source_stream
=
new
FileInputStream
(
source
);
destination_stream
=
new
FileOutputStream
(
destination
);
byte
[]
buffer
=
new
byte
[
4096
];
int
read_bytes
=
0
;
while
((
read_bytes
=
source_stream
.
read
(
buffer
))!=-
1
){
destination_stream
.
write
(
buffer
,
0
,
read_bytes
);
}
result
=
true
;
}
catch
(
Exception
e
){
System
.
out
.
println
(
"Detected Error: "
+
e
.
getMessage
());
result
=
false
;
}
finally
{
try
{
if
(
source_stream
!=
null
){
source_stream
.
close
();
}
if
(
destination_stream
!=
null
){
destination_stream
.
close
();
}
}
catch
(
Exception
e
){
System
.
out
.
println
(
"Error during closing"
);
}
}
return
result
;
}
static
boolean
file_to_console
(
String
source
){
boolean
result
=
false
;
FileInputStream
fis
=
null
;
FileOutputStream
fos
=
null
;
try
{
fis
=
new
FileInputStream
(
source
);
fos
=
new
FileOutputStream
(
source
+
"_temp"
);
byte
[]
buffer
=
new
byte
[
4096
];
int
quentity_byte
=
0
;
while
((
quentity_byte
=
fis
.
read
(
buffer
))!=-
1
){
String
s
=
new
String
(
buffer
,
0
,
quentity_byte
);
System
.
out
.
println
(
s
);
}
result
=
true
;
}
catch
(
Exception
e
){
System
.
out
.
println
(
"Detected Error into method <file_to_console> "
);
result
=
false
;
}
finally
{
try
{
if
(
fis
!=
null
){
fis
.
close
();
};
if
(
fos
!=
null
){
fos
.
close
();
}
}
catch
(
Exception
e
){
System
.
out
.
println
(
"Detected Error when closing files"
);
}
}
return
result
;
}
static
boolean
urlconnection_to_console
(
String
url_path
){
boolean
result
=
false
;
URLConnection
c
=
null
;
try
{
c
=(
new
URL
(
url_path
)).
openConnection
();
c
.
connect
();
System
.
out
.
println
(
"Òèï ñîäåðæèìîãî: "
+
c
.
getContentType
());
System
.
out
.
println
(
"Êîäèðîâêà: "
+
c
.
getContentEncoding
());
System
.
out
.
println
(
"Ðàçìåð ñîäåðæèìîãî: "
+
c
.
getContentLength
());
System
.
out
.
println
(
"Äàòà: "
+(
new
Date
(
c
.
getDate
())));
System
.
out
.
println
(
"Äàòà ïîñëåäíåãî èçìåíåíèÿ: "
+(
new
Date
(
c
.
getLastModified
())));
System
.
out
.
println
(
"Ñðîê ãîäíîñòè: "
+(
new
Date
(
c
.
getExpiration
())));
// åñëè ýòî HTTP ñîåäèíåíèå
if
(
c
instanceof
HttpURLConnection
){
HttpURLConnection
h
=(
HttpURLConnection
)
c
;
System
.
out
.
println
(
"Ìåòîä çàïðîñà: "
+
h
.
getRequestMethod
());
System
.
out
.
println
(
"Ñîîáùåíèå îòâåòà: "
+
h
.
getResponseMessage
());
System
.
out
.
println
(
"Êîä îòâåòà: "
+
h
.
getResponseCode
());
}
}
catch
(
Exception
e
){
System
.
out
.
println
(
" Detected exception into method <urlconnection_to_console>"
);
}
return
result
;
}
}
public
class
connect_to_internet
{
public
static
void
main
(
String
args
[]){
net
.
url_to_file
(
"http://127.0.0.1"
,
"c:
\\
3.txt"
);
net
.
file_to_file
(
"c:
\\
3.txt"
,
"c:
\\
4.txt"
);
net
.
file_to_console
(
"c:
\\
4.txt"
);
net
.
urlconnection_to_console
(
"http://127.0.0.1"
);
}
}
Back
|
FazBrowse Home
|
New Git URL