您的位置:首页 > 编程语言 > Java开发

[Java] 读写字符串数据

2016-04-13 14:48 411 查看
package test.stream;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 读写字符串数据
* @author Frost.Yen
* @E-mail 871979853@qq.com
* @date 2016年4月13日
*/
public class TestWriteData01 {
public static void main(String[] args) {
FileOutputStream fos = null;
FileInputStream fis = null;
try {
fos = new FileOutputStream("E:\\JAVA\\Examples\\To Learn\\src\\test\\stream\\data.dat");
String hello = "hello world";
byte[] buf = hello.getBytes();
fos.write(buf, 0, buf.length);

String str = String.valueOf(12024568);
fos.write(str.getBytes(), 0, str.getBytes().length);
str = String.valueOf(13);
fos.write(str.getBytes(), 0, str.getBytes().length);
str = String.valueOf(28);
fos.write(str.getBytes(), 0, str.getBytes().length);

fis = new FileInputStream("E:\\JAVA\\Examples\\To Learn\\src\\test\\stream\\data.dat");
byte[] buf1 = new byte[1024];
int len = 0;
while((len = fis.read(buf1))>=0){
System.out.write(buf1, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(fos!=null) fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(fis!=null) fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: