文件输出为字符串示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
/**
* 读取文件为字符串
*
* @return
*/
public static string readstring() {
string str = "" ;
file file = new file( "c:/users/wan7/desktop/表单/粗集料试验/粗集料冲击值试验(t0322-2000).html" );
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, "utf-8" );
} catch (ioexception e) {
return null ;
}
return str;
}
|
字符串输出为文件示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
/**
* 输出到文件
*/
public static void outfile(string s) {
file file = new file( "c:/users/wan7/desktop/11111111111.html" );
try (fileoutputstream fop = new fileoutputstream(file)) {
// if file doesn't exists, then create it
if (!file.exists()) {
file.createnewfile();
}
// get the content in bytes
byte [] contentinbytes = s.getbytes();
fop.write(contentinbytes);
fop.flush();
fop.close();
system.out.println( "done" );
} catch (ioexception e) {
e.printstacktrace();
}
}
|
以上这篇java操作文件输出为字符串以及字符串输出为文件的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/ffj0721/article/details/73996576