基础IO操作

基本IO操作代码

简单读取文件
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* Stream
* 流的方向分类:输入流输出流
* 流中的数据分类:字节流、字符流
* 1、认识数据源(字节数组、String对象、文件、管道、其他的流、网络)
* 2、根据数据源选择正确的读取数据流的类
* 3、为了提高读写效率用装饰流对原先的流进行装饰
* 字节输入流:InputStream(所有字节输入流的父类)
* ByteArrayInputStream(内存缓冲区会当作数据源)
* StringBufferInputStream(将String对象转换为数据源)
* FileInputStream(将文件作为数据源)
* PipedInputStream(用于将PipedOutputStream的数据作为数据流)
* 字节输出流:OutputStream
*
* read:阻塞方法(Scanner)
* int read();读取输入流的第一个字节,返回值是其读的内容,返回值的前三个字节是没有用的,
* 仅仅在读到文件末尾时,会返回-1
* int read(byte[] b);读取输入流数据并存储到byte[]数组当中,在读到文件末尾时,会返回-1,或返回读取的字节数
* int read(byte[] b ,int offset,int len);读取输入流数据并存入byte[]的offset位置,放len这么多
* write:
* void write(int b);向流的尾部写入一个字节
* void write(byte[] b);将数组b中的内容写入流中
* void write(byte[] b,int offset,int len);将数组b中offset开始的len个字符的内容写入流中
*/
/*
* InputStream
* FileInputStream 文件字节输入流
* ByteArrayInputStream byte数组字节输入流,用于临时文件处理
* ObjectInputStream 类类型字节输入流,对类对象的保存(要实现Serializable接口)
* BufferedInputStream 拥有缓冲区的字节输入流
* DataInputStream 基本数据类型字节输入流
* OutputStream
* FileOutputStream 文件字节输出流
* ObjectOutputStream 类类型字节输入流,对类对象的保存(要实现Serializable接口)
* ByteArrayOutputStream byte数组字节输出流,用于临时文件处理
* BufferedOutputStream 拥有缓冲区的字节输入流
* DataOutputStream 基本数据类型字节输入流
* Reader
* FileReader 文件字符输入流
* BufferReader 拥有缓冲区的字符输入流
* InputStreamReader 把字节输入流转换为字符输入流
* LineNumberReader 按行读取文本输入流
* StringReader 把字符串按字符输入流
* Writer
* FileWriter 文件字符输出流
* BufferedWriter 拥有缓冲区的字符输出流
* PrintWriter 打印输出流
* OutputStreamWriter 把字节输出流转换为字符输出流
* StringWriter 把字符串按字符输出流
* RandomAccessFile
* FileNameFilter(接口) 在文件中查找指定的名字
*/
public class ReadFileByByte {
public static void readFile() {
InputStream is = null;
try {
is = new FileInputStream("/home/soft02/桌面/a.txt");//打开文件
//读
byte[] b = new byte[500];
int length = is.read(b);
System.out.println("length:"+length);
System.out.println("读到的内容:"+new String(b,0,length));
} catch (FileNotFoundException e) {
System.out.println("没有该文件"+e.getMessage());
} catch (IOException e) {
e.printStackTrace();
}finally{
if(is != null){
try {
is.close();//关闭文件
} catch (IOException e) {
System.out.println("关闭文件失败"+e.getMessage());
}
}
}
}

public static void main(String[] args) {
readFile();
}
}
读取大数据量文件
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
/*
* 使用512字节的数组读取41Kb的文件
* 以512字节为单位,循环读取
*/
public class ReadFromLongFile {
public static void readFile() {
InputStream is = null;
File file = new File("/home/soft02/桌面/a.txt");
try {
is = new FileInputStream(file);
byte[] b = new byte[512];
int length;
while((length = is.read(b)) != -1){
System.out.println(new String(b,0,length));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

public static void main(String[] args) {
readFile();
}
}
文件复制
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
/*
* 文件复制
* FileOutPutStream 如果文件不存在,则创建文件再写入文件中
* 如果文件存在,重新创建该文件然后再写入文件中,原内容被覆盖了
*/
public class CopyFileByByte {
public static void copyFile() {
InputStream is = null;
File file = new File("/home/soft02/桌面/a.txt");
File newFile = new File("/home/soft02/桌面/b.txt");
OutputStream os = null;
try {
is = new FileInputStream(file);
os = new FileOutputStream(newFile,true);
byte[] b = new byte[512];
int length;
while((length = is.read(b)) != -1){
os.write(b, 0, length);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

public static void main(String[] args) {
copyFile();
}
}
用FileReader和FileWriter读写文件
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
/*
* 一个文件中存了以下信息。
* 大家好,我叫name,今年age岁,name是一个爱学习的好孩子,
* name养了一条宠物狗,它的名字叫dogname,dogname很调皮
* 欢迎大家来name家做客,和dogname一起玩。
* 从键盘输入name,age,dogname
* 存入新文件
*/
public class FileReaderAndWriter {
public static void changeStr() throws IOException {
FileReader fr = null;
FileWriter fw = null;
BufferedReader br = null;
BufferedWriter bw = null;
Person p = new Person("zhangsan", 23, "ahuang");
fr = new FileReader("/home/soft02/桌面/homework.txt");
fw = new FileWriter("/home/soft02/桌面/homework1.txt");
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
String s ;
while((s = br.readLine()) != null){
s = s.replaceAll("dogname", p.getDogname());
s = s.replaceAll("name", p.getName());
s = s.replaceAll("age", Integer.toString(p.getAge()));
bw.write(s);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}

public static void main(String[] args) throws IOException {
changeStr();
}
}
class Person{
private String name;
private int age;
private String dogname;
public Person(String name, int age, String dogname) {
super();
this.name = name;
this.age = age;
this.dogname = dogname;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getDogname() {
return dogname;
}
}
posted @ 2019-07-31 21:26  qxwang  阅读(51)  评论(0)    收藏  举报