Java输入输出流简单案例
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输入输出流简单案例的更多相关文章
- Java输入/输出流体系
在用java的io流读写文件时,总是被它的各种流能得很混乱,有40多个类,理清啦,过一段时间又混乱啦,决定整理一下!以防再忘 Java输入/输出流体系 1.字节流和字符流 字节流:按字节读取.字符流: ...
- 深入理解Java输入输出流
Java.io包的File类,File类用于目录和文件的创建.删除.遍历等操作,但不能用于文件的读写. Java 对文件的写入和读取涉及到流的概念,写入为输出流,读取为输入流.如何理解流的概念呢?可以 ...
- Java 输入输出流 转载
转载自:http://blog.csdn.net/hguisu/article/details/7418161 1.什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作. Java所 ...
- java输入输出流总结 转载
一.基本概念 1.1 什么是IO? IO(Input/Output)是计算机输入/输出的接口.Java中I/O操作主要是指使用Java进行输入,输出操作. Java所有的I/O机制都是 ...
- Java输入输出流(转载)
转自http://blog.csdn.net/hguisu/article/details/7418161 目录(?)[+] 1.什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作 ...
- 【java开发系列】—— java输入输出流
前言 任何语言输入输出流都是很重要的部分,比如从一个文件读入内容,进行分析,或者输出到另一个文件等等,都需要文件流的操作.这里简单介绍下reader,wirter,inputstream,output ...
- Java 输入输出流 (七)
1.什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作. Java所有的I/O机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流动序列.Java的I/O流提供了读 ...
- Java基础学习总结(47)——JAVA输入输出流再回忆
一.什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作. Java所有的I/O机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流动序列. Java的I/O流提供了 ...
- java输入/输出流的基本知识
通过流可以读写文件,流是一组有序列的数据序列,以先进先出方式发送信息的通道. 输入/输出流抽象类有两种:InputStream/OutputStream字节输入流和Reader/Writer字符输入流 ...
随机推荐
- bzoj3312
K个硬币,要买N个物品. 给定买的顺序,即按顺序必须是一路买过去,当选定买的东西物品序列后,付出钱后,货主是不会找零钱的.现希望买完所需要的东西后,留下的钱越多越好,如果不能完成购买任务,输出-1 $ ...
- java面试题09
A卷 1.选择题 public class Test01 { public static void changeStr(String str) { str = "welcome"; ...
- 【Lintcode】028.Search a 2D Matrix
题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...
- finetune
微调的具体方法和技巧有很多种,这里总结了在不同场景下的微调技巧: 1)新数据集比较小且和原数据集相似.因为新数据集比较小(比如<5000),如果fine-tune可能会过拟合:又因为新旧数据集类 ...
- linux——boot空间不足
1. 先用df命令,查看磁盘分区情况 2. dpkg --get-selections|grep linux-image(查看更新了多少内核) root@ubuntu:/home/hadoop# dp ...
- appium+python 快速给真机安装app
#coding=utf-8from appium import webdriverfrom time import sleepimport os,time,unittest '''给手机快速装app的 ...
- Java 打包成exe安装包
1.在eclipse中导出Runnable JAR file 2.选择主函数所在的类和输出位置后finish: 3.这里选择的打包工具是exe4j,在网上找序列号注册一下,否则在打完后在exe运行时, ...
- Redux API之Store
Store Store 就是用来维持应用所有的 state 树 的一个对象. 改变 store 内 state 的惟一途径是对它 dispatch 一个action. Store 不是类.它只是有几个 ...
- 4. docker镜像的概念、管理(查看、下载、删除)
镜像的概念 镜像是一个包含程序运行必要依赖环境和代码的只读文件,它采用分层的文件系统,将每一次改变以读写层的形式增加到原来的只读文件上.镜像是容器运行的基石. 下图展示的是Docker镜像的系统结构. ...
- java中多个线程访问共享数据的方式有哪些
多个线程对共同数据的访问的实现,要根据情况而定 (1)当访问共同的代码的时候:可以使用同一个Runnable对象,这个Runnable对象中有这个共享数据,比如卖票系统就可以这么做.或者这个共享数据封 ...