package com.fmy;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.CharArrayReader;
import java.io.CharArrayWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Serializable;
import java.io.Writer; public class Demo1 { static File file1 =new File("D:/info1.txt");
static File file2 =new File("D:/info2.txt"); /**
* 使用字节流复制文本
*/
public static void demo1(){
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(file1);
os = new FileOutputStream(file2);
byte []buf =new byte[1024];
int len;
while ((len=is.read(buf))!=-1) {
System.out.println(new String(buf,0,len));
os.write(buf, 0, len);
} } catch (Exception e) {
e.printStackTrace();
}finally{
try {
is.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 使用字符流赋值文本
*/
public static void demo2() {
Writer w=null;
Reader r=null;
try { w = new FileWriter(file2);
r = new FileReader(file1); char buf[] = new char[512];
int len;
while ((len=r.read(buf))!=-1) {
w.write(buf);
} } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
w.close();
r.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
/**
* 使用字节流赋值文本 ---使用包装流
*/
public static void demo3() {
InputStream is=null;
OutputStream os=null;
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try {
is = new FileInputStream(file1);
os = new FileOutputStream(file2);
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(os); byte[] buf =new byte[1024];
int len;
while ((len=bis.read(buf))!=-1) {
bos.write(buf,0,len);
} } catch (Exception e) {
e.printStackTrace();
}finally{
try {
bos.close();
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} /**
* 使用字符流赋值文本-使用包装流
*/
public static void demo4() {
Writer w = null;
Reader r = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
r = new FileReader(file1);
w = new FileWriter(file2);
br = new BufferedReader(r);
bw = new BufferedWriter(w);
String buf="";
while ((buf=br.readLine())!=null) {
bw.write(buf);
bw.newLine();
} } catch (Exception e) {
e.printStackTrace();
}finally{
try {
bw.close();
br.close();
} catch (Exception e2) {
e2.printStackTrace();
}
} } /**
* 字节流复制文本 ---运用转化流
*/
public static void demo5(){
BufferedWriter bw = null;
BufferedReader br = null;
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(file1);
os = new FileOutputStream(file2);
bw = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
String buf="";
while ((buf=br.readLine())!=null) {
bw.write(buf);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
bw.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
} } /**
* 使用printStream 输出一个文本
*/
public static void demo6(){ OutputStream os = null;
PrintStream ps = null;
try {
os = new FileOutputStream(file2);
ps = new PrintStream(os);
ps.println(true);
ps.println("你好漂亮");
ps.print('我');
ps.print('呸');
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
ps.close();
} } /**
* 使用printWriter输出一个文本
*/
public static void demo7(){ Writer w = null;
PrintWriter pw = null; try {
w = new FileWriter(file2);
pw = new PrintWriter(w);
pw.println(98);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
pw.close();
}
} /**
* 使用 CharArrayReader 和 CharArraysWriter 复制文本
*/
public static void demo8() {
Writer w = null;
Reader r = null;
CharArrayReader car = null;
CharArrayWriter caw = null;
try {
w = new FileWriter(file2);
r = new FileReader(file1);
char [] buf=new char[512];
caw = new CharArrayWriter();
int len;
while ((len=r.read(buf))!=-1){
caw.write(buf, 0, len);
}
car = new CharArrayReader(caw.toCharArray()); while ((len=car.read(buf))!=-1) {
w.write(buf, 0, len);
} } catch (Exception e) {
e.printStackTrace();
}finally{
if (car!=null) {
car.close();
}
if (caw!=null) {
caw.close();
}
try {
if (w!=null) {
w.close();
}
if (r!=null) {
r.close();
}
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
} } /**
* 使用ByteArrayInputStream和 ByteArrayOutputStream
*/
public static void demo9() { InputStream is = null;
OutputStream os = null;
ByteArrayInputStream bais = null;
ByteArrayOutputStream baos = null;
try {
is = new FileInputStream(file1);
os = new FileOutputStream(file2);
baos = new ByteArrayOutputStream();
byte [] buf = new byte [1024];
int len; while ((len=is.read(buf))!=-1) {
baos.write(buf,0,len);
}
bais =new ByteArrayInputStream(baos.toByteArray());
while ((len=bais.read(buf))!=-1) {
os.write(buf, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
bais.close();
baos.close();
os.close();
is.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
} /**
* DataInputStream DataOutputStream使用
*/
public static void demo10(){
DataInputStream dis = null;
DataOutputStream dos = null ;
InputStream is = null;
OutputStream os = null; try {
is = new FileInputStream(file2);
os = new FileOutputStream(file2);
dis = new DataInputStream(is);
dos = new DataOutputStream(os); dos.writeUTF("我喜欢你很久了");
dos.writeFloat(13); dos.close();
System.out.println(dis.readUTF());
System.out.println(dis.readFloat());
dis.close();
} catch (Exception e) {
e.printStackTrace();
} } /**
* 使用ObjectInputSteam和ObjectOutStream
*/
public static void demo11() { InputStream is = null;
OutputStream os = null;
ObjectInputStream ois = null;
ObjectOutputStream oos = null; try {
is = new FileInputStream(file1);
os = new FileOutputStream(file1);
oos = new ObjectOutputStream(os);
ois = new ObjectInputStream(is); oos.writeObject(new Studen(13,"嘿嘿",124125,123));
oos.close(); Studen s = (Studen)ois.readObject();
System.out.println(s.toString());
ois.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} } public static void main(String[] args){
demo11();
} }
class Studen implements Serializable{
/**
*
*/
private static final long serialVersionUID = 8485022571103676109L; private transient int age = 1;//序列化时只会保存age的默认值0 如果是String则为null 其他略.... static private String name="张三"; //序列化时只会保存静态赋值的数值。不管后期对象如何new 序列化存储为张三
/*这个测试成功,是因为都在同一个机器(而且是同一个进程),
* 因为这个jvm已经把name加载进来了,所以获取的是加载好的
* name,如果你是传到另一台机器或者你关掉程序
* 重写写个程序读入Studen.obj,此时因为别的机器或新
* 的进程是重新加载name的,所以name信息就是初始时的信息。
* */
private int salary=100;//实验表明可以序列化
private int weight;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Studen [age=" + age + ", name=" + name + ", salary=" + salary
+ ", weight=" + weight + "]";
}
public Studen(int age, String name, int salary, int weight) {
super();
this.age = age;
this.name = name;
this.salary = salary;
this.weight = weight;
} }

java常用IO流集合用法模板的更多相关文章

  1. Java 常用IO流操作详解

    1.基本概念 IO:Java对数据的操作是通过流的方式,IO流用来处理设备之间的数据传输,上传文件和下载文件,Java用于操作流的对象都在IO包中. 2.IO流的分类 图示:(主要IO流) 3.字节流 ...

  2. java常用IO流数据流小结

      类名 常用方法 说明 输入流 InputStream int read(); 只能读字节流,虽然返回值是int,但只有低8位起作用. DataInputStream Type readType() ...

  3. java常用IO流总结

  4. Java 的 IO 流

    接着上一篇的 “Java 的 File 类” 的随笔,在File类的基础上,我们就走进Java的IO流吧. 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在 ...

  5. Java基础知识强化之IO流笔记68:Properties和IO流集合使用

    1. Properties和IO流集合使用 这里的集合必须是Properties集合:  public void load(Reader reader):把文件中的数据读取到集合中  public v ...

  6. java基础-IO流对象之Properties集合

    java基础-IO流对象之Properties集合 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Properties集合的特点 Properties类表示了一个持久的属性集. ...

  7. Java之IO流用法总结

    Java的IO流概述:1.I/O是Input/Output的缩写,I/O技术是非常实用的技术,用于处理设备之间的数据传输.如读/写文件,网络通讯等.2.Java程序中,对于数据的输入/输出操作以“流( ...

  8. 【Java】IO流简单分辨

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/5827509.html Java的IO流体系十分庞大,并且体系层次稍复杂,很容易记混或记错.在此,我把平时经常用 ...

  9. JAVA中IO流总结

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42119261 我想你对JAVA的IO流有所了解,平时使用的 ...

随机推荐

  1. 开发者说 | 使用Visual Studio Code编译、调试Apollo项目

    转载地址:https://mp.weixin.qq.com/s?__biz=MzI1NjkxOTMyNQ==&mid=2247484266&idx=1&sn=d6bcd4842 ...

  2. Spring学习笔记2——创建Product对象,并在其中注入一个Category对象

    第一步:创建Product类.在Product类中有对Category对象的set和get方法 package com.spring.cate; public class Product { priv ...

  3. Windows Server 2008 R2服务器系统安全设置参考指南

    Server 2008 R2服务器系统安全设置参考指南  重点比较重要的几部 1.更改默认administrator用户名,复杂密码 2.开启防火墙 3.安装杀毒软件 1)新做系统一定要先打上补丁(升 ...

  4. 【SpringMVC】<context:include-filter>和<context:exclude-filter>使用时要注意的地方

    http://jinnianshilongnian.iteye.com/blog/1762632 http://blog.51cto.com/wenshengzhu/1700340 http://ww ...

  5. servlet的web-xml配置详解

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...

  6. Vue生命周期-手动挂载理解

    改前端遇到个bug,console能够输出值,但是前端不能显示. 我简直一脸懵逼,vue的问题?网络的问题?浏览器的缓存问题? 公司网络,所以直接排除网络问题. 浏览器缓存,试了下确实一定概率可以显示 ...

  7. Tomcat 报错的解决方法:The APR based Apache Tomcat Native library which allows optimal

    下载 http://tomcat.heanet.ie/native/1.1.12/binaries/win32/tcnative-1.dll将这个文件复制到C:\WINDOWS\system32\,. ...

  8. Struts+Hibernate+jsp页面,实现分页

    dao层代码 package com.hanqi.dao; import java.util.ArrayList; import java.util.List; import org.hibernat ...

  9. LeeCode

    No1. Given an array of integers, return indices of the two numbers such that they add up to a specif ...

  10. 轻松理解AOP问题

    先说一个Spring是什么吧,大家都是它是一个框架,但框架这个词对新手有点抽象,以致于越解释越模糊,不过它确实是个框架的,但那是从功能的角度来定义的,从本质意义上来讲,Spring是一个库,一个Jav ...