第七章:IO流
一:File文件类概括(一)File两大分类(二)File文档的操作(isFile)(三)对文件夹的操作(isDirectory)(四)查看一个文件夹下的所有文件(包括子文件夹下的和文档)
二:IO流的分类(一)什么是IO(二)IO流的分类(三)IO流下常用的16个类(壹):文件流专属①:字节流②:字符流
(贰):缓冲流流专属①:字节流②:字符流
(叁):转换流专属(字节流转化为字符流转化器)①:字节转字符流
(肆):数据流专属①:字节流②:字符流
(伍):对象流专属(序列化和反序列化)①:字节流下的对象流
(陆):标准输出流
一:File文件类概括
(一)File两大分类
文件类的操作包括对两大分类的操作:文档和文件夹
(二)File文档的操作(isFile)
@Test
public void testA() throws IOException
{
File file
= new File("D:\\有道云翻译\\aaa.txt");
boolean exists
= file
.exists();
if (!exists
){
boolean newFile
= file
.createNewFile();
}
boolean file1
= file
.isFile();
System
.out
.println("file1 = " + file1
);
boolean directory
= file
.isDirectory();
System
.out
.println("directory = " + directory
);
String name
= file
.getName();
System
.out
.println("name = " + name
);
String absolutePath
= file
.getAbsolutePath();
System
.out
.println("absolutePath = " + absolutePath
);
File absoluteFile
= file
.getAbsoluteFile();
System
.out
.println("absoluteFile = " + absoluteFile
);
boolean b
= absoluteFile
.equals(file
);
System
.out
.println("b = " + b
);
long totalSpace
= file
.getTotalSpace();
System
.out
.println("totalSpace = " + totalSpace
/1024/1024/1024 + "GB");
long freeSpace
= file
.getFreeSpace();
System
.out
.println("freeSpace = " + freeSpace
/1024/1024/1024 + "GB");
boolean b1
= file
.canRead();
System
.out
.println("b1 = " + b1
);
boolean b2
= file
.canRead();
System
.out
.println("b2 = " + b2
);
boolean b3
= file
.canExecute();
String parent
= file
.getParent();
System
.out
.println("parent = " + parent
);
String path
= file
.getPath();
System
.out
.println("path = " + path
);
long length
= file
.length();
System
.out
.println("length = " + length
);
file
.delete();
}
(三)对文件夹的操作(isDirectory)
@Test
public void testB() throws IOException
{
File file
= new File("D:\\aa\\bb\\cc\\dd/ee");
if (!file
.exists()){
boolean mkdirs
= file
.mkdirs();
System
.out
.println("newFile = " + mkdirs
);
}
boolean directory
= file
.isDirectory();
System
.out
.println("directory = " + directory
);
boolean file1
= file
.isFile();
System
.out
.println("file1 = " + file1
);
File file2
= new File("D:\\aa");
String
[] list
= file2
.list();
for (String s
: list
) {
System
.out
.println("s = " + s
);
}
File
[] files
= file2
.listFiles();
for (File file3
: files
) {
String absolutePath
= file3
.getAbsolutePath();
System
.out
.println("absolutePath = " + absolutePath
);
}
File f
= new File("D:/aa/a.txt");
f
.renameTo(new File("D:\\aa\\bb\\b.txt"));
}
(四)查看一个文件夹下的所有文件(包括子文件夹下的和文档)
我们需要判断文件对象是文档还是文件夹,如果是文件夹,那么就获取其下面所有文件,如果是文档就直接获取其抽象路径名称,然后递归此方法就行了
@Test
public void testC(){
File file
= new File("D:\\零碎学习软件");
testGet(file
);
}
private void testGet(File file
) {
File
[] files
= file
.listFiles();
for (File file1
: files
) {
if (file1
.isDirectory()) {
String aa
= file1
.getAbsolutePath();
System
.out
.println("目录: " + aa
);
testGet(file1
);
} else {
String absolutePath
= file1
.getAbsolutePath();
System
.out
.println("文档:" + absolutePath
);
}
}
}
二:IO流的分类
(一)什么是IO
I:Input---------》输入O:output------》输出通过IO可以完成硬盘文件的读写操作
(二)IO流的分类
按照流的方向的不同可分为:输入流和输出流,我们以内存或者正在执行的程序为参照物,外界到内存为输入,内存到外界为输出按照读取数据的方式不同:分为字节流和字符流,字节流:InputStream 和 OutputStream,他们都是抽象类,只能通过其子类实例化!字节流是一次读取一个字节,即1个byte,8位。可以读取任何文件字符流:Reader和Writer,他们都是抽象类,只能通过其子类实例化! 字符流是一次读取一个字符,即char,2字节,16位。只能读取记事本能打开的普通文本文档,其中一个中文占用2个字节
(三)IO流下常用的16个类
(壹):文件流专属
①:字节流
FileInputStream
public class TestByteSteam {
@Test
public void testA() throws IOException
{
File file
= new File("D:\\有道云翻译\\SumatraPDF-settings.txt");
FileInputStream fis
= new FileInputStream(file
);
byte[] b
= new byte[(int) file
.length()];
int read
= fis
.read(b
);
String s
= new String(b
);
System
.out
.println(s
);
fis
.close();
}
FileOutputStream
@Test
public void testB() throws IOException
{
File file
= new File("D:\\a\\a.txt");
if (!file
.exists()){
file
.createNewFile();
}
FileOutputStream fos
= new FileOutputStream(file
);
String str
= "dsadsasda";
byte[] b
= str
.getBytes();
fos
.write(b
);
fos
.close();
}
读和写一起操作
@Test
public void testC() throws IOException
{
File oldF
= new File("D:\\有道云翻译/SumatraPDF-settings.txt");
File newF
= new File("D:\\a/SumatraPDF-settings.txt");
if (!newF
.exists()){
newF
.createNewFile();
}
FileInputStream fis
= new FileInputStream(oldF
);
FileOutputStream fos
= new FileOutputStream(newF
);
byte[] b
= new byte[(int) oldF
.length()];
fis
.read(b
);
fos
.write(b
);
fos
.flush();
fis
.close();
fos
.close();
}
②:字符流
FileReader
**
* 字符流:Reader 和 Writer
* 字符流特点:一次读写一个字符即
2位,只能用于文本文档中
*/
public class TestCharStream {
@Test
public void testA() throws IOException
{
File file
= new File("D:\\有道云翻译/SumatraPDF-settings.txt");
FileReader fr
= new FileReader(file
);
char[] c
= new char[(int) file
.length()];
fr
.read(c
);
System
.out
.println(c
);
fr
.close();
}
FileWriter
@Test
public void testB() throws IOException
{
File file
= new File("D:/a/a.txt");
FileWriter fw
= new FileWriter(file
,true);
String str
= "啊啊啊啊啊,dadaismdsadsads";
fw
.write(str
);
fw
.flush();
fw
.close();
}
读和写操作
@Test
public void testC() throws IOException
{
File file
= new File("D:/a/testdemo.txt");
if (!file
.exists()){
file
.createNewFile();
}
FileWriter fw
= new FileWriter(file
);
String str
= "李明|张小林|胡守成";
fw
.write(str
);
fw
.flush();
FileReader fr
= new FileReader(file
);
char[] ch
= new char[(int) file
.length()];
fr
.read(ch
);
String s
= new String(ch
);
String
[] split
= s
.split("\\|");
for (String s1
: split
) {
System
.out
.println( s1
);
}
fr
.close();
fw
.close();
}
(贰):缓冲流流专属
缓冲流其实就是一个自带缓冲功能的包装流,其需要传入一个字节流或者字符流对象,即节点流。一次缓冲流可以储存8kb字节。所以传输速度快。字节关闭缓冲流即可达到关闭节点流的效果,其中字符缓冲流一次可以读取一行。readLine();
①:字节流
BufferedInputStream
@Test
public void testB() throws IOException
{
File f1
= new File("D:\\java学习课程\\Java面试汇总0510.pdf");
File f2
= new File("D:/Java面试汇总0510.pdf");
FileInputStream fis
= new FileInputStream(f1
);
BufferedInputStream bis
= new BufferedInputStream(fis
);
FileOutputStream fos
= new FileOutputStream(f2
);
BufferedOutputStream bos
= new BufferedOutputStream(fos
);
int read
= 0;
while (!((read
= bis
.read()) == -1)){
bos
.write(read
);
}
bis
.close();
bos
.close();
}
BufferedOutputStream
②:字符流
字符流的缓冲字符流有一个特有的一次读取一行文件的方法ReadLint()
BufferedReader
@Test
public void testC() throws IOException
{
File file
= new File("D:\\a.txt");
FileReader fw
= new FileReader(file
);
BufferedReader bw
= new BufferedReader(fw
);
String s
= bw
.readLine();
System
.out
.println("s = " + s
);
bw
.close();
}
我们常常用读取一行的方式用在键盘输入输出流中结合使用,请看标准输入输出流 2. BufferedWriter
(叁):转换流专属(字节流转化为字符流转化器)
①:字节转字符流
多用于字节流转化为字符流做转换器的功能
InputSreamReaderOutputStreamWriter
(肆):数据流专属
①:字节流
DataInputStreamDataOutputStream
②:字符流
DataReaderDataWriter
(伍):对象流专属(序列化和反序列化)
①:字节流下的对象流
此流可以将内存中的对象分割保存到硬盘中,也可以将硬盘中的分割对象组装到内存中。对象流可以传递一个对象(实现可序列化接口Serializable(标记接口),包括属性行为,transient标记的属性是关闭序列化功能…),提供对对象图形的持久存储,很少用,因为有数据库,可以用在模板与模板之间读取调用对象的功能,也可以对集合进行序列化与反序列化 标记接口没有任何内容,JVM遇到这个接口会生成此对象的序列化版本号,版本号用来区分同名类是否是同一个类
ObjectInputStream----------》组装,反序列化
@Test
public void testB() throws IOException
, ClassNotFoundException
{
File f
= new File("D:/aa/b/a.txt");
FileInputStream fis
= new FileInputStream(f
);
ObjectInputStream ois
= new ObjectInputStream(fis
);
for (int i
= 0; i
< 4; i
++) {
Student st
= (Student
) ois
.readObject();
System
.out
.println(st
);
}
ois
.close();
}
ObjectOutputStream--------》分割,序列化
public class Test03 {
@Test
public void testA() throws IOException
{
File file
= new File("D:/aa/b");
file
.mkdirs();
File file1
= new File("D:/aa/b/a.txt");
Student s1
= new Student(1,"小红","女",18,"美国");
Student s2
= new Student(2,"小白","男",18,"欧洲");
Student s3
= new Student(3,"小绿","女",18,"日本");
Student s4
= new Student(4,"小黑","男",18,"非洲");
FileOutputStream fos
= new FileOutputStream(file1
);
ObjectOutputStream oos
= new ObjectOutputStream(fos
);
oos
.writeObject(s1
);
oos
.writeObject(s2
);
oos
.writeObject(s3
);
oos
.writeObject(s4
);
oos
.close();
}
(陆):标准输出流
PrintWriterPrintSream
public static void testD() throws IOException
{
InputStream in
= System
.in
;
File f
= new File("D:/b.txt");
InputStreamReader isr
= new InputStreamReader(in
);
BufferedReader br
= new BufferedReader(isr
);
PrintWriter ps
= new PrintWriter(f
);
String s
= "";
while (!"end".equalsIgnoreCase(s
)){
s
= br
.readLine();
ps
.println(s
);
}
br
.close();
ps
.close();
}