FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java-Programming/Java NIO/TransferData.java at main · yogesh5259/Java-Programming · GitHub
yogesh5259
/
Java-Programming
Public
Notifications
You must be signed in to change notification settings
Fork
2
Star
1
Code
Issues
0
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
Java-Programming
/
Java NIO
/
TransferData.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
74 lines (49 loc) · 2.11 KB
Breadcrumbs
Java-Programming
/
Java NIO
/
TransferData.java
Copy path
File metadata and controls
74 lines (49 loc) · 2.11 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
import
java
.
io
.
File
;
import
java
.
io
.
FileInputStream
;
import
java
.
io
.
FileOutputStream
;
import
java
.
io
.
RandomAccessFile
;
import
java
.
nio
.
channels
.
FileChannel
;
import
java
.
nio
.
channels
.
WritableByteChannel
;
import
java
.
util
.
Scanner
;
// Transfer data from 1 file to other file using Java-NIO
public
class
TransferData
{
public
static
void
main
(
String
[]
args
) {
try
(
Scanner
scanner
=
new
Scanner
(
System
.
in
)) {
System
.
out
.
println
(
"Select The opetion That Listed below : "
);
System
.
out
.
println
(
"1 : TransferTo"
);
System
.
out
.
println
(
"2 : TransferFrom"
);
switch
(
scanner
.
next
()) {
case
"1"
:
// Destination File
FileOutputStream
outputStream
=
new
FileOutputStream
(
new
File
(
"G:/1.txt"
));
WritableByteChannel
destinationChannel
=
outputStream
.
getChannel
();
// Write Data in Source File
FileOutputStream
sourceFileStream
=
new
FileOutputStream
(
"G:/12.txt"
);
sourceFileStream
.
write
(
"Data Write in Source File"
.
getBytes
());
sourceFileStream
.
close
();
// Source File open with read mode
FileInputStream
inputStream
=
new
FileInputStream
(
new
File
(
"G:/12.txt"
));
FileChannel
sourceChannel
=
inputStream
.
getChannel
();
sourceChannel
.
transferTo
(
0
,
sourceChannel
.
size
(),
destinationChannel
);
sourceChannel
.
close
();
inputStream
.
close
();
System
.
out
.
println
(
"Successfully Transfer The Data from '1.txt' to '12.txt'"
);
break
;
case
"2"
:
FileOutputStream
outputStream2
=
new
FileOutputStream
(
new
File
(
"G:/22.txt"
));
outputStream2
.
write
(
"Write Data in source file"
.
getBytes
());
outputStream2
.
close
();
// by Using Random Access File with JAVA-NIO
RandomAccessFile
sourceFile
=
new
RandomAccessFile
(
"G:/22.txt"
,
"r"
);
FileChannel
sourceChannelFrom
=
sourceFile
.
getChannel
();
RandomAccessFile
destFile
=
new
RandomAccessFile
(
"G:/33.txt"
,
"rw"
);
FileChannel
destinationChannelFrom
=
destFile
.
getChannel
();
destinationChannelFrom
.
transferFrom
(
sourceChannelFrom
,
0
,
sourceChannelFrom
.
size
());
System
.
out
.
println
(
"Successfully Transfer The Data from '22.txt' to '33.txt'"
);
break
;
}
}
catch
(
Exception
e
) {
e
.
printStackTrace
();
}
}
}
Back
|
FazBrowse Home
|
New Git URL