io流

概念
  • 设备之间的数据传输

  • 对于程序来说,把数据传输到其他节点,成为输出,反之输入

  • 分类

    • 输出流
      • 字节,实际存储最小单位(8 bit)
      • 字符,utf编码,两个字节为单位(16 bit)
    • 输入流,输出流
      • 同上
  • 读文件步骤 (把大象拿出冰箱需要几步)

    • 打开文件,创建流对象,建立通道
    • 读取文件,通过管道处理数据
    • 关闭输入流,关闭管道
  • 写文件 (把大象关进冰箱)

    • 打开文件
    • 写入
    • 关闭流
基本流
基本流字节流字符流
输入流InputStream(抽象基类)Reader(抽象基类)
输出流OutputStrenam(抽象基类)Writer(抽象基类)
文件流
文件流字节流字符流
输入流FileInputStreamFileReader
输出流FileOutputStrenamFileWriter
  • FileReader
    • 父类:InputStreamReader
    • 方法(父类)
      • void close() 关闭流
        • Closes the stream and releases any system resources associated with it.
      • String getEncoding() ???
        • Returns the name of the character encoding being used by this stream.
      • int read() 读取一个字节,并返回int
        • Reads a single character.
      • int read(char[] cbuf, int offset, int length) 读取到指定的字符串中,指定开始下表和长度
        • Reads characters into a portion of an array.
      • boolean ready() 是否可读
        • Tells whether this stream is ready to be read.
  • FilteWriter
    • 父类:OutputStreamWriter
    • 方法(父类)
      • void close() 关闭流
        • Closes the stream and releases any system resources associated with it.
      • String getEncoding() **
        • Returns the name of the character encoding being used by this stream.
      • int read() 读一个char转int返回
        • Reads a single character.
      • int read(char[] cbuf, int offset, int length)给指定的字符数组里面读一些字符串,从指定下标,读取指定长度,返回读取了多少个字符
        • Reads characters into a portion of an array.
      • boolean ready() 是否可读
        • Tells whether this stream is ready to be read.
