Java输入输出流

概述

文件输入——读:读取文件中的数据
文件输出——写:将数据写入到目的地

File类的常用方法

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
package com.imooc.file;

import java.io.File;
import java.io.IOException;

public class FileDemo {

public static void main(String[] args) {
//创建File对象
//File file1=new File("c:\\imooc\\io\\score.txt");
//File file1=new File("c:\\imooc","io\\score.txt");
File file=new File("c:\\imooc");
File file1=new File(file,"io\\score.txt");
//判断是文件还是目录
System.out.println("是否是目录:"+file1.isDirectory());
System.out.println("是否是文件:"+file1.isFile());
//创建目录
File file2=new File("c:\\imooc\\set\\HashSet");
if(!file2.exists()) {
file2.mkdirs();
}
//创建文件
if(!file1.exists()) {
try {
file1.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

}

绝对路径与相对路径

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
绝对路径:从盘符开始的路径
File f = new File("c:\\imooc\\java\\thread\\thread.txt");
相对路径:从当前路径开始的路径
1. 如果Java文件跟它其中要找的txt文件处于同一 个目录下,就用相对路径,如下:
File f = new File("thread.txt");
2. 如果java文件跟thread在同一个目录,thread底下才是它要找的thread.txt,就这样写:
File f = new File("thread\\thread.txt");
3. 如果io跟thread文件夹在同一个目录下,而java文件在io里,thread文件仍在thread文件夹里,如下:
File f = new File("..\\thread\\thread.txt");
这样就跳到了上一级目录之后再往下找
4. 创建文件
File f = new File("thread.txt");
try{
f.createNewFile();
}catch(IOException e){
e.printStackTrace();
}
这样创建的文件是存放于工程目录里的,为避免后续获取路径时出错,由于一开始new File的时候是指定thread.txt在thread文件夹下的,所以要在工程目录里创建thread文件夹,再把thread.txt放进去
5. 判断是绝对路径还是相对路径
f.isAbsolute();
true则是绝对路径,否则是相对路径
6. 获取相对路径
f.getPath();
7. 获取绝对路径
f.getAbsolutePath();
8. 获取文件名
f.getName();
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
package com.imooc.path;

import java.io.File;
import java.io.IOException;

public class FileDemo {

public static void main(String[] args) {
```

// File f=new File("c:\\imooc\\java\\thread\\thread.txt");
// System.out.println(f.exists());
File f=new File("thread.txt");
try {
f.createNewFile();
//是否是绝对路径
System.out.println(f.isAbsolute());
//获取相对路径
System.out.println(f.getPath());
//获取绝对路径
System.out.println(f.getAbsolutePath());
//获取文件名
System.out.println(f.getName());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

字节流概述

字节流
InputStream字节输入流:从输入设备中读取数据
OutputStream字节输出流:将数据写入到输出设备当中
主要介绍以下几类:
FileInputStream
BufferedInputStream
FileInputStream
BufferedOutpueStream

FileInputStream(上)

FileInputStream 文件输入流

从文件系统中的某个文件中获得输入字节
用于读取诸如图像数据之类得原始字节流
public int read() :从输入流中获取一个数据字节
public int read(byte[] b);从输入流中将最多b.length个字节的数据读入一个byte数组
public int read(byte[] b ,int off,int len); 从输入流中将最对len个字节的数组读入byte数组中
pubic void close(); 关闭此文件输入流并释放与流有关的所有系统资源
如果返回值为-1,则表示已经到达文件的末尾

FileInputStream(下)

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
package com.imooc.file;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileInputDemo1 {

public static void main(String[] args) {
//创建一个FileInputStream对象
try {
FileInputStream fis=new FileInputStream("imooc.txt");

// int n=fis.read();
int n=0;
// while(n!=-1){
// System.out.print((char)n);
// n=fis.read();
// }
while((n=fis.read())!=-1){
System.out.print((char)n);
}

fis.close();
}catch (FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.imooc.file;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileInputDemo2 {

public static void main(String[] args) {
// 创建一个FileInputStream对象
try {
FileInputStream fis = new FileInputStream("imooc.txt");
byte[] b=new byte[100];
fis.read(b,0,5);
System.out.println(new String(b));
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}

FileOutputStream(上)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public static void main(String[] args) {
FileOutputStream fos;
FileInputStream fis;
try {
fos = new FileOutputStream("imooc.txt",true);
fis=new FileInputStream("imooc.txt");
fos.write(50);
fos.write('a');
System.out.println(fis.read());
System.out.println((char)fis.read());
fos.close();
fis.close();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

此时运行程序,打开imooc.txt文件中的内容不再是Hello,imooc而是2a,因为编码的一些原因,存进去的数据不一定是我们想要的,但是用InputStream再去读文件的时候,输出的就是我们想要的50a了.可见字节流不适合做一些和字符相关的操作的,可能会存入意想不到的数据,它更适合处理一些二进制相关的数据,比如图片文件的复制
如果把try块中第一句改成
fos = new FileOutputStream(“imooc.txt”,true);
此时打开txt文档,就变成2a2a了

FileOutputStream(下)

图片的复制粘贴
复制粘贴的过程就是读取写入的过程
程序如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static void main(String[] args) {
// 文件拷贝
try {
FileInputStream fis=new FileInputStream("happy.gif");
FileOutputStream fos=new FileOutputStream("happycopy.gif");
int n=0;
byte[] b=new byte[1024];
while((n=fis.read(b))!=-1){
fos.write(b,0,n);
}
fis.close();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
1
2
3
//如何解决拷贝之后的文件比之前的文件大的问题?
fos.write(b,0,n);
从0开始写数据,n不等于-1的时候表示的是实际读取到的字节数组b当中的字节数,如果最后一次没读满1024个字节而是x个字节(x<1024),n的值就是x

缓冲流概述

作用:提高读写速度。
其中关于BufferedOutputStream中,有一个flush方法,用于强制将缓冲区内容输出。原因:缓冲区的工作特性是:当缓冲区的空间存放满了,缓冲区的数据将自动执行写操作。当缓冲区空间未满时,则不会执行写操作,这样将会导致问题。调用flush方法强制清空缓冲区,将内容完整的输出。

缓冲流案例

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
package com.imooc.file;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class BufferedDemo {

public static void main(String[] args) {
try {
FileOutputStream fos=new FileOutputStream("imooc.txt");
BufferedOutputStream bos=new BufferedOutputStream(fos);
FileInputStream fis=new FileInputStream("imooc.txt");
BufferedInputStream bis=new BufferedInputStream(fis);
long startTime=System.currentTimeMillis();
bos.write(50);
bos.write('a');
bos.flush();
System.out.println(bis.read());
System.out.println((char)bis.read());
long endTime=System.currentTimeMillis();
System.out.println(endTime-startTime);
fos.close();
bos.close();
fis.close();
bis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

字符流概述

字节字符转换流

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
package com.imooc.charstream;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class ReaderDemo {

public static void main(String[] args) {
try {
FileInputStream fis=new FileInputStream("imooc.txt");
InputStreamReader isr=new InputStreamReader(fis,"GBK");
BufferedReader br=new BufferedReader(isr);
FileOutputStream fos=new FileOutputStream("imooc1.txt");
OutputStreamWriter osw=new OutputStreamWriter(fos,"GBK");
BufferedWriter bw=new BufferedWriter(osw);
int n=0;
char[] cbuf=new char[10];

// while((n=isr.read())!=-1){
// System.out.print((char)n);
// }
// while((n=isr.read(cbuf))!=-1){
// String s=new String(cbuf,0,n);
// System.out.print(s);
// }
while((n=br.read(cbuf))!=-1){
//String s=new String(cbuf,0,n);
bw.write(cbuf, 0, n);
bw.flush();
}
fis.close();
fos.close();
isr.close();
osw.close();
br.close();
bw.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

}

对象的序列化与反序列化

步骤:
创建一个类,继承Serializable接口
创建对象
将对象写入文件
从文件读取对象信息
对象的读写涉及到两个类:
对象输入流ObjectOutputStream
对象输出流ObjectOutpurStream

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
package com.imooc.serial;

import java.io.Serializable;

public class Goods implements Serializable{
private String goodsId;
private String goodsName;
private double price;
public Goods(String goodsId,String goodsName,double price){
this.goodsId=goodsId;
this.goodsName=goodsName;
this.price=price;
}
public String getGoodsId() {
return goodsId;
}
public void setGoodsId(String goodsId) {
this.goodsId = goodsId;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "商品信息 [编号:" + goodsId + ", 名称:" + goodsName

- ", 价格:" + price + "]";
}

}
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
package com.imooc.serial;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class GoodsTest {

public static void main(String[] args) {
// 定义Goods类的对象
Goods goods1 = new Goods("gd001", "电脑", 3000);
try {
FileOutputStream fos = new FileOutputStream("imooc.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
FileInputStream fis = new FileInputStream("imooc.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
// 将Goods对象信息写入文件
oos.writeObject(goods1);
oos.writeBoolean(true);
oos.flush();
// 读对象信息
try {
Goods goods = (Goods) ois.readObject();
System.out.println(goods);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(ois.readBoolean());

fos.close();
oos.close();
fis.close();
ois.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}
-------------本文结束感谢您的阅读-------------