我是靠谱客的博主 慈祥宝马,这篇文章主要介绍Java基础(十),现在分享给大家,希望可以做个参考。

10-1 字节流与字符流有什么区别?应用场合分别是什么?
字节流:处理字节和字节数组或二进制对象;
字符流:处理字符、字符数组或字符串。
字符流方便处理字符,和文本处理有关系的就用字符流 ,其他的比如说音频视频图片等等文件的,和字符没有关系了,就用字节流

10-2 编写一个控制台程序,对于用户输入的一个目录名,程序列出该目录下所有的文件。

复制代码
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
import java.util.Scanner; import java.io.File; public class Demo_1{ public static void list(String f){ File file=new File(f); if(file.isDirectory()){ File a[]=file.listFiles(); if(a!=null){ for(int i=0;i<a.length;i++){ String name = a[i].getName(); System.out.println(name); if(a[i].isDirectory()){ String path=a[i].getPath(); list(path); } } } } } public static void main(String[] args) { System.out.println("请输入路径"); Scanner scan=new Scanner(System.in); String f=scan.nextLine(); list(f); } }

10-3 编写程序,查找指定目录下所有的word文档。

复制代码
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
import java.io.File; public class Demo_2 { public static void list(String filename) { File file = new File(filename); if (file.isDirectory()) { File test[] = file.listFiles(); if (test != null) { for (int i = 0; i < test.length; i++) { String name = test[i].getName(); if (name.trim().toLowerCase().endsWith(".docx")) { System.out.println(name); } if(test[i].isDirectory()){ String path=test[i].getPath(); list(path); } } } } } public static void main(String[] args) { Demo_2 filename = new Demo_2(); filename.list("D:\"); } }

10-4编写程序,实现文件拷贝的功能。

复制代码
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
import java.io.*; public class Demo_3 { private static void copy(File source, File dest) throws IOException { InputStream input = null; OutputStream output = null; try { input = new FileInputStream(source); output = new FileOutputStream(dest); byte[] buf = new byte[1024]; int bytesRead; while ((bytesRead = input.read(buf)) > 0) { output.write(buf, 0, bytesRead); } } finally { input.close(); output.close(); } } public static void main(String[] args) throws IOException { File f1=new File("1.txt"); File f2=new File("2.txt"); copy(f1,f2); System.out.println("完成"); } }

10-5 编写程序,实现文件剪切的功能。

复制代码
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
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Demo_4 { public static void cut(File souce, File des) throws IOException { InputStream input = null; OutputStream output = null; try { if (!souce.exists()) { souce.createNewFile(); } if (!des.exists()) { des.createNewFile(); } input = new FileInputStream(souce); output = new FileOutputStream(des); byte[] buf = new byte[1024]; int bytesRead; while ((bytesRead = input.read(buf)) > 0) { output.write(buf, 0, bytesRead); } }finally { output.close(); input.close(); } } public static void main(String[] args) throws IOException { File f1 = new File("D:/1.txt"); File f2 = new File("D:/1/4.txt"); cut(f1, f2); f1.delete(); } }

10-6 编写一个控制台程序:猜数字游戏。程序产生1个1~100之间随机数,并提示用户输入,一直到用户的输入恰好等于那个随机数,程序结束。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.util.Scanner; public class Demo_5 { public static void main(String[] args) { int j = (int) (100 * Math.random()) + 1; // System.out.println(j); System.out.println("请输入1~100间的数字:"); Scanner scan = new Scanner(System.in); int in = scan.nextInt(); while (in != j) { System.out.println("数字不对,请重新输入:"); in = scan.nextInt(); } System.out.println("你猜对了!"); } }

10-7 编写一个控制台程序:日记程序。接收用户的输入并保存到文件中。

复制代码
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
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; public class Demo_6 { public static void main(String args[]) throws IOException { String s; InputStreamReader in = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(in); FileWriter fw = new FileWriter("D:\日记.txt"); BufferedWriter bw = new BufferedWriter(fw); System.out.println("请开始你的表演:(以空行结束)"); while (true) { s = br.readLine(); if (s.length() == 0) break; bw.write(s); bw.newLine(); } br.close(); bw.close(); System.out.println("日记已经保存"); } }

10-8编写程序,分别以字节流和字符流的方式读取包含中文符号的文件,比较结果。

复制代码
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
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Date; public class Demo_7 { public static void main(String[] args) throws IOException { File f = new File("D:\日记.txt"); FileInputStream r = new FileInputStream(f); BufferedInputStream buf = new BufferedInputStream(r); System.out.println("使用字节流开始读取文件时间" + new Date()); while (buf.read() != -1) { } System.out.println("使用字节流读取完时间" + new Date()); buf.close(); r.close(); FileReader r2 = new FileReader(f); BufferedReader buf2 = new BufferedReader(r2); System.out.println("使用字符流开始读取时间" + new Date()); while (buf2.read() != -1) { } System.out.println("使用字符流读取完时间" + new Date()); buf2.close(); r2.close(); } }

10-9 编写程序,使用序列流,读取某目录下所有的Java源文件,并统计“class”出现的次数。

复制代码
1
这里写代码片

10-10 编写程序,分别用字符流和缓冲字符流读取一个大文件,并比较它们的效率。

复制代码
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
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Date; public class Dem0_9 { public static void main(String[] args) { try { File f = new File("D:\日记.txt"); FileReader f1 = new FileReader(f); BufferedReader f2 = new BufferedReader(f1); System.out.println("开始时间是" + new Date()); while (f1.read() != -1) { } System.out.println("结束时间是" + new Date()); System.out.println("开始时间是" + new Date()); while (f2.read() != -1) { } System.out.println("结束时间是" + new Date()); f2.close(); f1.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

结论:使用缓冲字符流较快

最后

以上就是慈祥宝马最近收集整理的关于Java基础(十)的全部内容,更多相关Java基础(十)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(70)

评论列表共有 0 条评论

立即
投稿
返回
顶部