Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wushizhenking
CS-Notes
提交
e027c81f
C
CS-Notes
项目概览
wushizhenking
/
CS-Notes
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
CS-Notes
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
e027c81f
编写于
6月 11, 2018
作者:
C
CyC2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
auto commit
上级
858c9b73
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
12 addition
and
14 deletion
+12
-14
notes/Java IO.md
notes/Java IO.md
+12
-14
未找到文件。
notes/Java IO.md
浏览文件 @
e027c81f
...
...
@@ -56,7 +56,7 @@ public static void listAllFiles(File dir) {
使用字节流操作进行文件复制:
```
java
public
static
void
fileCopy
(
String
src
,
String
dist
)
throws
IOException
{
public
static
void
copyFile
(
String
src
,
String
dist
)
throws
IOException
{
FileInputStream
in
=
new
FileInputStream
(
"file/1.txt"
);
FileOutputStream
out
=
new
FileOutputStream
(
"file/2.txt"
);
byte
[]
buffer
=
new
byte
[
20
*
1024
];
...
...
@@ -184,7 +184,7 @@ private transient Object[] elementData;
Java 中的网络支持:
-
InetAddress:用于表示网络上的硬件资源,即 IP 地址;
-
URL:统一资源定位符
,通过 URL 可以直接读取或者写入网络上的数据
;
-
URL:统一资源定位符;
-
Sockets:使用 TCP 协议实现网络通信;
-
Datagram:使用 UDP 协议实现网络通信。
...
...
@@ -199,7 +199,7 @@ InetAddress.getByAddress(byte[] address);
## URL
可以直接从 URL 中读取字节流数据
可以直接从 URL 中读取字节流数据
。
```
java
URL
url
=
new
URL
(
"http://www.baidu.com"
);
...
...
@@ -311,29 +311,25 @@ I/O 包和 NIO 已经很好地集成了,java.io.\* 已经以 NIO 为基础重
```
java
public
class
FastCopyFile
{
public
static
void
main
(
String
args
[])
throws
Exception
{
String
inFile
=
"data/abc.txt"
;
String
outFile
=
"data/abc-copy.txt"
;
String
inFile
=
"file/abc.txt"
;
String
outFile
=
"file/abc-copy.txt"
;
// 获得源文件的输入字节流
FileInputStream
fin
=
new
FileInputStream
(
inFile
);
// 获取输入字节流的文件通道
FileChannel
fcin
=
fin
.
getChannel
();
// 获取目标文件的输出字节流
FileOutputStream
fout
=
new
FileOutputStream
(
outFile
);
// 获取输出字节流的通道
FileChannel
fcout
=
fout
.
getChannel
();
// 为缓冲区分配 1024 个字节
ByteBuffer
buffer
=
ByteBuffer
.
allocateDirect
(
1024
);
while
(
true
)
{
// 从输入通道中读取数据到缓冲区中
int
r
=
fcin
.
read
(
buffer
);
// read() 返回 -1 表示 EOF
if
(
r
==
-
1
)
if
(
r
==
-
1
)
{
break
;
}
// 切换读写
buffer
.
flip
();
// 把缓冲区的内容写入输出文件中
...
...
@@ -477,17 +473,19 @@ public class NIOServer {
private
static
String
readDataFromSocketChannel
(
SocketChannel
sChannel
)
throws
IOException
{
ByteBuffer
buffer
=
ByteBuffer
.
allocate
(
1024
);
StringBu
ffer
data
=
new
StringBuff
er
();
StringBu
ilder
data
=
new
StringBuild
er
();
while
(
true
)
{
buffer
.
clear
();
int
n
=
sChannel
.
read
(
buffer
);
if
(
n
==
-
1
)
if
(
n
==
-
1
)
{
break
;
}
buffer
.
flip
();
int
limit
=
buffer
.
limit
();
char
[]
dst
=
new
char
[
limit
];
for
(
int
i
=
0
;
i
<
limit
;
i
++)
for
(
int
i
=
0
;
i
<
limit
;
i
++)
{
dst
[
i
]
=
(
char
)
buffer
.
get
(
i
);
}
data
.
append
(
dst
);
buffer
.
clear
();
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录