IO流读写文件
来源:互联网 发布:赛迪智库待遇知乎 编辑:程序博客网 时间:2024/06/07 06:28
java已经有强大的类库,其中有一类就是IO读写的类。以下是进行文件的复制。
//用原始的字节流技术来读取文件
public static void test() throws Exception{
File f= new File("D:\\aa.txt");
String s=f.getName();
FileInputStream in=new FileInputStream(new File("D:\\aa.txt"));
FileOutputStream out=new FileOutputStream(new File("E:\\"+s));
byte b[]=new byte[1024];
int num=0;
while((num=in.read(b))!=-1){
out.write(b, 0, num);
}
out.close();
in.close(); }
//用缓冲流技术来读取文件
public static void test2() throws Exception{
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:\\aa.txt")))); //读一行
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out)); //换行
String line=null;
while((line=br.readLine())!=null){
bw.write(line); //读取一行,换行
bw.newLine();
}
bw.close();
br.close();
} java序列化对象
//谈到序列化:这涉及到文件网络传输,一般为了安全,都会将文件序列化,达到客户端时,再反序列化。进行解密。
序列化之前,首先对象都要实现Serializable接口。
public class StuManage implements Serializable {
private ArrayList<Student> a;
public StuManage(){
this.a=new ArrayList<Student>();
}
public void addStu(Student s){
a.add(s);
}
public void showStu(){
for(Student s:a){
System.out.println(s);
}
}
}
/* 序列化 , 保存*/
public static void main(String[] args)throws IOException {
StuManage sm=new StuManage();
sm.addStu(new Student("晓晓","boy",98));
sm.addStu(new Student("莉莉","boy",88));
sm.addStu(new Student("张聊","girl",76));
sm.addStu(new Student("肖凌","girl",99));
FileOutputStream fos=new FileOutputStream(new File("E:\\xlh2.bin"));
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(sm);
oos.close();
fos.close();
System.out.println("序列化完成!");
} /* 反序列化的过程 读*/
public static void main(String[] args) throws IOException,ClassNotFoundException{
FileInputStream fis=new FileInputStream(new File("E:\\xlh2.bin"));
ObjectInputStream ois =new ObjectInputStream(fis);
StuManage o= (StuManage)ois.readObject();
o.showStu();
fis.close();
ois.close();
}
- IO流读写文件
- IO 流读写文件
- IO流文件读写
- Io流读写文件
- java io流读写文件
- java IO流读写文件
- IO流的文件读写
- 使用IO流 读写文件
- IO流之读写文件
- 文件的读写,io流
- io流读写文件(文件内容修改)
- java io 流,批量读写文件
- JAVA IO流 创建文件 读写字符串
- Java IO流读写文件
- java IO流文件的读写
- IO流之文件读写操作
- java IO流分析,java文件读写
- java IO 流 读写文件 以及复制
- IOS Block循环引用的内存泄漏问题
- Radhat 自动挂载window共享
- 计算当前时间到星期N之间的时间差
- Java 二维数组
- 黑马程序员----JAVASE高级部分之JavaBean与内省
- IO流读写文件
- Activiti5完整版视频教程下载
- Debug清除BIOS密码的命令
- Web应用中session的实现机制
- 解决IE的url过长问题
- Adobe AIR for Android 中的GPU 渲染 [译]
- jena 简单查询(不带推理,直接读取owl文件)
- 2013.11.28 Base64编码后的图片信息在JSP页面中的显示[JFinal框架项目]
- TCP/IP Network Layer Protocols