
一。file的常用api
二。算法:递归
1.定义:递归算法是把问题转化为规模缩小了的同类问题的子问题。然后递归调用函数(或过程)来表示问题的解。
一个过程(或函数)直接或间接调用自己本身,这种过程(或函数)叫递归过程(或函数).
案例:计算10的阶乘: 10*9*8*7*6*5*4*3*2*1
三。文件处理IO
1.字节流:可以处理任何文件类型
字节输入流
InputStream in = BufferedInputStream FileInputStream File
in.read()
字节输出流
OutputStream os = BufferedOutputStream FileOutputStream File
in.write(byte[])
2.字符流:只能处理文本型文件类型
Reader r = BufferedReader FileReader File
r.readerLine()
Write w = BufferedWrite FileWrite File
w.write
四、作业及练习
1.做一个模拟dos窗口的命令集合:dir和cd功能
dir是显示当前目录下的所有子目录和文件名 还有文件大小
cd是将当前目录进入到指定文件目录下
要求从d盘开始
2.输入一个字符串,要求把字符串从第一个字符每次叠加一个字符输出,用递归算法
例如:“白日依山尽” 输出:白 白日 白日依 白日依山 白日依山尽
3.输入一个文件夹路径,打印输出该文件夹下所有文件路径
testhtmlhtmlindex.htmlindex.htmljavaFirst.javaHelloWorld.Huwa.javapublic void directory(String path){//构造file//拿到目录结构下的所有file//循环file数组,判断如果是文件打印输出,如果是目录则继续调用自己递归
} 4.写一个方法,用来替换文章中的字符串
5.写一个方法,用来统计文章中字符串出现的次数
1.将一篇没有自动换行的文章按规则实现换行。
规则为:句号,分号,省略号,感叹号,疑问号。
遇到以上符号就换行
2.将成绩文档中的所有成绩求平均成绩,最高分,和总人数?
学号,姓名,学科,成绩 1,张安,语文,88 2,刘鹗,语文,67 3,修改,语文,87 4,鳄鱼,语文,55 5,没地方,语文,45 6,大飞哥,语文,66 7,苟富贵,语文,78 8,表单,语文,98 9,放到,语文,78 10,二维热无,语文,99 11,都是,语文,87 12,放到,语文,67 13,的v,语文,56 14,地方,语文,76 15,水电费,语文,87 16,大飞,语文,99 17,同意,语文,89 18,回滚,语文,78 19,交换机,语文,67 20,玩儿,语文,87
五、例子
文件基本操作+dos操作系统模拟实例:
1 package com.demo1120;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.Scanner;
6
7 import org.junit.Test;
8
9 public class FileDemo {
10 @Test
11 public void test(){
12 File file = new File("d:/");
13 try {
14 ateNewFile();
15 } catch (IOException e) {
16 e.printStackTrace();
17 }
18 file.delete();
19 System.out.ists());
20
21 File test = new File("d:/test");
22 File[] files = test.listFiles();
23 for (int i = 0; i < files.length; i++) {
24 System.out.println(files[i].getName());
25 }
26 }
27
28 @Test
29 public void dos(){
30 Scanner sc = new Scanner(System.in);
31 String path = "d:/";
32
33 System.out.println("d:/>");
34 while(true){
35 String command = sc.nextLine();
36 if("dir".equals(command)){
37 File file = new File(path);
38 File[] files = file.listFiles();
39 for (int i = 0; i < files.length; i++) {
40 System.out.println(files[i].getName());
41 }
42 System.out.println(path+">");
43 }
44
45 if(command.startsWith("cd")){
46 path += "/"+command.substring(command.indexOf(" ")+1);
47 File file = new File(path);
48 System.out.println(path+">");
49 }
50 }
51 }
52 } 递归实例:
1 package com.demo1120;
2
3 import org.junit.Test;
4
5 public class RecursionDemo {
6 public static void main(String[] args) {
7 RecursionDemo r = new RecursionDemo();
8 // int result = r.factoril(10);
9 // System.out.println(result);
10 r.string("白日依山尽");
11 }
12
13 //计算阶乘
14 public int factoril(int num){
15 if(num==1){
16 return 1;
17 }else{
18 int result = num*factoril(num-1);
19 System.out.println(result);
20 return result;
21 }
22 }
23
24 public String string(String s){
25 if(s.length()==1){
26 System.out.println(s);
27 return s;
28 }else{
29 String result = s+string(s.substring(0,s.length()-1));
30 System.out.println(s);
31 return result;
32 }
33 }
34 } 打印文件夹下所有文件实例
1 package com.demo1120;
2
3 import java.io.File;
4
5 public class Work {
6 public static void main(String[] args) {
7 Work w = new Work();
8 w.directory("d:/test",0);
9 }
10
11 public void directory(String path,int table){
12 //构造file
13 File file = new File(path);
14 //拿到目录结构下的所有file
15 File[] files = file.listFiles();
16
17
18 //循环file数组,判断如果是文件打印输出,如果是目录则继续调用自己递归
19 for (int i = 0; i < files.length; i++) {
20 for (int j = 0; j < table; j++) {
21 System.out.print("t");
22 }
23 if(files[i].isFile()){
24 System.out.println(files[i].getName());
25 }
26 if(files[i].isDirectory()){
27 System.out.println(files[i].getName());
28 directory(path+"/"+files[i].getName(),table+1);
29 }
30 }
31 }
32 } 读取一个文件中的内容并写入到另一个文件中的实例(字节流):
1 package com.demo1120;
2
3 import java.io.BufferedInputStream;
4 import java.io.BufferedOutputStream;
5 import java.io.File;
6 import java.io.FileInputStream;
7 import java.io.FileNotFoundException;
8 import java.io.FileOutputStream;
9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.io.OutputStream;
12
13 public class StreamDemo {
14
15 public static void main(String[] args) {
16 StreamDemo sd = new StreamDemo();
17 byte[] b = sd.readFile("d:/test/笔记");
18 sd.writeFile("d:/海文", b);
19 }
20 public byte[] readFile(String path){
21
22 File file = new File(path);
23 //把文件读成字节输入流
24 InputStream in = null;
25 byte[] bytes = new byte[(int)file.length()];
26 try{
27 in = new BufferedInputStream(new FileInputStream(file));
28 //从流中把字节内容存入到java内存,字节数组
29 int offset = 0;//记录每次读取的初始位置
30 int num = 0;//记录每次读取的量
31 //循环从流中把字节存储到字节数组中
32 while(offset<bytes.length&&num>=0){
33 num = in.read(bytes, offset, bytes.length-offset);
34 offset = num;
35 }
36 //将字节数组构造成字符创 “gbk”是支持中文的字符集编码
37 System.out.println(new String(bytes, "gbk"));
38 }catch (Exception e) {
39 e.printStackTrace();
40 }finally{
41 try {
42 in.close();//关闭资源
43 } catch (IOException e) {
44 e.printStackTrace();
45 }
46 }
47 return bytes;
48 }
49
50 public void writeFile(String path,byte[] bytes){
51 //字节输出流
52 OutputStream os = null;
53 try {
54 os = new BufferedOutputStream(new FileOutputStream(new File(path)));
55 os.write(bytes);
56 os.flush();//清空缓存,如果不清空有可能有一部分在缓存中无法输出到文件
57 } catch (Exception e) {
58 e.printStackTrace();
59 }finally{
60 try {
61 os.close();
62 } catch (Exception e) {
63 e.printStackTrace();
64 }
65 }
66 }
67 } 读取一个文件中的内容并写入到另一个文件中的实例(字符流):
1 package com.demo1120;
2
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
5 import java.io.File;
6 import java.io.FileNotFoundException;
7 import java.io.FileReader;
8 import java.io.FileWriter;
9 import java.io.IOException;
10 import java.io.Reader;
11 import java.io.Writer;
12
13 /**
14 * 字符流案例
15 */
16 public class CharStreamDemo {
17
18 public static void main(String[] args) {
19 CharStreamDemo cd = new CharStreamDemo();
20 // cd.readFile("d:/test/笔记");
21 cd.writeFile("d:/测试");
22 }
23
24 public String readFile(String path){
25 File file = new File(path);
26 BufferedReader reader = null;
27 StringBuilder sb = new StringBuilder();
28 try {
29 reader = new BufferedReader(new FileReader(file));
30 String s;
31 while((s = adLine())!=null){
32 sb.append(s+"n");
33 }
34 System.out.String());
35 } catch (Exception e) {
36 // TODO Auto-generated catch block
37 e.printStackTrace();
38 } finally{
39 try {
40 reader.close();
41 } catch (IOException e) {
42 e.printStackTrace();
43 }
44 }
45 String();
46 }
47
48 public void writeFile(String path){
49 File file = new File(path);
50 Writer w = null;
51 try {
52 w = new BufferedWriter(new FileWriter(file));
53 w.write("你好啊。rn我不好");//内存输出到文件,换行符是rn
54 w.flush();//清空缓存
55 } catch (IOException e) {
56 e.printStackTrace();
57 } finally {
58 try {
59 w.close();
60 } catch (IOException e) {
61 e.printStackTrace();
62 }
63 }
64 }
65 }
转载于:.html
本文发布于:2024-01-28 00:39:44,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/17063735863600.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
| 留言与评论(共有 0 条评论) |