java 字节流实现写入数据,然后读取。
public class FileRead {
public static void main(String[] args) throws IOException {
//创建路径名称
String pathname="D:\\aaa.txt";
//创建file对象
File file=new File(pathname);
//创建写出对象
FileOutputStream fos = new FileOutputStream(file);
//需要写出的字符串,然后转换成字符串数组
String str="cherish";
//写入到文件中
fos.write(str.getBytes());
//关闭流
fos.close();
//创建读取对象
FileInputStream fis = new FileInputStream("D:\\aaa.txt");
//定义有效个数
int len;
//字节数组 用来存储字节数据
byte[] bytes=new byte[10];
//读取数据
while ((len=fis.read(bytes))!=-1){
System.out.println(new String(bytes,0,len));//len有效的字节个数
}
//关闭资源
fis.close();
}
}
努力学习java的Cherish