public void copyjava() {
	FileWriter fileWriter = null;
	FileReader fileReader = null;
	
	try {
		fileReader = new FileReader("file");
		fileWriter = new FileWriter("file.bak");
		char [] buff = new char[100];
		int realCount = fileReader.read(buff);
		while (realCount != -1) {
			fileWriter.write(buff, 0, realCount);
			realCount = fileReader.read(buff);
		}
		
	} catch (Exception e) {
		System.out.println(e.getMessage());
	}finally {
		if(fileReader!= null) {
			try {
				fileReader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if(fileWriter != null){
			try {
				fileWriter.close();
			} catch (Exception e2) {
			}
		}
		
	}
}
缓冲流
处理流字节流字符流
输入流BufferedInputStreamBufferedReader
输出流BufferedOutputStrenamBufferedWriter
  • 包装字符(字节流)流,包装类
  • 包装方式,关联
  • fileReader -> BufferedReader
  • fileWriter -> BufferedWriter
  • 可以多层嵌套
对象流
  • ObjectInputStream
  • ObjectutputStrenam
@Test
public void test5() {
	FileOutputStream fos = null;
	BufferedOutputStream bos = null;
	ObjectOutputStream oos = null; // 它是字节流中的辈份最高
	
	try {
		fos = new FileOutputStream("二进制文件");
		bos = new BufferedOutputStream(fos);
		oos = new ObjectOutputStream(bos);
		
		oos.writeInt(15);
		oos.writeBoolean(true);
		oos.writeBoolean(false);
		oos.writeLong(20);
		oos.writeDouble(3.14);
		
		oos.writeUTF("abc我和你xxx好吗"); // 把字符串以UTF8编码方式写入文件
		
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (oos != null) {
			try {
				oos.close();
			} catch (Exception e2) {
			}
		}
	}
}
对象流
  • ObjectInputSteam

  • OjbectOutputSteam

  • 序列化,用OOS类将一个java对象写入io流中,只传输gc的数据

  • 反序列化,用OIS类从io流中恢复该java对象

  • 要求

    • 要求实现序列化和反序列化的对象必须已经实现了Serializable接口
    • 读写顺序一致
  • 方法,

    • writeXX或writeObject
    • readXX或readObject
  • RMI(远程方法调用)基础

  • Serializable接口有即可,做标志使用,表明这个类的对象可以被序列化

  • 静态属性不在gc区中,无法序列化

  • 不想被序列化:可以用关键词:transient 修饰

  • 可以序列化数组,反序列化为数组,但要求数组中的元素,必须可以序列化

    • 当作对象序列化
  • 可以序列化集合,反序列化为集合,要求元素可以序列化

  • 刷新

    • bufWriter.flush();//把缓冲区的数据刷到磁盘
  • 写入追加,

    • new 流的时候进行添加第二参数true,改变重写为追加
转换流
  • InputStreamReader
    • 字节流转字符流
    • 可以解码
  • OutputStreamWriter
    • 字符转字节流
    • 可以编码
//读取UTF8编码的文件
@Test
public void testName2() {
	FileInputStream fileInputStream = null;
	InputStreamReader isr = null;
	BufferedReader bufferedReader = null;
	try {
		fileInputStream = new FileInputStream("编码测试2.txt");
		isr = new InputStreamReader(fileInputStream, "utf8"); // 第2个参数是告诉转换流, 我的文本文件中是UTF8编码方式的内容
		bufferedReader = new BufferedReader(isr);
		
		String readLine = bufferedReader.readLine();
		while (readLine != null) {
			// 处理数据
			System.out.println(readLine);
			// 继续读
			readLine = bufferedReader.readLine();
		}
		
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (bufferedReader != null) {
			try {
				bufferedReader.close();
			} catch (Exception e2) {
			}
		}
	}
}
// 写一个UTF8编码方式的文件
@Test
public void testName3() {
	FileOutputStream fos = null;
	OutputStreamWriter osw = null;
	BufferedWriter bufWriter = null;
	try {
		fos = new FileOutputStream("写一个utf8文本文件.txt"); 
		osw = new OutputStreamWriter(fos, "utf8");
		bufWriter = new BufferedWriter(osw);
		
		bufWriter.write("abc我喜欢你1234567");
		bufWriter.newLine();
		
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (bufWriter != null) {
			try {
				bufWriter.close();
			} catch (Exception e2) {
			}
		}
	}
}

String类自转码
  • Byte[] getBytes(String str = “gbk”) //编码
  • String String(byte b, String = “utf8”)//解码
String string21 = new String(bytes1, "gbk");//缺省取操作系统编码方式
byte[] bytes1 = string.getBytes("gbk");     //缺省取操作系统编码方式,中国一般为gbk
System中的io
  • System.in 标准流,
  • System.in 属于 InputStream
  • System.out.println() //标准的输出流
  • System.err.println() //颜色不同,它的执行是在另一个线程里的
Scanner类
  • Scanner 类,扫描器
    • 扫描器各种空间
    • boolean hasNext() //有没有下一个
    • String next() //获取下一个
      • 会用空格来分割字符
    • String nextLine() //获取一行
    • int nextInt() //获取整数
    • boolean hasInt() //是否有下一个整数
    • 等支持任意类型
打印流
处理流
数据流
File类
  • 代表文件,和目录
  • 主要方法
    • boolean mkdir() //创建单层目录
    • boolean mkdirs() //创建多层目录
    • File listFiles() //列出目录下的所有的文件
    • long length() //长度
    • boolean delete() //删除
    • boolean isDirectory() //是目录
    • boolean isFile() //是文件
    • String getName() //文件名或目录名
io流体系
分类字节输入流字节输出流字符输入流字符输入流
抽象基类InputStreamOutputStreamReaderWtiter
访问文件FileInputStreamFileOutputStreamFileReaderFileWtiter
访问数组ByteArrayInputStreamByteArrayOutputStreamCharArrayReaderCharArrayWtiter
访问管道PipedInputStreamPipedOutputStreamPipedReaderPipedWriter
访问字符串ReaderWriter
缓冲流BufferedInputStreamBufferedOutputStreamBufferedReaderBufferedWriter
转换流InputStreamReaderOutputStreamWriter
对象流ObjectInputStreamObjectOutputStream
?FilterInputStreamFilterOutputStreamFilterReaderFilterWriter
打印流PrintStreamPrintWriter
推回输入流PushbackInputStreamPushbackReader
特殊流InputStreamOutputStream

public class HomeWork {
	
	public void copyDir(File src, File dest) {
		dest.mkdir(); // 先创建目录目录
		File[] childFiles = src.listFiles();
		for (File file : childFiles) {
			File file2 = new File(dest, file.getName());
			if (file.isFile()) {
				// 复制文件
				copyFile(file, file2);
			} else if (file.isDirectory()) {
				copyDir(file, file2);
			}
		}
	}
	
	public void copyFile(File src, File dest) {
		System.out.println("正在复制文件" + src + "到" + dest);
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream(src);
			fos = new FileOutputStream(dest);
			byte[] buf = new byte[8192];
			int realCount = fis.read(buf);
			while (realCount != -1) {
				// 1) 处理已经读到的数据
				fos.write(buf, 0, realCount);
				// 2) 继续读
				realCount = fis.read(buf);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (Exception e2) {
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (Exception e2) {
				}
			}
		}
	}

	@Test
	public void work1() {
		//复制D:/MyWork 到C:/MyWork
		File src = new File("d:/MyWork");
		File dest = new File("c:/MyWork");
		copyDir(src, dest);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值