Java Day 22

Posted zhuzhuqwa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java Day 22相关的知识,希望对你有一定的参考价值。

 1 package com.company.Day022;
 2 
 3 import sun.java2d.pipe.BufferedRenderPipe;
 4 
 5 import java.io.*;
 6 
 7 /**
 8  * Created by junius on 2016/10/24.
 9  */
10 public class InDemo001 {
11     public static void main(String[] args) throws IOException {
12 //        keyread();
13 //        keyread_2();
14 //        System.out.println((int)‘\r‘);
15 //        System.out.println((int)‘\n‘);
16         keyread_3();
17 
18     }
19 
20     private static void keyread_3() throws IOException {
21 /*        InputStream in = System.in;
22         InputStreamReader isr = new InputStreamReader(in);
23         BufferedReader br = new BufferedReader(isr);
24 
25         OutputStream out = System.out;
26         OutputStreamWriter osw = new OutputStreamWriter(out);
27         BufferedWriter bw = new BufferedWriter(osw);*/
28 
29         BufferedReader bufr = new BufferedReader(new InputStreamReader(new FileInputStream("demo.txt")));
30         BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("c.txt")));
31 
32         String line = null;
33         while((line=bufr.readLine())!=null){
34             if("over".equals(line))
35                 break;
36 //            System.out.println(line.toUpperCase());
37             bufw.write(line.toUpperCase());
38             bufw.newLine();
39             bufw.flush();
40         }
41     }
42 
43     private static void keyread_2() throws IOException {
44         /*
45         * 获取键盘录入
46         * 1、键盘只读取一个字节,要判断是否是over
47         * 2、那就需要一个容器
48         * 3、在用户回车之前,将字节变成字符串判断
49         * */
50         //创建容器
51         StringBuilder sb = new StringBuilder();
52 
53         //获取键盘读取流
54         InputStream in = System.in;
55 
56         //定义变量记录读取到的字节,并循环
57         int ch = 0;
58         while((ch=in.read())!=-1){
59             //判断换行标记
60             if(ch==‘\r‘)
61                 continue;
62             if(ch==‘\n‘) {
63                 String temp = sb.toString();
64                 if ("over".equals(temp))
65                     break;
66                 System.out.println(temp.toUpperCase());
67                 sb.delete(0,sb.length());
68             }else{
69                 sb.append((char)ch);
70 //                System.out.println(ch);
71 
72             }
73 
74         }
75 
76     }
77 
78 
79     private static void keyread() throws IOException {
80         InputStream in = System.in;
81         int ch = in.read();
82         System.out.println(ch);
83         int ch1 = in.read();
84         System.out.println(ch1);
85         int ch2 = in.read();
86         System.out.println(ch2);
87         //流只有一个,不能关闭
88 //        in.close();
89     }
90 }

 

转换流
InputStreamReader 解码 字节到字符
OutputStreamWriter 编码 字符到字节

1、明确源和目的
   源:InputStream Reader
   目的:OutputStream Writer

2、明确数据是否使用纯文本数据
   源:是纯文本 Reader
 否 InputStream
   目的:是纯文本 Writer
 否 OutputStream

3、明确具体的设备
   源
  硬盘:File
 键盘:System.in
 内存:数组
 网络:Socket流
   目的设备
 硬盘
 控制台 System.out
 内存
 网络

4、是否需要其他额外功能
   1、是否需要高效(缓冲区)buffer
   2、转换 字节 字符

转换流的编码解码
 什么时候使用转换流
 1、源或目的对应的设备是字节流,但是操作的却是文本数据,
 2、一旦操作文本涉及到具体的指定编码表,必须使用转换流

 1 package com.company.Day022;
 2 
 3 import javax.annotation.processing.Filer;
 4 import java.io.*;
 5 
 6 /**
 7  * Created by junius on 2016/10/25.
 8  * 1、字节->字符
 9  * InputStream in = System.in;
10  * FileWriter fw = new FileWriter("a.txt");
11  *
12  * InputStreamReader isr = new InputStreamReader(System.in)
13  * FileWriter fw = new FileWriter("a.txt");
14  *
15  *
16  * 2、字符->字节
17  * FileRead fr = new FileRead("b.txt");
18  * OutputStream out = System.out;
19  *
20  * FileRead fr = new FileRead("b.txt");
21  * OutputStreamWriter osw = new OutputStreamWriter(System.out);
22  *
23  * 3、字符->字符
24  * FileRead fr = new FileRead("c.txt");
25  * FileWriter fw = new FileWriter("d.txt");
26  *
27  * BufferedReader bufr = new BufferedReader(new FileRead("c.txt"))
28  * BufferedWriter bufw = new BufferedWriter(new FileWrite("d.txt"))
29  *
30  * 4、字节->字节
31  * InputStream in = System.in;
32  * OutputStream out = System.out;
33  */
34 public class Demo002 {
35     public static void main(String[] args) throws IOException {
36 //        writeText();
37 //        writeText_2();
38 //        writeText_3();
39 //        readText_1();
40         readText_2();
41     }
42 
43     private static void readText_2() throws IOException {
44         InputStreamReader isr = new InputStreamReader(new FileInputStream("U8_1.txt"),"UTF-8");
45         char[] buf = new char[10];
46         int len = isr.read(buf);
47         String str = new String(buf, 0, len);
48         System.out.println(str);
49         isr.close();
50     }
51 
52 
53     private static void readText_1() throws IOException {
54         FileReader fr = new FileReader("gbk_1.txt");
55 
56         char[] buf = new char[10];
57         int len = fr.read(buf);
58         String str = new String(buf, 0, len);
59         System.out.println(str);
60         fr.close();
61 
62     }
63 
64     private static void writeText_3() throws IOException {
65         OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("U8_1.txt"),"UTF-8");
66         osw.write("你好");
67         osw.close();
68     }
69 
70     private static void writeText_2() throws IOException {
71         OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("gbk_2.txt"),"GBK");
72         osw.write("你好");
73         osw.close();
74     }
75 
76     private static void writeText() throws IOException {
77         FileWriter fw = new FileWriter("gbk_1.txt");
78         fw.write("你好");
79         fw.close();
80     }
81 }

 

