IO是什么意思?
IO是指input和outpu指的是输入和输出
字节流和字符流有什么区别,输入流和输出流有什么区别?
- 字符流和字节流是流的一种划分,按照数据流的处理单位来划分的。两类都分为输入和输出操作。
- 在字节流中输出数据使用outputStream完成,输入数据使用inputStream完成。
- 在字符流中输出数据使用Writer完成,输入数据使用Reader完成。
- 字符流处理的单位为2个字节的unicode字符,分别是操作字符,字符数组和字符串。
- 字节流的处理单位为1个字节,操作的是字节和字节数组。
- 字节流是用来处理2进制数据,但实际中,很多数据是文本,所有提出了字符流的概念,它是按照虚拟机的编码来处理的。
- 也就是2者之间要进行字符集的转化,通过InputStreamReader,OutputStreamWriter来转换,实际上是通过byte[]和String来关联
节点流和处理流有什么区别?
- 节点流和处理流是按照功能的不同划分。
- 节点流可以从或是向一个特定的地方(节点)读写数据。
- 处理流是对一个已存在流的连接和封装,通过所封装流的功能调用实现数据读写。
二者都是字节流的抽象父类。以字节为单位处理数据,每次读取或写入一个字节。适合处理二进制文件。
Reader和Writer的基本特点是?
二者都是字符流的抽象父类。以字符为基本处理单位,每次读取或写入一个字符,适合处理纯文本文件。
一个基本的文件复制程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public static void createAndSave() { File file = new File(path); InputStream stream = new FileInputStream(file); File file1 = new File(path); OutputStream outputStream = new FileOutputStream(file1); int b = 0; while((b=stream.read()) != -1) outputStream.write(b); outputStream.close(); stream.close(); }
|
- 它们是缓冲字节流,分别是缓存字节输入流和缓冲字节输出流。
- 缓冲流是处理流,它不直接连接数据源,而是以一个节点流为参数,在节点流的基础上提供一些操作。
- 不带缓冲的流的处理特点是:读取一个字节,存储一个字节。因而会使得数据的传输较慢。
- 带缓冲流的处理特点是:读取数据,待数据达到了缓冲的大小,便存储数据,重复,直至读取完毕。
- 优点是减少了对硬盘的读取次数,降低了对硬盘的损耗。提高了文件的传输效率。
使用BufferedReader和BufferedWriter完成文件的复制程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| public static void copyUseBuffer() { Reader reader = new FileReader(path); BufferedReader br = new BufferedReader(reader); Writer writer = new FileWriter(path); BufferedWriter bw = new BufferedWriter(writer); String str = null; for ((str = br.readLine()) != null ) { bw.write(str); bw.newLine(); } bw.flush(); bw.close(); br.close(); }
|
二者都是转换流,前者是输入转化流,后者是输出转换流,都是将字节流转换成字符流
1 2 3 4 5 6 7 8 9 10 11 12 13
| public static void modifyStream() { InputStream is = new FileInputStream(path); InputStreamReader isr = new InputStreamReader(is,"utf-8"); String str = null; while ((str = isr.read()) != null ) { System.out.println(str); } isr.close(); }
|
字节数组和其它数据类型的相互转换
1 2 3 4 5 6 7 8 9 10 11
| public static void byteArray() { Boolean bool = false; ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); dos.write(bool);
byte[] b = baos.toByteArray();
}
|
什么是序列化和反序列化?
- 将对象以byte流的形式写入到文件中称之为序列化。
- 将要被序列化的对象要实现Serializable接口。
- 将文件中的数据以byte流的形式读取到程序中。
- 静态的是不能被序列化的,序列化只能对堆中的对象序列化,不能对方法区中的对象序列化。
- 在不需要序列化的字段前面加上transient即可
transient的作用是?
不希望序列化的属性,可以添加transient关键字,密码字段是非常敏感的字段,所以在序列化时,不允许被写到文件。
完成目录的copy代码。(伪代码)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| public class CopyDir{ public static void main(String[] args) { copyDirectory("F:/UTAU","G:/CloudMusic"); } public static void copyFile(File sourceFile,File targetFile) { BufferedInputStream bis = new BufferedInputStream(new InputStream(sourceFile)); BufferedOutputStream bos = new BufferedOutputStream(new OutputStream(targetFile)); byt[] b = new byte[1024*5]; int len ; while ((len = bis.read(b)) != -1) bos.write(b,0,len); bos.flush(); bos.close(); bis.cloes(); } public static void copyDirectory(String sourceDir,String targetDir) { File fileSourceDir = new File(sourceDir); if (!fileSourceDir.exists() || !fileSourceDir.isDirectory()) { return; } File fileTargetDir = new File(targetDir); if (!fileTargetDir.exists()) { fileTargetDir.mkdirs(); } File[] files = fileSourceDir.listFiles(); for (File file : files) { if (file.isFile()) { File sourceFile = file; File targetFile = new File(fileTargetDir,file.getName()); copyFile(sourceFile,targetFile); } if (file.isDirectory()) { String subSourceDir = sourceDir+File.separator+file.getName(); String subTargetDir = targetDir+File.separator+file.getName(); copyDirectory(subSourceDir,subTargetDir); } } } }
|