JAVA再学习1

学习资料

Java从入门到精通(第二版)
课后实战练习答案可见
https://blog.csdn.net/weixin_39525865/article/details/114191482
(非常感谢博主,全网很难找代码资源)

io 输出流

java.io.PrintWriter 与PrintStream

问题发现:
在java网络程序设计中,服务器端和客户端之间的通信,可能用到这两个包

贴一段官方的解释

A PrintStream adds functionality to another output stream, namely the ability to print representations of various data values conveniently. Two other features are provided as well. Unlike other output streams, a PrintStream never throws an IOException; instead, exceptional situations merely set an internal flag that can be tested via the checkError method. Optionally, a PrintStream can be created so as to flush automatically; this means that the flush method is automatically invoked after a byte array is written, one of the println methods is invoked, or a newline character or byte ('\n') is written. 

All characters printed by a PrintStream are converted into bytes using the platform's default character encoding. The PrintWriter class should be used in situations that require writing characters rather than bytes.

也就是说:

PrintStream继承自OutputStream,属于字节流的一种,方法包含写入单个字节和字节数组的方法
相似流有PrintWriter,继承自Writer()方法,属于字符流的一种.PrintWriter流中没有写入字节的方法,而有写入单个字符和字符数组的方法.

具体:
PrintStream流中基本所有的print(Object obj)重载方法和println(Object obj)重载方法都是通过将对应数据先转换成字符串,然后调用write()方法写到底层输出流中.常见用到PrintStream流:System.out就被包装成PrintStream流,System.err也是PrintStream流,注意System.in不是PrintStream,是没有包装过的InputStream.所以System.in不能直接使用.在从字节流到文件这个过程中,一般使用中间流OutputStreamWriter转换。

PrintStream <->BufferedWriter<->OutputStreamWriter<->FileOutputStream<->文件

注:
1.由于PrintStream流内部做了try{}catch(){},会将异常捕获,因此在使用时永远不会抛出异常.出现异常情况会在内部设置标识,通过checkError()获取此标识.
2.PrintStream流有自动刷新机制,例如当向PrintStream流中写入一个字节数组后自动调用flush()方法.

基础知识

枚举类

问题发现:
枚举类的使用注意

1.enum前没有修饰符,即访问权限为默认类型,则enum定义的对象可在同一个包内访问,无需重复定义。其他需要用到该枚举类的文件可以通过import导入复用该定义。
2.如果enum访问权限为public,则需要单独定义为 .java文件,不可与包含main方法的public类同处于一个文件
3.使用enum定义的枚举类,默认使用final修饰,无法派生子类,默认继承于java.lang.Enum类
4.使用enum定义的枚举类,其所有枚举值(该类的实例)必须在枚举类的第一行声明,否则无法产生实例。系统自动加上 public static final 修饰
5.在枚举中定义了抽象方法,则每个枚举对象都必须单独实现该方法

//第一种方法
enum MyColor {红色,绿色,蓝色};
//之后是操作代码块...

//第二种方法
enum MyColor{
	//枚举类定义的对象必须出现在该类有效代码第1行
	RED("红色",4),GREEN("绿色",5),BLUE("蓝色",6);
	//然后为构造函数、及有关于index 和name的set、get方法(考虑是否有参数传入)
}

5.所有使用enum定义的枚举类,系统会隐式提供**values()**方法,用来访问枚举值。

enum MyColor {红色,绿色,蓝色};

//enum
MyColor red = MyColor.红色;

//enum array
MyColor[]  allcolor = MyColor.values(); //读取枚举中的值
for(MyColor acolor : allcolor) System.out.println(acolor.name() + "-->" + acolor.ordinal()); //不同对象的名称和编号

//enummap 需要import java.util.Map 和 EnumMap
//.class提供了Enum对象的元信息,包括枚举值数量。EntrySet返回整个映射实例(entry)的集合
EnumMap<MyColor,String> eMap = new EnumMap<MyColor,String>(MyColor.class);
eMap.put(MyColor.绿色,“Green”);
for(Map.Entry<MyColor,String> ee: eMap.entrySet())
	{System.out.println(ee.getKey() + "-->" + ee.getValue());}
	
//enumset import java.util.EnumSet 和 Iterator
EnumSet<MyColor> eSet = EnumSet.allOf(MyColor.class);
Iterator<MyColor> eIt = eSet.iterator();
while(elt.hasNext())   System.out.println(elt.Next();




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值