黑马程序员技术交流社区
标题:
IO流预习总结
[打印本页]
作者:
jekyll
时间:
2015-9-27 00:38
标题:
IO流预习总结
package com.cnstrong.study;
import java.io.*;
public class T01 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
/*
1. 流的分类
按照方向分:输入流,输出流
以程序运行起来那一段内存而言,他的流向。
按照传输单位分:字节流(1字节),字符流(2字节)
按照目的地分:文件流,网络流,内存流等等
按照处理能力分:节点流,处理流(包装在另外一个流之上,提供了更强大的对流的处理功能的流)
2. IO的框架
2.1 4个顶层抽象类
输入 输出
字节 InputStream OutputStream
字符 Reader Writer
2.2 InputStream | ByteArrayInputStream ==> 节点流
FileInputStream ==> 节点流,文件流
ObjectInputStream ==> 处理流( 传输对象 )
BufferedInputStream ==> 处理流
DataInputStream ==> 处理流,数据输入流允许应用程序以与
机器无关方式从底层输入流中读取基本 Java 数据类型。
3. available():预估字节数,一般情况下读取本地文件都比较准确,网络之间不太准确,因为有可能网络阻塞。
4. 练习写文件("gahsgdfgadf563534653")
5. Reader | CharArrayReader
FileReader
BufferedReader
InputStreamReader:字节流通向字符流的桥梁
Writer | .....
OutputStreamWriter:字符流通向字节流的桥梁
作业题目1:遍历某一个目录下面的所有文件和目录(明天交)
作业题目2:将C盘下的一个图片(1.jpg)Copy到D盘下(明天交)
作业题目3:实现学生管理系统( 学生新增【 编号,姓名,年龄 】,删除,查询(按照姓名进行模糊查询) )(端午节后交)
实现文件存储,存储的是ArrayList
* */
testFile2();
}
public static void testFile2()
{
File f = new File("C:\\");
String[] str1 = f.list();
for (String string : str1) {
System.out.println(string);
}
}
public static void testFile1()
{
File f = new File("C:\\11.txt");
boolean bln = f.exists();
System.out.println(bln);
bln = f.isDirectory();
System.out.println(bln);
}
public static void testAppendFile()throws IOException
{
//第二个参数代表追加
Writer w = new FileWriter("C:\\11.txt" ,true);
BufferedWriter bw = new BufferedWriter(w);
bw.newLine();
bw.write("我是朱元璋!");
bw.newLine();
bw.write("我是鲁智深!");
bw.flush();
w.flush();
bw.close();
w.close();
}
//从字节流到字符流
public static void testByteToChar()throws IOException
{
InputStream is = new FileInputStream("C:\\11.txt");
//将字节流处理成字符流
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String str = null;
while( (str = br.readLine()) != null )
{
System.out.println(str);
}
br.close();
isr.close();
is.close();
}
public static void testReader1()throws IOException
{
Reader r = new FileReader("C:\\11.txt");
BufferedReader br = new BufferedReader(r);
String str = null;
while( (str = br.readLine()) != null )
{
System.out.println(str);
}
br.close();
r.close();
}
public static void testWriter1()throws IOException
{
Writer w = new FileWriter("C:\\11.txt");
BufferedWriter bw = new BufferedWriter(w);
bw.write("我是张三丰!");
bw.newLine();
bw.write("我是杨广!");
bw.flush();
w.flush();
bw.close();
w.close();
}
public static void testDataInputStream1()throws IOException
{
InputStream is = new FileInputStream("C:\\11.txt");
DataInputStream dis = new DataInputStream(is);
int i = dis.readInt();
double dbl = dis.readDouble();
dis.close();
is.close();
System.out.println(i + " " + dbl);
}
public static void testDataOutputStream1() throws IOException
{
OutputStream os = new FileOutputStream("C:\\11.txt");
DataOutputStream dos = new DataOutputStream(os);
dos.writeInt(1);
dos.writeDouble(2.3);
dos.flush();
os.flush();
dos.close();
os.close();
}
public static void testObjectInputStream1()throws IOException, ClassNotFoundException
{
InputStream is = new FileInputStream("C:\\11.txt");
ObjectInputStream ois = new ObjectInputStream(is);
/*
Student stu1 = (Student)ois.readObject();
System.out.println(stu1.id + " " + stu1.name);
Student stu2 = (Student)ois.readObject();
System.out.println(stu2.id + " " + stu2.name);
*/
Object object = null;
while( (object = ois.readObject())!=null )
{
Student stu = (Student)object;
System.out.println( stu.id + " " + stu.name );
}
ois.close();
is.close();
}
public static void testObjectOutputStream1()throws IOException
{
Student student = new Student();
student.id = 1;
student.name = "刘德华";
OutputStream os = new FileOutputStream("C:\\11.txt");
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(student);
student = new Student();
student.id = 2;
student.name = "马德华";
oos.writeObject(student);
oos.flush();
os.flush();
oos.close();
os.close();
}
public static void testFileInputStream1() throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = new FileInputStream("C:\\11.txt");
byte[] bytes = new byte[8];
int length = -1;
while( (length = is.read(bytes)) != -1 )
{
baos.write(bytes, 0, length);
}
is.close();
String str = new String(baos.toByteArray());
System.out.println(str);
}
public static void testFileInputStream2() throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = new FileInputStream("C:\\11.txt");
BufferedInputStream bis = new BufferedInputStream(is);
byte[] bytes = new byte[8];
int length = -1;
while( (length = bis.read(bytes)) != -1 )
{
baos.write(bytes, 0, length);
}
bis.close();
is.close();
String str = new String(baos.toByteArray());
System.out.println(str);
}
}
class Student implements Serializable
{
public int id;
public String name ;
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2