Java输入输出流

输出流

@输出流

输入流

@输入流

绝对路径和相对路径

  • 绝对路径:从盘符开始的路径
  • 相对路径:从当前路径开始的路径

字节流

字节输入流InputStream

Alt text

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

例子

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

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

/**
* @Description:文件练习
* @Date: Created in 15:36 2018/2/21
* @Author: Anthony_Duan
*/
public class FileInputDemo1 {

public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("/Users/duanjiaxing/data/imooc/test");
int n;
while ((n=fis.read())!=-1){
System.out.print((char)n);
}
fis.close();
}catch (FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}

/*字节数组的方式
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();
}
*/

}
}

字节输出流OutputStream

Alt text

  1. FileOutputStream文件输出流常用方法
方法名 说明
public void write(int b) 将制定字节写入此文件输入流
public void write(byte[] b) 将b.length个字节从制定byte数组写入此文件输出流中
public void write (byte[] b, int off,int len) 将制定byte数组中从偏移量off开始的len个字节写入此文件输出流
public void close() 关闭此文件输出流并释放此流有关的所有系统资源

文件拷贝例子

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.company;

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

/**
* @Description: 文件拷贝练习
* @Date: Created in 15:49 2018/2/21
* @Author: Anthony_Duan
*/
public class FileOutputDemo1 {

public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("/Users/duanjiaxing/data/imooc/test");
FileOutputStream fos = new FileOutputStream("/Users/duanjiaxing/data/imooc/test1");
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) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
}

缓冲流
  • 缓冲输入流BufferedInputStream
  • 缓冲输出流BufferedOutputStream

例子

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
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("/Users/duanjiaxing/data/imooc/test");
BufferedOutputStream bos=new BufferedOutputStream(fos);
FileInputStream fis=new FileInputStream("/Users/duanjiaxing/data/imooc/test");
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();
}
}
}

字符流
  • 字符输入流Reader
    Alt text

  • 字符输出流Writer
    Alt text

字节字符转换流
  • InputStreamReader
  • OutputStreamWriter

例子

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
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("/Users/duanjiaxing/data/imooc/test");
InputStreamReader isr=new InputStreamReader(fis,"GBK");
BufferedReader br=new BufferedReader(isr);
FileOutputStream fos=new FileOutputStream("/Users/duanjiaxing/data/imooc/test");
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();
}


}

}

对象序列化
  • 对象输入流ObjectInputStream
  • 对象输出流ObjectOutputStream
  • 序列化步骤
    • 创建一个类,继承Serializable接口
    • 创建对象
    • 将对象写入文件
    • 从文件读取对象信息

例子

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("/Users/duanjiaxing/data/imooc/test");
ObjectOutputStream oos = new ObjectOutputStream(fos);
FileInputStream fis = new FileInputStream("/Users/duanjiaxing/data/imooc/test");
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();
}

}

}

-------------End Of This ArticleThank You For Reading-------------