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 }
时间: 2024-10-13 16:10:29

Java Day 22的相关文章

Java笔记(22):IO流(04)

1.登录注册案例IO版实现 在笔记17中的登录注册代码中,只需要修改注册登录的实现类 1 package cn.itcast.dao.impl; 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.

java之 22天 GUI 图形界面编程(一)

转自:http://takeme.iteye.com/blog/1876850 GUI(图形用户界面) Java代码 1 import java.awt.Button; 2 import java.awt.FlowLayout; 3 import java.awt.Frame; 4 import java.awt.event.WindowAdapter; 5 import java.awt.event.WindowEvent; 6 7 /** 8 * GUI(图形用户界面) 9 * Graphi

java之 22天 GUI 图形界面编程(二)

转自:http://takeme.iteye.com/blog/1876853 模拟window开打文件 Java代码 1 import java.awt.Button; 2 import java.awt.Dialog; 3 import java.awt.FlowLayout; 4 import java.awt.Frame; 5 import java.awt.Label; 6 import java.awt.TextArea; 7 import java.awt.TextField; 8

java 多线程 22 :生产者/消费者模式 进阶 利用await()/signal()实现

java多线程15 :wait()和notify() 的生产者/消费者模式 在这一章已经实现了  wait/notify 生产消费模型 利用await()/signal()实现生产者和消费者模型 一样,先定义一个缓冲区: public class ValueObject { public static String value = ""; } 换种写法,生产和消费方法放在一个类里面: public class ThreadDomain41 extends ReentrantLock {

最佳新秀Java(22)——再次了解泛型

仿制药Java SE 1.5新功能.通用自然是参数化类型.即操作数据类型被指定为一个参数.这样的参数类型可以在课堂上使用.创建的接口和方法,他们被称为通用类..泛型方法. Java语言引入泛型的优点是安全简单. 规则和限制 1.泛型的类型參数仅仅能是类类型(包含自己定义类),不能是简单类型.2.同一种泛型能够相应多个版本号(由于參数类型是不确定的),不同版本号的泛型类实例是不兼容的.3.泛型的类型參数能够有多个.4.泛型的參数类型能够使用extends语句.比如<T extends superc

java基础---22. 发红包案例

1 题目 2 思路 3 代码事项 用户类 public class User { private String name; private int money; public User() { } public User(String name, int money) { this.name = name; this.money = money; } //展示一下用户有多少钱 public void show(){ System.out.println("我叫:"+ name + &q

深入浅出 Java Concurrency (22): 并发容器 part 7 可阻塞的BlockingQueue (2)[转]

在上一节中详细分析了LinkedBlockingQueue 的实现原理.实现一个可扩展的队列通常有两种方式:一种方式就像LinkedBlockingQueue一样使用链表,也就是每一个元素带有下一个元素的引用,这样的队列原生就是可扩展的:另外一种就是通过数组实现,一旦队列的大小达到数组的容量的时候就将数组扩充一倍(或者一定的系数倍),从而达到扩容的目的.常见的ArrayList就属于第二种.前面章节介绍过的HashMap确是综合使用了这两种方式. 对于一个Queue而言,同样可以使用数组实现.使

Java [leetcode 22]Generate Parentheses

题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 解

Java基础知识强化之集合框架笔记59:Map集合之TreeMap(TreeMap&lt;String,String&gt;)的案例

1. TreeMap类的概述: 键是红黑树结构,可以保证键的排序和唯一性. 2. TreeMap案例: TreeMap<String, String> 代码示例: 1 package cn.itcast_04; 2 3 import java.util.Set; 4 import java.util.TreeMap; 5 6 /* 7 * TreeMap:是基于红黑树的Map接口的实现. 8 * 9 * HashMap<String,String> 10 * 键:String 11