Posts Java IO
Post
Cancel

Java IO

Java IO

IO: Input And Output

java 中有IO,即输入与输出,输入input 输出output

java 有典型的IO与NIO,常说的IO指典型IO, 阻塞IO, NIO表示非阻塞IO

阻塞: 等待IO任务完成才能继续下一步的操作

非阻塞: 暂不能清晰描述,可以查看网址文章链接:http://ifeve.com/overview/

这里简单介绍Java中常见的文件读写’’

字节流读写文件:

读文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static void readFile(String filePath) throws IOException {
		File file = new File(filePath);
		if (!file.exists() || !file.isFile()) {
			System.out.println("file is not exist");
		} else {
			FileInputStream fileInputStream = new FileInputStream(file); //创建文件输入流
			byte[] read = new byte[1024];
			while (fileInputStream.read(read) != -1) {
				System.out.println("read content:" + read);
			}
			fileInputStream.close(); // 关闭流 释放资源
		}

	}

写文件

1
2
3
4
5
6
7
8
public static void writeFile(byte[] content) throws IOException {
		File file = new File("D:\\Jser\\data\\test\\writeFile.xml"); //获取写文件的位置
		if (!file.exists())
			file.createNewFile();
		FileOutputStream fileOutputStream = new FileOutputStream(file); //创建文件输出流
		fileOutputStream.write(content); // content 表示写入文件的内容 
		fileOutputStream.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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class TestIO {
    
	public static void main(String[] args) throws IOException {
		String originFilePath = "D:\\Jser\\data\\git-rep\\jdp\\note\\Program-StudyPlan.md";
		String aimFilePath = "D:\\Jser\\data\\test\\aimFile.md";
		copyFile(originFilePath, aimFilePath);
//		testRandonAccessFile();
	}    
	
	/*
	 * 读文件
	 */
	public static void readFile(String filePath) throws IOException {
		File file = new File(filePath);
		if (!file.exists() || !file.isFile()) {
			System.out.println("file is not exist");
		} else {
			FileInputStream fileInputStream = new FileInputStream(file);
			byte[] read = new byte[1024];
			while (fileInputStream.read(read) != -1) {
				System.out.println("read content:" + read);
			}
			fileInputStream.close();
		}

	}
	
	/**
	 * 写文件
	 * @throws IOException
	 */
	public static void writeFile(byte[] content) throws IOException {
		File file = new File("D:\\Jser\\data\\test\\writeFile.xml");
		if (!file.exists())
			file.createNewFile();
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		fileOutputStream.write(content);
		fileOutputStream.close();
	}
    
	/*
	 * 文件复制 -> 读取一个文件复制到另一个地方
	 */
	public static void copyFile(String originFilePath, String aimPath) {
		File originFile = new File(originFilePath);
		if (!originFile.exists()) {
			System.err.println("file is not exists!");
			return;   //文件不存在
		}
		File aimFile = new File(aimPath);
		FileInputStream fi = null;
		FileOutputStream fo = null;
		try {
			byte[] content = new byte[1024];   //每次读文件内容的字节数组
			if (!aimFile.exists())
				aimFile.createNewFile();
            // 通过路径转化为 输入流 和 输出流
			fi = new FileInputStream(originFile);  //输入流
			fo = new FileOutputStream(aimFile);	//输出流
			while (fi.read(content) != -1) { //输入流将数据读取到字节数组,如果返回值为 -1 表示文件已经读完
				fo.write(content); //输出流将数组内容写入文件
			}
		} catch (IOException e) {
			System.err.println("read file exception!");
			try {
				// close resources;
				if (fo != null)
					fo.close();
				if (fi != null)
					fi.close();
			} catch (IOException es) {
				// TODO Auto-generated catch block
				es.printStackTrace();
			}
		} finally {
			try {
				if (fo != null)
					fo.close();
				if (fi != null)
					fi.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
    
    /*
    * java 涉及NIO的 RandomAccessFile 类
    */
	private static void testRandonAccessFile() throws IOException {
		RandomAccessFile aFile = new RandomAccessFile("D:\\Jser\\data\\git-rep\\config\\maven\\settings.xml", "rw");
		FileChannel channel = aFile.getChannel();
		File file = new File("D:\\Jser\\data\\test\\creatFile.xml");
		if (!file.getParentFile().exists())
			file.getParentFile().mkdirs();

		if (!file.isFile()) {
			System.err.println("file is not exist!");
			System.exit(0);
		}
		if (!file.exists())
			file.createNewFile();

		FileOutputStream newFile = new FileOutputStream(file);
		ByteBuffer buffer = ByteBuffer.allocate(1024);
		int i = 0;
		while (channel.read(buffer) != -1) {
			i++;
			buffer.flip();
			while (buffer.hasRemaining()) {
				newFile.write(buffer.get());
			}
			buffer.clear();
		}
		aFile.close();
		newFile.close();
		System.out.println("文件读取次数:" + i);
	}

}
This post is licensed under CC BY 4.0 by the author.

==与equals区别

Oracle

Comments powered by Disqus.