JAVA中几种读取文件为字符串
来源:互联网 发布:中文域名转让 编辑:程序博客网 时间:2024/06/05 16:16
方式一
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
/**
* 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
* 当然也是可以读字符串的。
*/
/* 貌似是说网络环境中比较复杂,每次传过来的字符是定长的,用这种方式?*/
public
String readString1()
{
try
{
//FileInputStream 用于读取诸如图像数据之类的原始字节流。要读取字符流,请考虑使用 FileReader。
FileInputStream inStream=
this
.openFileInput(FILE_NAME);
ByteArrayOutputStream bos =
new
ByteArrayOutputStream();
byte
[] buffer=
new
byte
[
1024
];
int
length=-
1
;
while
( (length = inStream.read(buffer) != -
1
)
{
bos.write(buffer,
0
,length);
// .write方法 SDK 的解释是 Writes count bytes from the byte array buffer starting at offset index to this stream.
// 当流关闭以后内容依然存在
}
bos.close();
inStream.close();
return
bos.toString();
// 为什么不一次性把buffer得大小取出来呢?为什么还要写入到bos中呢? return new(buffer,"UTF-8") 不更好么?
// return new String(bos.toByteArray(),"UTF-8");
}
}
方式二
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 有人说了 FileReader 读字符串更好,那么就用FileReader吧
// 每次读一个是不是效率有点低了?
private
static
String readString2()
{
StringBuffer str=
new
StringBuffer(
""
);
File file=
new
File(FILE_IN);
try
{
FileReader fr=
new
FileReader(file);
int
ch =
0
;
while
((ch = fr.read())!=-
1
)
{
System.out.print((
char
)ch+
" "
);
}
fr.close();
}
catch
(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(
"File reader出错"
);
}
return
str.toString();
}
方式三
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*按字节读取字符串*/
/* 个人感觉最好的方式,(一次读完)读字节就读字节吧,读完转码一次不就好了*/
private
static
String readString3()
{
String str=
""
;
File file=
new
File(FILE_IN);
try
{
FileInputStream in=
new
FileInputStream(file);
// size 为字串的长度 ,这里一次性读完
int
size=in.available();
byte
[] buffer=
new
byte
[size];
in.read(buffer);
in.close();
str=
new
String(buffer,
"GB2312"
);
}
catch
(IOException e) {
// TODO Auto-generated catch block
return
null
;
e.printStackTrace();
}
return
str;
}
方式四
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
/*InputStreamReader+BufferedReader读取字符串 , InputStreamReader类是从字节流到字符流的桥梁*/
/* 按行读对于要处理的格式化数据是一种读取的好方式 */
private
static
String readString4()
{
int
len=
0
;
StringBuffer str=
new
StringBuffer(
""
);
File file=
new
File(FILE_IN);
try
{
FileInputStream is=
new
FileInputStream(file);
InputStreamReader isr=
new
InputStreamReader(is);
BufferedReader in=
new
BufferedReader(isr);
String line=
null
;
while
( (line=in.readLine())!=
null
)
{
if
(len !=
0
)
// 处理换行符的问题
{
str.append(
"\r\n"
+line);
}
else
{
str.append(line);
}
len++;
}
in.close();
is.close();
}
catch
(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return
str.toString();
}
0 0
- JAVA中几种读取文件为字符串
- JAVA中几种读取文件为字符串
- JAVA中几种读取文件为字符串
- JAVA中读取文件转换为字符串
- java读取文件内容为string字符串
- Java读取文件内容并转换为字符串
- Java读取文件内容并转换为字符串
- Java读取文件内容并转换为字符串
- java读取文件为string
- 读取文件转为java字符串
- java读取文件 返回字符串
- 读取Json文件为String字符串
- python将文件读取为字符串
- java 读取clob,blob转换为字符串
- java io ---文件读取为byte数组
- JAVA读取FTP文件并转为字符串
- java读取文件内容转成字符串
- JAVA 读取文件字符串乱码问题
- 筛法求素数,组合数公式,唯一分解定理(选择与除法,uva 10375)
- C++ Primer | 第三章 字符串、向量和数组
- String的常用方法
- 移动手机APP测试从零开始(高级篇)
- JAVA从菜鸟【入门】到新手【实习】一一转入第二语言Python学习“灵活学习,学练结合,经验积累”
- JAVA中几种读取文件为字符串
- ORA-00600: 内部错误代码, 参数: [19004], [], [], [], [], []
- TCP/IP、Http、Socket的区别
- 安卓学习 fill_parent、wrap_content和match_parent的区别(学习总结)
- #308 – 获得当前拥有键盘输入焦点的控件(Checking to See Which Control Has Keyboard Focus)
- Storm Topology的性能瓶颈
- Scrapy网络爬虫框架实战[以腾讯新闻网为例]
- Java读取文件内容的几种方式
- 玩转微信应用号,「小程序」开发实操指南第一弹