目录
2 、文件节点类: FileInputStream 和 FileReader
3 、缓冲处理类:BufferedInputStream 和 BufferedReader
4 、转换处理流: InputStreamReader(OutputStreamWriter)
2 、文件节点类: FileOutputStream 和 FileWriter
3 、缓冲处理流:BufferedOutputStream 和 BufferedWriter
一、IO流
1. 流概念
- 源头与目的地之间的流动,在java语言中即从 java 内存流动到存储介质中。
- 储存介质包括:硬盘文件、数据库与网络等节点(数据源)。记住:一切以java内存为中心。
- 流是一个抽象、动态的概念,是一连串连续动态的数据集合。
2. 数据源
提供原始数据的原始媒介。数据源就像水箱,流就像水管中流着的水流,程序就是我们最终的用户。
3. IO流分类
分类 | 字节流 | 字符流 | |||
抽象父流 | InputStream | OutputStream | Reader | Writer | |
节点流 | 文件 | FileInputStream | FileOutputStream | FileReader | Filewriter |
数组 | ByteArrayInputStream | ByteArrayOutputStream | CharArrayReader | CharArrayWriter | |
字符串 | StringReader | StringWriter | |||
管道 | PipedInputStream | PipedOutputStream | PipedReader | PipedWriter | |
处理流 | 缓冲流 | BufferedInputStream | BufferedOutputStream | BufferedReader | BufferedWriter |
转换流 | InputStreamReader | OutpuStreamWriter | |||
对象流 | ObjectInputStream | ObjectOutputStream | |||
数据流 | DateInputStream | DateOutputStream | |||
打印流 | PrintStream |
(1)按流向分类:
- 流入流:从节点到 java 内存叫输入流,基类为 InputStream 和 Reader
- 流出流:从 java 内存到节点叫输出流,基类为 OutputStream 和 Writer
(2)按数据分类:
- 字节流:可以操作一切内容、包括:文本、图片、音频、视频、doc等,基类为 InputStream 和 OutputStream,数据单元为8位。
- 字符流:仅能处理文本,基类为 Reader 和 Writer,数据单位为16位。
(3)按功能分类:
- 节点流:从/向一个特定的 I/0 设备(磁盘、网络等)读写数据的流称为节点流,也常被称为低级流。
- 处理流:处理流则对于一个已存在的节点流进行连接或封装,常被称为高级流(装饰器设计模式)。处理流为增强、提升性能的,本身不具备直接操作节点的能力。
- 处理流的功能主要体现在:
a、性能的提高:主要以增加缓冲的方式来提高输入/输出的效率 ;
b、操作的便捷:提供了系列便捷的方法来一次输入/输出大批量内容
4. IO的操作步骤
- 建立联系
- 选择流
- 操作:写出 读取
- 释放资源(程序中打开的文件 IO 资源不属于内存中的资源,垃圾回收无法回收,需要显示关闭。)
二、流入流
字节流和字符流的操作方式几乎完全一样,只是操作的数据单元不同而已 。字节流可以操作所有文件,字符流仅操作纯文本。
1 、抽象类:InputStream 和 Reader
InputStream 和 Reader 是所有输入流的基类,它们是两个抽象类,是所有输入流的模版,其中定义的方法在所有输入流中都可以使用。
(1)InputStream(字节流入) 类中包含如下方法:
方法摘要 | |
---|---|
int | available() 返回此输入流下一个方法调用可以不受阻塞地从此输入流读取(或跳过)的估计字节数。 |
void | close() 关闭此输入流并释放与该流关联的所有系统资源。 |
void | mark(int readlimit) 在此输入流中标记当前的位置。 |
boolean | markSupported() 测试此输入流是否支持 mark 和 reset 方法。 |
abstract int | read() 从输入流中读取数据的下一个字节。 |
int | read(byte[] b) 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。 |
int | read(byte[] b, int off, int len) 将输入流中最多 len 个数据字节读入 byte 数组。 |
void | reset() 将此流重新定位到最后一次对此输入流调用 mark 方法时的位置。 |
long | skip(long n) 跳过和丢弃此输入流中数据的 n 个字节。 |
(2)在 Reader(字符流入) 类中包含如下方法:
法摘要 | |
---|---|
abstract void | close() 关闭该流并释放与之关联的所有资源。 |
void | mark(int readAheadLimit) 标记流中的当前位置。 |
boolean | markSupported() 判断此流是否支持 mark() 操作。 |
int | read() 读取单个字符。 |
int | read(char[] cbuf) 将字符读入数组。 |
abstract int | read(char[] cbuf, int off, int len) 将字符读入数组的某一部分。 |
int | read(CharBuffer target) 试图将字符读入指定的字符缓冲区。 |
boolean | ready() 判断是否准备读取此流。 |
void | reset() 重置该流。 |
long | skip(long n) 跳过字符。 |
注意:返回结果为-1 时表明到了输入流的结束点。 InputStream 和 Reade 都是抽象的,不能直接创建它们的实例,可以使用它们的子类。
2 、文件节点类: FileInputStream 和 FileReader
FileInputStream 和 FileReader,它们都是节点流,直接和指定文件关联。 操作方式基本一致。
(1)单个字节读取
import java.io.*;
/*
* 输出流 OutputStream
* 每次值输出一个数据 write(int)
* 每次只输出一个,不符合需求
*/
public class Demo03 {
public static void main(String[] args){
//1.选择流 只是联系
OutputStream out=null;
try {
out=new FileOutputStream("d:/test.txt");
//2.操作
//定义写出数据
int num=97;
//写出
out.write(num);
out.write(98);
//3.强制刷出
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
//4.关闭
try {
if(out!=null){
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
(2)批量读取(字节|字符重点)
import java.io.*;
/*
* 输入流 InputStream
* 解决一次能够输出多个数据 write(byte[])
* 注意:
* 1.如果目标文件不存在,会自动为你创建
* 2.如果目标文件夹不存在,不会为你自动创建,会抛异常找不到路径
* 3.每次运行输出的内容会进行覆盖,如果想要追加,可以设置第二个参数,如果是true则追加,如果是false
是覆盖,默认false
*/
public class Demo04 {
public static void main(String[] args){
//1.选择流 只是联系
OutputStream out=null;
try {
//每次运行输出的内容会进行覆盖,如果想要追加,可以设置第二个参数,如果是true则追加,如果是false是覆盖,默认false
out=new FileOutputStream("d:/haha/haha.txt",false);
//2.操作
//定义写出数据
String str="我们都在爱情里少一点天份...才发现跌跌撞撞满身伤痕...123";
byte[] by=str.getBytes();
//写出
out.write(by);
//3.强制刷出
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
//4.关闭
try {
if(out!=null){
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3 、缓冲处理类:BufferedInputStream 和 BufferedReader
作用:缓冲提高性能,直接套上即可;
- 字节缓冲流没有新增方法,可以多态调用。
- 字符缓冲流 有新增方法,多态调用对子类新增的方法不可见。readLine()和 newLine()
- readLine():读取一行,返回读取到的字符串, 否则返回false
- newLine():写入一个分隔符
(1)字节缓冲流 BufferedInputStream(BufferedOutputStream)
import java.io.*;
/*
* 字节缓存流
输入:BufferedInputStream
输出:BufferedOutputStream
没有新增方法,可以多态调用
使用和InputSteam一样
*/
public class BufferedInputStreamDemo11 {
public static void main(String[] args) {
//1,建立联系
File src=new File("src/copy.txt");
File dest=new File("src/new.txt");
//2.选流
InputStream is=null;
OutputStream os=null;
try {
is=new BufferedInputStream(new FileInputStream(src));
os=new BufferedOutputStream(new FileOutputStream(dest));
//3.操作
int len=0;
byte[] by=new byte[1024];
while((len=is.read(by))!=-1){
os.write(by,0,len);
}
//4.强制刷出
os.flush();
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
//关闭
if(os!=null){
os.close();
}
if(is!=null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
(2)字符缓冲流 BufferedReader(BufferedWriter)
/*
* 字符缓冲流
输入:BufferedReader readLine() 读取一行,返回读取到的字符串 ,否则返回false
输出:BufferedWriter newLine() 写入一个行分隔符。
有新增方法,多态调用对子类新增的方法不可见
*/
public class BufferedReaderDemo12 {
public static void main(String[] args) {
//1,建立联系
File src=new File("src/copy.txt");
File dest=new File("src/newchar.txt");
//2.选流
BufferedReader is=null;
BufferedWriter os=null;
try {
is=new BufferedReader(new FileReader(src));
os=new BufferedWriter(new FileWriter(dest,true));
//3.操作
int len=0;
String mes=null;
char[] by=new char[1024];
/*while((len=is.read(by))!=-1){
os.write(by,0,len);
}*/
os.newLine();
while((mes=is.readLine())!=null){
os.write(mes);
}
//4.强制刷出
os.flush();
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
//关闭
if(os!=null){
os.close();
}
if(is!=null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4 、转换处理流: InputStreamReader(OutputStreamWriter)
转换流:将字节流转为字符流,作用是用来处理乱码(编码集、解码集)。
import java.io.*;
/*
* InputStreamReader(inputStream,charset)----Reader
OutputStreamWriter(OutputStream,charset)---Writer
*/
public class ConvertDemo14 {
public static void main(String[] args){
//1.建立联系
//2.选择流
BufferedReader rd=null;
BufferedWriter rt=null;
try {
rd=new BufferedReader(new InputStreamReader(new BufferedInputStream(new FileInputStream("src/char.txt")),"utf-8"));
rt=new BufferedWriter(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream("src/newnewchar.txt")),"utf-8"));
//3.操作
String mes=null;
while((mes=rd.readLine())!=null){
rt.write(mes);
}
//4.强制刷出
rt.flush();
} catch (UnsupportedEncodingException | FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
rt.close();
rd.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
三、输出流
1 、抽象类:OutputStream 和 Writer
OutputStream 和 Writer 非常相似。
(1)OutputStream(字节输出)类中包含如下方法:
方法摘要 | |
---|---|
void | close() 关闭此输出流并释放与此流有关的所有系统资源。 |
void | flush() 刷新此输出流并强制写出所有缓冲的输出字节。 |
void | write(byte[] b) 将 b.length 个字节从指定的 byte 数组写入此输出流。 |
void | write(byte[] b, int off, int len) 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。 |
abstract void | write(int b) 将指定的字节写入此输出流。 |
(2)在 Writer 中, 因为字符流直接以字符作为操作单位,所以 Writer 可以用字符串来代替字符数组,即以 String 对象来作为参数。Writer (字符输出)类中包含如下方法:
方法摘要 | |
---|---|
Writer | append(char c) 将指定字符添加到此 writer。 |
Writer | append(CharSequence csq) 将指定字符序列添加到此 writer。 |
Writer | append(CharSequence csq, int start, int end) 将指定字符序列的子序列添加到此 writer.Appendable。 |
abstract void | close() 关闭此流,但要先刷新它。 |
abstract void | flush() 刷新该流的缓冲。 |
void | write(char[] cbuf) 写入字符数组。 |
abstract void | write(char[] cbuf, int off, int len) 写入字符数组的某一部分。 |
void | write(int c) 写入单个字符。 |
void | write(String str) 写入字符串。 |
void | write(String str, int off, int len) 写入字符串的某一部分。 |
2 、文件节点类: FileOutputStream 和 FileWriter
FileOutputStream 和 FileWriter,它们都是节点流,直接和指定文件关联。结合输入输出流,可以实现文件拷贝。
在此之前,因为需要重复使用到文件的拷贝功能,所以我们把文件的拷贝功能封装成一个工具类,在后面使用字节进行拷贝时便于直接调用,详见代码。
(1)文件拷贝工具类
import java.io.*;
/*
* 文件拷贝工具类
*/
public class FileUtil06 {
/**
*
* @param srcfile 数据源 字符串
* @param destfile 目的地字符串
*/
public static void copyFile(String srcfile,String destfile){
copyFile(new File(srcfile),new File(destfile));
}
/**
*
* @param srcfile 数据源对象
* @param destfile 目的地对象
*/
public static void copyFile(File srcfile,File destfile){
//2.选择流
InputStream is=null;
OutputStream os=null;
try {
is=new FileInputStream(srcfile);
os=new FileOutputStream(destfile,true);
//3.操作 读入
//字节数组
byte[] car=new byte[1024];
int len=0; //长度
while((len=is.read(car))!=-1){
os.write(car,0,len);
}
//4.刷出
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
//5.先打开的后关闭
try {
if(os!=null){
os.close();
}
if(is!=null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
(2) 使用字节流可以同时拷贝文件夹和文件
import java.io.File;
import java.io.IOException;
/*
* 使用字节流拷贝文件夹:
* 1.拷贝文件---FileUtil08.copyFile()
* 2.递归
*/
public class CopyDir07 {
public static void main(String[] args) {
try {
copyDir("F:/haha", "F:/haha/heihei/");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 不允许拷贝在当前路径下...
*
* @param src
* @param dest
* @throws IOException
*/
public static void copyDir(String src, String dest) throws IOException {
// 不允许拷贝在当前路径下...
int index = src.lastIndexOf("/");
String a = src.substring(0, index); // [0,index)
if (a.equals(dest) || (a + "/").equals(dest)) {
throw new IOException("不允许拷贝在当前路径下");
}
File srcFile = new File(src);
File destFile = new File(dest);
copyDir(srcFile, destFile);
}
/**
* 不允许拷贝在自己的子目录下
*
* @param srcFile
* 原文件对象
* @param destFile
* 目标文件对象
* @throws IOException
*/
public static void copyDir(File srcFile, File destFile) throws IOException {
// 不允许拷贝在自己的子目录下
// 创建完整的目标文件的对象 拼接路径 destFile(d:/haha/heihei/)+文件名(haha)
File dest = new File(destFile, srcFile.getName());
if (dest.getAbsolutePath().contains(srcFile.getAbsolutePath())) {
throw new IOException("不允许拷贝在自己的子目录下");
}
copy(srcFile, dest);
}
/**
* 开始拷贝
*
* @param srcFile
* @param destFile
*/
public static void copy(File srcFile, File destFile) {
if (srcFile.isFile()) {
// 判断目标文件路径中是否存在没有的文件夹,如果有,就创建
destFile.getParentFile().mkdirs();
// 调用工具类进行文件拷贝
FileUtil06.copyFile(srcFile, destFile);
} else if (srcFile.isDirectory()) {
// 创建文件夹
destFile.mkdirs();
// 遍历源文件夹中的所有内容,进行判断拷贝
for (String s : srcFile.list()) {
copy(new File(srcFile, s), new File(destFile, s));
}
}
}
}
(3) 使用字符流拷贝只能拷贝文件
import java.io.*;
/*
* 使用字符流做文件拷贝...
*/
public class CopyDemo10 {
public static void main(String[] args) {
// 1.建立联系
File src = new File("src/char.txt"); // 数据源
File dest = new File("src/copy.txt"); // 目标
// 2.选择流
Reader rd = null;
Writer wr = null;
try {
rd = new FileReader(src);
wr = new FileWriter(dest);
// 3.操作
char[] car = new char[1024];
int len = 0;
while ((len = rd.read(car)) != -1) {
wr.write(car, 0, len);
}
// 强制刷出
wr.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4.关闭
try {
wr.close();
rd.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3 、缓冲处理流:BufferedOutputStream 和 BufferedWriter
上面流入流中已经阐述。
4 、转换处理流:OutputStreamWriter
上面流入流中已经阐述。
四、IO流其他
1.字节数组节点类
(1)特点
字节数组节点流操作的节点为字节数组,数组在JVM内存中,由垃圾回收机制管理,不需要手动关闭。
字节数组节点流属于节点流的一种,根据输入和输出分为ByteArrayInputStream的和ByteArrayOutputStream
- 输入流:ByteArrayInputStream进行的
没有新增方法,使用和InputStream中的一样。
- 输出流:ByteArrayOutputStream
有新增方法,不能够多态使用.byte [] toByteArray()字节数组流转为字节数组
(2)基本操作
public static void main(String[] args) throws IOException {
read("留我在原地,一脸疑问,一脸懵逼...".getBytes());
System.out.println(new String(write()));
}
//写出 字节数组 从本地道服务器
public static byte[] write() throws IOException {
//定义目的地
byte[] dest;
//2.定义流
ByteArrayOutputStream by=new ByteArrayOutputStream();
by.write("我的心里只有你没有他...".getBytes());
dest=by.toByteArray();
//强制刷出
by.flush();
by.close();
return dest;
}
//读入
public static void read(byte[] b) throws IOException{
//1.创建流 从另一台接受一个字节数组
InputStream in=new ByteArrayInputStream(b);
//2.操作
byte[] by=new byte[1024];
int len=0;
String s=null;
while((len=in.read(by))!=-1){
s=new String(by,0,len);
}
System.out.println(s);
//可以不去关闭,因为不是操作文件的流,没有方法体
in.close();
}
(3)通过字节数组进行文件的读写
操作过程:文件--->程序--->数组字节
字节数组--->程序--->文件
public static void main(String[] args) throws IOException {
byte[] b=toByteArray("d://shsxtgood.txt");
read(b,"d://abc.txt");
}
//文件-----程序-----字节数组
public static byte[] toByteArray(String src) throws IOException{
//1.创建联系
File file=new File(src);
byte[] dest;
//2.选择流
InputStream in=new BufferedInputStream(new FileInputStream(file));
ByteArrayOutputStream out=new ByteArrayOutputStream();
byte[] car=new byte[1024];
int len=0;
while((len=in.read(car))!=-1){
out.write(car,0,len);
}
dest=out.toByteArray();
//强制刷出
//关闭
out.close();
in.close();
return dest;
}
//字节数组---程序----文件
public static void read(byte[] b,String dest) throws IOException{
//1.创建流
InputStream in=new BufferedInputStream(new ByteArrayInputStream(b));
OutputStream out=new BufferedOutputStream(new FileOutputStream(dest));
byte[] car=new byte[1024];
int len=0;
while((len=in.read(car))!=-1){
out.write(car, 0, len);
}
//强制刷出
out.flush();
//关闭
out.close();
in.close();
}
2.数据处理流
(1)特点
可以处理基本类型+字符串,保留数据的类型。前提是读取顺序与写出顺序一致,否则读取数据不正确。
- 输入流:DataInputStream --->新增方法:readXXX()
- 输出流:DataOutputStream --->新增方法:writeXXX()
- 有新增方法,无法多态调用。
- EOFException类文件存在,但是内容没有读到,只能够读取到写出的源文件中,否则就算输内容相同的其他文件会跑异常
(2)基本操作
public static void main(String[] args) throws IOException {
// write("d://dce.txt");
read("d://dce2.txt");
}
//读入
public static void read(String src) throws IOException{
//1.创建流
DataInputStream in=new DataInputStream(new BufferedInputStream(new FileInputStream(src)));
byte[] car=new byte[1024];
int len=0;
int i=in.readInt();
long l=in.readLong();
String s=in.readUTF();
boolean b=in.readBoolean();
in.close();
System.out.println(i+"-->"+l+"-->"+s+"-->"+b);
}
//写出
public static void write(String dest) throws IOException{
//1.创建流
DataOutputStream out=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dest)));
//2.创建数据
int i=5;
long l=333;
String s="哈哈";
boolean b=true;
out.writeInt(i);
out.writeLong(l);
out.writeUTF(s);
out.writeBoolean(b);
//强制刷出
//out.flush();
//关闭
out.close();
}
(3)基本数据类型流通过字节数组流读写文件
public static void main(String[] args) throws IOException {
writeFile("d:/hhhhhhhhhhhh.txt");
readByteArray(toByteArray("d:/hhhhhhhhhhhh.txt"));
System.out.println(new String(toByteArray("d:/hhhhhhhhhhhh.txt")));
}
//从字节数组中读到程序
public static void readByteArray(byte[] b) throws IOException{
//创建流
DataInputStream in=new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(b)));
System.out.println(in.readInt());
System.out.println(in.readLong());
System.out.println(in.readUTF());
System.out.println(in.readBoolean());
in.close();
}
//从文件中读到程序--写出到字节数组
public static byte[] toByteArray(String srcStr) throws IOException{
//创建流
DataInputStream in=new DataInputStream(new BufferedInputStream(new FileInputStream(new File(srcStr))));
ByteArrayOutputStream by=new ByteArrayOutputStream();
DataOutputStream out=new DataOutputStream(new BufferedOutputStream(by));
byte[] car=new byte[1024];
int len=0;
//目的地
byte[] dest;
out.writeInt(in.readInt());
out.writeLong(in.readLong());
out.writeUTF(in.readUTF());
out.writeBoolean(in.readBoolean());
out.flush();
dest=by.toByteArray() ;
in.close();
return dest;
}
//输出到文件
public static void writeFile(String destStr) throws IOException{
//1.元数据
int i=5;
long l=333;
String s="哈哈";
boolean b=true;
//.创建流
DataOutputStream out=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(new File(destStr))));
out.writeInt(i);
out.writeLong(l);
out.writeUTF(s);
out.writeBoolean(b);
//强制刷出
out.flush();
//关闭
out.close();
}
3.对象处理流
(1)特点
- 引用数据类型流|对象流数据类型+数据
- 序列化输出流ObjectOutputStream --- writeObject()
- 反序列化输入流ObjectInputStream --- readObject()
注意:
- 先序列化后反序列化,反序列化的顺序要和序列化相同。
- 不是所有的类都能序列化,解决异常NotSerializableException,实现Serializable接口接口接口才可以序列化。
- 不是所有的属性都需要序列化,当属性不需要序列化时使用transient 标记,返回默认值。
(2)基本操作
public class ObjectDemo05 {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
File file=new File("F:/heihei.txt");
write(file);
read(file);
}
public static void read(File file) throws FileNotFoundException, IOException, ClassNotFoundException{
//1.创建流
ObjectInputStream in=new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)));
//2.读取
Object p=in.readObject();
if(p instanceof Person){
Person person=(Person)p;
System.out.println(person.getName()+"----->"+person.getGender());
}
//3.关闭
in.close();
}
/**
* 序列化
* @param file
* @throws IOException
* @throws FileNotFoundException
*/
public static void write(File file) throws FileNotFoundException, IOException{
//1.创建流
ObjectOutputStream out=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
//2.准备数据
Person p=new Person("张三",1);
int[] arr={3,4,5,6};
//3.写数据
out.writeObject(p);
out.writeObject(arr);
//4.关闭=flush+clear+close
out.close();
}
}
class Person implements Serializable{
private String name; //名字
private transient int gender; //性别
public Person(String name, int gender) {
super();
this.name = name;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Person [name=" + name + ", gender=" + gender + "]";
}
}
4.枚举
- 所有的枚举类都隐式的继承了java.lang.Enum中的中的抽象类。
- 枚举也是类,枚举中的所有成员都是类型的对象|实例,成员默认是public static final。
public class EnumDemo06 {
public static void main(String[] args) {
Weekday sun = Weekday.Sun;
// 1.返回此枚举常量的名称
System.out.println(sun.name());
// 2.返回枚举常量的索引
System.out.println(sun.ordinal());
// 3.values() 返回当前枚举类的所有属性
Weekday[] week = sun.values();
for (Weekday w : week) {
System.out.print(w + " ");
}
}
}
enum Weekday {
Sun, Mon, Tues, Wed, Thur, Fri, Sat;
// 可以存在成员方法
public void test() {
}
}
// class Weekday{
// public static final Weekday Sun = new Weekday();
// }