前提背景
虽然说现在我已经开始工作了,而且在公司里,我也写过上传文件和下载文件。
但是实际上,我的字符流和字节流的输入和输出这一模块并不是很清楚,比如给我一个任务,叫我把数据库中查出来的数据写入到一个文件中,我都不知道怎么下手,那就趁着周末的时间来加强一下把!
练习
现有一个文件a.txt。(以下用到的b.txt文件是一开始不存在的)
我是bamboo
你是谁呢
字节流
1、练习1:单字节流输出
(1)代码
public static void main(String[] args) throws Exception {
File file = new File("D:/a.txt");
FileInputStream in = new FileInputStream(file);
int read;
while ((read=in.read())!=-1) {
System.out.print((char) read);
}
}
(2)输出结果,因为是字节流,而且我的文件中带有中文,为此只要中文打印出来的都是我们看不懂的,但是英文打印出来的还是能看懂的。
ææ¯bamboo
ä½ æ¯è°å¢
Process finished with exit code 0
2、练习2:多字节流输出
(1)代码
public static void main(String[] args) throws Exception {
File file = new File("D:/a.txt");
FileInputStream in = new FileInputStream(file);
int len;
byte[] bytes = new byte[2024];
while ((len=in.read(bytes))!=-1) {
System.out.println(new String(bytes,0,len));
}
in.close();
}
(2)输出结果:可以看到其打印结果是转换成String打印出来的,为此可以看到中文了。
我是bamboo
你是谁呢Process finished with exit code 0
3、练习3:多字节写入
(单字节写入也是一样的道理)
(1)代码
public static void main(String[] args) throws Exception {
FileOutputStream fileOutputStream = new FileOutputStream("D:/a.txt");
for (int i = 0; i < 6; i++) {
fileOutputStream.write("abcdefghijklmnopqrdtuvecxyz\n我来测试一下".getBytes());
}
fileOutputStream.close();
test();
}
// 主要是为了展示输出结果
private static void test() throws Exception {
File file = new File("D:/a.txt");
FileInputStream in = new FileInputStream(file);
int len;
byte[] bytes = new byte[2024];
while ((len=in.read(bytes))!=-1) {
System.out.println(new String(bytes,0,len));
}
in.close();
}
(2)结果展示:可以看到字节写入是成功的,结果和最开始的文件对比已经发生变化了。
abcdefghijklmnopqrdtuvecxyz
我来测试一下abcdefghijklmnopqrdtuvecxyz
我来测试一下abcdefghijklmnopqrdtuvecxyz
我来测试一下abcdefghijklmnopqrdtuvecxyz
我来测试一下abcdefghijklmnopqrdtuvecxyz
我来测试一下abcdefghijklmnopqrdtuvecxyz
我来测试一下Process finished with exit code 0
4、练习4:字节流复制文件
(文件包括图片、pdf等,都可以采用此方法)
(1)代码
public static void main(String[] args) throws Exception {
FileInputStream in = new FileInputStream("D:/a.txt");
FileOutputStream out = new FileOutputStream("D:/b.txt");
byte[] bytes = new byte[2024];
int len;
while ((len=in.read(bytes))!=-1) {
out.write(bytes,0,len);
}
out.close();
in.close();
test1();
}
// 为了展示b.txt文件复制成功
private static void test1() throws Exception {
File file = new File("D:/b.txt");
FileInputStream in = new FileInputStream(file);
int len;
byte[] bytes = new byte[2024];
while ((len=in.read(bytes))!=-1) {
System.out.println(new String(bytes,0,len));
}
in.close();
}
(2)结果展示
abcdefghijklmnopqrdtuvecxyz
我来测试一下abcdefghijklmnopqrdtuvecxyz
我来测试一下abcdefghijklmnopqrdtuvecxyz
我来测试一下abcdefghijklmnopqrdtuvecxyz
我来测试一下abcdefghijklmnopqrdtuvecxyz
我来测试一下abcdefghijklmnopqrdtuvecxyz
我来测试一下Process finished with exit code 0
字符流
1、练习1:字符流写入
(1)代码
public static void main(String[] args) throws Exception {
FileWriter fileWriter = new FileWriter("D:/a.txt");
fileWriter.write("Hello, I am Bamboo.And you?\n你好,我是Bamboo,你呢?");
fileWriter.close();
test2();
}
// 为了展示a.txt文件写入成功
private static void test2() throws Exception {
FileReader fileReader = new FileReader("D:/a.txt");
char[] chars = new char[2024];
int len;
while ((len=fileReader.read(chars)) != -1) {
System.out.println(new String(chars,0,len));
}
fileReader.close();
}
(2)结果演示
Hello, I am Bamboo.And you?
你好,我是Bamboo,你呢?Process finished with exit code 0
2、 练习2:字符流输出
(1)代码
public static void main(String[] args) throws Exception {
FileReader fileReader = new FileReader("D:/a.txt");
char[] chars = new char[2024];
int len;
while ((len=fileReader.read(chars)) != -1) {
System.out.println(new String(chars,0,len));
}
fileReader.close();
}
(2)结果展示:可以看到本次我们读取,和前面的字节流有所不同,我们读取的时候采用的chars读取的。
Hello, I am Bamboo.And you?
你好,我是Bamboo,你呢?Process finished with exit code 0
3、练习3:字符流复制文件
(1)代码
public static void main(String[] args) throws Exception {
FileReader fileReader = new FileReader("D:/a.txt");
FileWriter fileWriter = new FileWriter("D:/b.txt");
char[] chars = new char[2024];
int len;
while ((len=fileReader.read(chars)) != -1) {
fileWriter.write(chars,0,len);
}
fileWriter.close();
fileReader.close();
test4();
}
// 为了展示b.txt文件复制成功
private static void test4() throws Exception {
FileReader fileReader = new FileReader("D:/a.txt");
char[] chars = new char[2024];
int len;
while ((len=fileReader.read(chars)) != -1) {
System.out.println(new String(chars,0,len));
}
fileReader.close();
}
(2)结果展示:可以看到,b.txt文件和前面的文件对比,发生了变化,说明文件复制成功了。
Hello, I am Bamboo.And you?
你好,我是Bamboo,你呢?Process finished with exit code 0
总结
通过本次的字节流和字符流练习,我对其有了整体上的认识,也解决了查询数据库中的数据,写出数据到文件中的问题。
字节流和字符流是入门的最基础知识,还是很重要的,当时我为了更快的入门,都是学一步跳一步学的,导致我现在基础有些薄弱。
总结一句:脚踏实地真的很重要!