java常用IO流集合用法模板
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流集合用法模板的更多相关文章
- Java 常用IO流操作详解
1.基本概念 IO:Java对数据的操作是通过流的方式,IO流用来处理设备之间的数据传输,上传文件和下载文件,Java用于操作流的对象都在IO包中. 2.IO流的分类 图示:(主要IO流) 3.字节流 ...
- java常用IO流数据流小结
类名 常用方法 说明 输入流 InputStream int read(); 只能读字节流,虽然返回值是int,但只有低8位起作用. DataInputStream Type readType() ...
- java常用IO流总结
- Java 的 IO 流
接着上一篇的 “Java 的 File 类” 的随笔,在File类的基础上,我们就走进Java的IO流吧. 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在 ...
- Java基础知识强化之IO流笔记68:Properties和IO流集合使用
1. Properties和IO流集合使用 这里的集合必须是Properties集合: public void load(Reader reader):把文件中的数据读取到集合中 public v ...
- java基础-IO流对象之Properties集合
java基础-IO流对象之Properties集合 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Properties集合的特点 Properties类表示了一个持久的属性集. ...
- Java之IO流用法总结
Java的IO流概述:1.I/O是Input/Output的缩写,I/O技术是非常实用的技术,用于处理设备之间的数据传输.如读/写文件,网络通讯等.2.Java程序中,对于数据的输入/输出操作以“流( ...
- 【Java】IO流简单分辨
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/5827509.html Java的IO流体系十分庞大,并且体系层次稍复杂,很容易记混或记错.在此,我把平时经常用 ...
- JAVA中IO流总结
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42119261 我想你对JAVA的IO流有所了解,平时使用的 ...
随机推荐
- ChatGirl is an AI ChatBot based on TensorFlow Seq2Seq Model
Introduction [Under developing,it is not working well yet.But you can just train,and run it.] ChatGi ...
- 【js-xlsx和file-saver插件】前端导出数据到excel
最近在做项目,前端进行处理数据,导出excel中,还是遇到不少问题,这里将其进行总结一下,博主是vue框架开发,借用file-saver和xlsx插件进行导出excel,我们来看下代码和效果.地址链接 ...
- 一个页面从输入url到页面加载显示完成,中间都经历了什么
第一种解释: 一般会经历以下几个过程: 1.首先,在浏览器地址栏中输入url 2.浏览器先查看浏览器缓存-系统缓存-路由器缓存,如果缓存中有,会直接在屏幕中显示页面内容.若没有,则跳到第三步操作. 3 ...
- 移动端web开发中对点透的处理,以及理解fastclick如何做到去除300ms延迟
一.点透问题以及处理办法 开发中遇到一个问题,就是点击layer弹出框的取消按钮之后,按钮下方的click事件就直接触发了.直接看代码: $('.swiper-slide').on('click', ...
- find 命令查找文件,文件夹
查找文件 find / -name httpd.conf 查找文件夹 find / -name "*1526*" -type d, 其中双引号里的东西表示文件夹名字包含" ...
- Promise--优雅的异步回调解决方案
当一个接口需要依赖另一个接口的请求数据时,通常有两种解决方式,一个是将请求数据的接口设为同步,之后调另一个接口,另一个是在请求数据接口的成功回调里调另一个接口. 但是:当一个接口需要依赖很多个接口的请 ...
- Dynamics CRM2016 Web Api之查询查找字段的相关属性
之前有篇博文介绍了如何获取查找字段的name值(跳转),本篇在此基础上再延伸下,实现的效果类似于EntityReference,可以取到查找字段的id,name,localname. 这里我以客户实体 ...
- 初识mybatis(二)
上篇博客我们介绍通过Java代码来创建mybatis的配置文件,港真,这种方式看起来有意思实际在开发中用的并不多,mybatis的配置还是以xml配置为主,本文我们就来看看如何通过xml文件来配置my ...
- Swift:Foundation框架中的NS前缀的由来
可能大家对于著名的NS前缀的由来有一些疑问. 绝大多数这些NS前缀的类是NeXTSTEP操作系统中Foundation框架里的一部分,而该操作系统是OS X的基础. NeXTSTEP的程序员对它们的类 ...
- Scikit-learn:模型评估Model evaluation 之绘图
http://blog.csdn.net/pipisorry/article/details/53001866 绘制ROC曲线 def plotRUC(yt, ys, title=None): ''' ...