[Java] 读写字符串数据

时间:2023-03-09 05:57:01
[Java] 读写字符串数据
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();
}
} }
}