java文件流操作
package common; import java.io.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import entity.Commodity; public class CommodityFile {
public List<Commodity> read(){
List<Commodity> commodities=new ArrayList<>();
File file=new File("E:/commodities1.txt");
FileInputStream fis=null;
ObjectInputStream ois=null;
//BufferedReader br=null;
//InputStreamReader isr=null;
try {
file.createNewFile();
fis=new FileInputStream(file);
ois=new ObjectInputStream(fis);
commodities=(List<Commodity>)ois.readObject();
/*isr=new InputStreamReader(fis,"utf-8");
br=new BufferedReader(isr);
String s;
while ((s=br.readLine())!=null){
Commodity commoditie=new Commodity();
String[] s1=s.split(" ");
commoditie.setName(s1[0]);
commoditie.setPrice(new BigDecimal(s1[1]));
commoditie.setDetial(s1[2]);
commodities.add(commoditie);
}*/
}catch (Exception e){
e.printStackTrace();
}
finally {
try {
/*if(br!=null){
br.close();
}if(isr!=null){
isr.close();
}*/
if(ois!=null){
ois.close();
}
if(fis!=null){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
} }
return commodities;
}
public void write(List<Commodity>commodities){
/*StringBuffer sb=new StringBuffer();
for (Commodity c:commodities){
sb.append(c.getName()+" "+c.getPrice()+" "+c.getDetial()+"\n");
}*/
File file=new File("E:/commodities1.txt");
FileOutputStream fos=null;
ObjectOutputStream oos=null;
//OutputStreamWriter osw=null;
//BufferedWriter bw=null;
try {
file.createNewFile();
fos=new FileOutputStream(file);
oos=new ObjectOutputStream(fos);
oos.writeObject(commodities);
/*osw=new OutputStreamWriter(fos,"utf-8");
bw=new BufferedWriter(osw);
bw.write(sb.toString());*/
} catch (Exception e) {
e.printStackTrace();
}finally {
/*if(bw!=null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(osw!=null){
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}*/
if(oos!=null){
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
java文件流操作的更多相关文章
- Java 文件流操作.
一.概念 在Java中,文件的输入和输出是通过流(Stream)来实现的.一个流,必有源端和目的端,它们可以是计算机内存的某些区域,也可以是磁盘文件,甚至可以是 Internet 上的某个 URL.对 ...
- Java IO流操作汇总: inputStream 和 outputStream【转】
我们在进行Android java 开发的时候,经常会遇到各种IO流操作.IO流操作一般分为两类:字符流和字节流.以“Reader”结尾都是字符流,操作的都是字符型的数据:以“Stream”结尾的都是 ...
- java字符流操作flush()方法及其注意事项
java字符流操作flush()方法及其注意事项 flush()方法介绍 查阅文档可以发现,IO流中每一个类都实现了Closeable接口,它们进行资源操作之后都需要执行close()方法将流关闭 ...
- delphi 基础之三 文件流操作
文件流操作 Delphi操作流文件:什么是流?流,简单来说就是建立在面向对象基础上的一种抽象的处理数据的工具.在流中,定义了一些处理数据的基本操作,如读取数据,写入数据等,程序员是对流进行所有操作的, ...
- (C/C++学习)2.C语言中文件流操作基本函数总结
函数所在头文件:stdio.h 说明:前半部分主要为对各个文件流操作函数的例举,后半部分着重于上机运行分析.文中部分引用自王桂林老师的C/C++课件. 1.FIELE *fopen(const cha ...
- 全面吃透JAVA Stream流操作,让代码更加的优雅
全面吃透JAVA Stream流操作,让代码更加的优雅 在JAVA中,涉及到对数组.Collection等集合类中的元素进行操作的时候,通常会通过循环的方式进行逐个处理,或者使用Stream的方式进行 ...
- java 文件类操作(转载)
11.3 I/O类使用 由于在IO操作中,需要使用的数据源有很多,作为一个IO技术的初学者,从读写文件开始学习IO技术是一个比较好的选择.因为文件是一种常见的数据源,而且读写文件也是程序员进行IO编程 ...
- java文件读写操作
Java IO系统里读写文件使用Reader和Writer两个抽象类,Reader中read()和close()方法都是抽象方法.Writer中 write(),flush()和close()方法为抽 ...
- java文件读写操作类
借鉴了项目以前的文件写入功能,实现了对文件读写操作的封装 仅仅需要在读写方法传入路径即可(可以是绝对或相对路径) 以后使用时,可以在此基础上改进,比如: 写操作: 1,对java GUI中文本框中的内 ...
随机推荐
- Hashtable、HashMap
JDK1.6 API public class Hashtable<K,V>extends Dictionary<K,V>implements Map<K,V>, ...
- wget 技巧
最近用到一个命令wget,有一个技巧分享一下. [root@py ~]# wget -m -k http://www.example.com 可以将示例网站整个打包,作为本地镜像.
- Django组件—forms组件
forms组件: 校验字段功能: 针对一个实例:注册用户. 模型:models.py class UserInfo(models.Model): name=models.CharField(max_l ...
- Django学习---路由url,视图,模板,orm操作
Django请求周期 url -> 路由系统 ->函数或者类 -> 返回字符串 或者 模板语言 Form表单提交: 点击提交 -> 进入url系统 -> 执行函数 ...
- Tomcat官方文档关于数据源配置的内容
虽然有网上有网友自己总结的文章,但说明得总是不够清晰,还是参考官方文档理解得比较透彻: http://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html h ...
- centos7 yum 安装 mysql
CentOS7默认数据库是mariadb,配置等用着不习惯,因此决定改成mysql,但是CentOS7的yum源中默认好像是没有mysql的.为了解决这个问题,我们要先下载mysql的repo源. 1 ...
- python grpc
pip install grpcio pip install grpcio-tools python -m grpc_tools.protoc -I. --python_out=. --grpc_py ...
- windows server R2 密钥
一.win2012r2激活码 永久激活 Volume版 Windows Server 2012 R2 Datacenter数据中心版: [Key]:TVNTG-VFJQ3-FQXFP-DVCP6-D3 ...
- 解剖Nginx·模块开发篇(3)ngx_http_hello_world_module 模块的基本函数实现
还记得我们定义过一个结构体如下吗? typedef struct { ngx_str_t output_words; } ngx_http_hello_world_loc_conf_t; 它就是 He ...
- Unity Remote 5 使用
从哪里下载,我是从应用商店里下载的 一. Android版 首先应该确保安装了最新的 Android SDK(这对于在设备上设置端口转发非常必要). 然后,使用 USB 连接线连接设备与电脑,并启动U ...