package com.jckb;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer; public class Test {
public static void main(String[] args) {
// m();
// inputStreamDemo();
// outputStreamDemo();
m8();
} // 序列输入流
static void m8() {
InputStream is = null;
ObjectInputStream ois = null;
File file = null;
try {
String pathname = "D://file6.txt";
file = new File(pathname);
is = new FileInputStream(file);
ois = new ObjectInputStream(is);
Object obj=ois.readObject();
if(obj instanceof Student){
Student stu =(Student)obj;
System.out.println(stu.toString());
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) { e.printStackTrace();
}
} // 序列输出流
static void m7() {
OutputStream out = null;
ObjectOutputStream os = null;
try {
out = new FileOutputStream("D://file6.txt");
os = new ObjectOutputStream(out);
Student stu = new Student("张三", 20);
os.writeObject(stu);
System.out.println("序列号成功!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
os.flush();
os.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} // 带缓冲的字符输出流
static void m6() {
Writer w = null;
BufferedWriter bw = null;
try {
w = new FileWriter("D://file5.txt");
bw = new BufferedWriter(w);
String str = "再见2016,你好2017!";
bw.write(str);
System.out.println("写入成功!");
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭流
try {
if (bw != null) {
bw.flush();
bw.close();
}
if (w != null) {
w.close();
}
} catch (IOException e) {
e.printStackTrace();
} } } // 带缓冲的字符输入流
static void m5() {
Reader in = null;
BufferedReader br = null;
try {
in = new FileReader("D://file4.txt");
br = new BufferedReader(in);
String result = null;
while ((result = br.readLine()) != null) {
System.out.println(result);
}
br.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
} // 字符输入流
static void m4() {
FileReader fr = null;
try {
fr = new FileReader("D://file4.txt");
char[] cbuf = new char[1024];
int result = -1;
while ((result = fr.read(cbuf)) != -1) {
String s = new String(cbuf, 0, result);
System.out.println("result=" + result);
System.out.println(s);
}
fr.close();
} catch (Exception e) {
e.printStackTrace();
}
} // 字符输出流
static void m3() {
FileWriter w = null;
try {
w = new FileWriter(new File("D://file4.txt"));
String str = "你好2017!";
w.write(str);
System.out.println("写入成功!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
w.flush();
w.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} // 缓冲流
static void m2() {
InputStream is;
BufferedInputStream bs = null;
try {
is = new FileInputStream("D:\\file3.txt");
bs = new BufferedInputStream(is);
byte[] b = new byte[24]; int result;
while ((result = bs.read(b)) != -1) {
String chunk = new String(b, 0, result);
System.out.println(chunk);
}
bs.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
} static void m() {
System.out.println(File.pathSeparator);
System.out.println(File.pathSeparatorChar);
System.out.println(File.separator);
System.out.println(File.separatorChar);
File file = new File("D:\\file2.doc");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
boolean b = file.exists();
System.out.println(b);
} // 字符流的读入
static void inputStreamDemo() {
String pathname = "D:\\file3.txt";
File file = new File(pathname);
FileInputStream in = null;
try {
in = new FileInputStream(file);
System.out.println("字节数为:" + in.available()); int data = -1;
// read() while((data=in.read())!=-1){
System.out.println((char) data); // 带缓冲区的字符流的读取read(byte[]b)
byte[] b = new byte[in.available()];
while (in.read(b) != -1) {
for (byte item : b) {
System.out.println((char) item);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} // 字符流的写出
static void outputStreamDemo() {
String pathname = "D:\\file.txt";
FileOutputStream os = null;
try {
os = new FileOutputStream(new File(pathname), true);
byte[] b = { 'a', 'b', 'c', 'v' };
os.write(b);
System.out.println("写入成功!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.jckb;

import java.io.Serializable;

/**学生类包含姓名、年龄
* 如果那个属性不需要序列号,要在前面加上关键字 transient
* @author gx
*
*/ public class Student implements Serializable{ private static final long serialVersionUID = 1L;
private String name;//姓名
private int age ;//年龄 public Student(String name, int age) {
super();
this.name = name;
this.age = age;
} public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} @Override
public String toString() {
return name+"---"+age;
} }

 

Java输入输出流简单案例的更多相关文章

  1. Java输入/输出流体系

    在用java的io流读写文件时,总是被它的各种流能得很混乱,有40多个类,理清啦,过一段时间又混乱啦,决定整理一下!以防再忘 Java输入/输出流体系 1.字节流和字符流 字节流:按字节读取.字符流: ...

  2. 深入理解Java输入输出流

    Java.io包的File类,File类用于目录和文件的创建.删除.遍历等操作,但不能用于文件的读写. Java 对文件的写入和读取涉及到流的概念,写入为输出流,读取为输入流.如何理解流的概念呢?可以 ...

  3. Java 输入输出流 转载

    转载自:http://blog.csdn.net/hguisu/article/details/7418161 1.什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作. Java所 ...

  4. java输入输出流总结 转载

    一.基本概念 1.1 什么是IO?     IO(Input/Output)是计算机输入/输出的接口.Java中I/O操作主要是指使用Java进行输入,输出操作.     Java所有的I/O机制都是 ...

  5. Java输入输出流(转载)

    转自http://blog.csdn.net/hguisu/article/details/7418161 目录(?)[+] 1.什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作 ...

  6. 【java开发系列】—— java输入输出流

    前言 任何语言输入输出流都是很重要的部分,比如从一个文件读入内容,进行分析,或者输出到另一个文件等等,都需要文件流的操作.这里简单介绍下reader,wirter,inputstream,output ...

  7. Java 输入输出流 (七)

    1.什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作. Java所有的I/O机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流动序列.Java的I/O流提供了读 ...

  8. Java基础学习总结(47)——JAVA输入输出流再回忆

    一.什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作. Java所有的I/O机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流动序列. Java的I/O流提供了 ...

  9. java输入/输出流的基本知识

    通过流可以读写文件,流是一组有序列的数据序列,以先进先出方式发送信息的通道. 输入/输出流抽象类有两种:InputStream/OutputStream字节输入流和Reader/Writer字符输入流 ...

随机推荐

  1. HihoCoder1649 : 漏写的数字([Offer收割]编程练习赛38)(模拟题)

    描述 小A今年刚上幼儿园,正在学习写100以内的数字.幼儿园的老师留了一项作业,要求小A从某个100以内的数X开始一直写到另一个100以内的数Y(Y - X > 1). 不过粗心的小A在作业中漏 ...

  2. kettle结合MySQL生成保留最近6个月月度报告_20161009

    之前计算用户ID各月的金额(各月在列字段),用的是下面代码 ,b.金额,,b.金额,,b.金额,NULL)) AS 9月金额 FROM ( SELECT city AS 城市,DATE_FORMAT( ...

  3. nginx 轮询模式 nginx_upstream_jvm_route 插件安装

    使用nginx_upstream_jvm_route插件的目的是为了保证在轮询机制下的session的共享 前提:源码方式安装nginx.patch命令 1.下载nginx_upstream_jvm_ ...

  4. [转] 编写高效的 CSS 选择器

    高效的CSS已经不是一个新的话题了,也不是我一个非得重拾的话题,但它却是我在Sky公司工作之时,所感兴趣的,关注已久的话题. 有很多人都忘记了,或在简单的说没有意识到,CSS在我们手中,既能很高效,也 ...

  5. 2011年浙大:Twin Prime Conjecture

    Twin Prime Conjecture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  6. 0001_第一个测试小程序Login

    # -*- coding:utf-8 -*- user = raw_input("Username:") password = raw_input("Password:& ...

  7. sorted matrix - search & find-k-th

    sorted matrix ( Young Matrix ) search for a given value in the matrix: 1) starting from upper-right ...

  8. ES5.X相关API和技巧汇总

    https://blog.csdn.net/laoyang360/article/details/77412668

  9. solr-建立单机版的服务器

    回到之前打开的页面,刷新,wenda就出来了: 这个wenda是单机版的.

  10. 基于Laravel框架的一个简单易学的微信商城(新手必学)

    俗话说,麻雀虽小可五脏俱全呀! 今天分享的这个基于Laravel的小项目大概功能有这些: 1.实现会员登录.注册功能.数据双向验证功能.2.实现手机短信验证.邮件激活账号.邮件通知.3.ajax提交数 ...