File对象--构造函数和字段
 文件和文件夹封装成对象
 文件属性
 
方法--获取、创建、删除

判断

重名名 renameTo 剪切+rename

系统根目录和容量获取
 
获取目录内容
 如果访问的不是目录,会发生异常

 1 package com.company.Day022;
 2 
 3 
 4 import java.io.File;
 5 import java.io.FileFilter;
 6 import java.io.IOException;
 7 import java.text.DateFormat;
 8 import java.util.Date;
 9 
10 /**
11  * Created by junius on 2016/10/26.
12  */
13 public class FileDemo003 {
14     public static void main(String[] args) throws IOException {
15 //        conDemo();
16 //        function_1();
17 //        function_2();
18 //        function_3();
19 //        function_4();
20         function_5();
21     }
22 
23     private static void function_5() {
24         File f1 = new File("D:\\java\\JavaSE基础视频22\\IO流");
25         String[] names = f1.list(new Suffix(".avi"));
26         for (String name:names){
27             System.out.println(name);
28         }
29 
30     }
31 
32     private static void function_4() {
33         File file = new File("D:\\java");
34         String[] names = file.list();
35         for(String name:names){
36             System.out.println(name);
37         }
38     }
39 
40     private static void function_3() {
41         File[] files = File.listRoots();
42         for (File file:files) {
43             System.out.println(file);
44 
45         }
46     }
47 
48     private static void function_2() throws IOException {
49         File f1 = new File("ab");
50         f1.mkdir();
51 //        f1.delete();
52     }
53 
54     private static void function_1() {
55         File file = new File("gbk_1.txt");
56         String name = file.getName();
57         String path = file.getPath();
58         String absPath = file.getAbsolutePath();
59         long len = file.length();
60         long time = file.lastModified();
61         Date date = new Date(time);
62         DateFormat datemate = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG) ;
63         String str_time = datemate.format(date);
64         System.out.println("name:"+name);
65         System.out.println("path:"+path);
66         System.out.println("absPath:"+absPath);
67         System.out.println("len:"+len);
68         System.out.println("time:"+time);
69         System.out.println("str_time:"+str_time);
70 
71     }
72 
73     private static void conDemo() {
74         File file = new File("d:\\a.txt");
75         File file2 = new File("d:\\","b.txt");
76         File file3 = new File("c:\\");
77         File file4 = new File(file3, "c.txt");
78 
79         File f4 = new File("d:"+System.getProperty("file.separator")+"abc"+File.separator+"e.txt");
80         System.out.print(f4);
81 
82     }
83 }

 

过滤器
 

 1 package com.company.Day022;
 2 
 3 import java.io.File;
 4 import java.io.FileFilter;
 5 import java.io.FilenameFilter;
 6 import java.util.logging.Filter;
 7 
 8 /**
 9  * Created by junius on 2016/10/27.
10  */
11 public class FilterByTxt implements FilenameFilter {
12     @Override
13     public boolean accept(File pathname,String name) {
14         return name.endsWith(".txt");
15     }
16 }
17 package com.company.Day022;
18 
19 import java.io.File;
20 import java.io.FilenameFilter;
21 
22 /**
23  * Created by junius on 2016/10/27.
24  */
25 public class Suffix implements FilenameFilter {
26     private String suffix;
27 
28     public Suffix(String suffix) {
29         this.suffix = suffix;
30     }
31 
32     @Override
33     public boolean accept(File dir, String name) {
34         return name.endsWith(suffix);
35     }
36 }

 


 

 

































以上是关于Java Day 22的主要内容,如果未能解决你的问题,请参考以下文章

22.java方法的定义

22年最新Java笔记:day7-Java面向对象

创建片段而不从 java 代码实例化它

22年最新Java笔记:day5-Java流程控制语句

Day22-JSP与Filter

22年最新Java笔记:day6-Java基础知识综合练习题