Java学习笔记(十)

枚举类

enum Season(
	SPRING("春天","");
	SUMMER("夏天","");//调用有参私有构造
	AUTUMN("秋天","");
	WINTER("冬天","");
	private final String name;
	private final String desc;
	private Season(String name,String desc){
		this.name=name;
		this.desc=desc;
	}
	public void showInfo(){
		System.out.println(this.name+" "+this.desc);
	}
)
main{
	Season spring= Season.SPRING;
	spring.showInfo();
	
	Season spring1=Season.SPRING;
	System.out.println(spring.equals(spring1));
	//结果为true,单例模式
}

枚举类也可以实现接口

注解(Annotation)

注解就是代码里的特殊标记,可以设置元数据,使用注解时需要在其前面增加@符号[

  • @Override
  • @Deprecated
  • @SupressWarnings 抑制编译器警告注解
  • 自定义注解@interface
@Target(ElementType.FIELD)//属性
@Retention(RetentionPolicy.RUNTIME)//注解的声明周期
@Documented//文档
@interface TestAnn{
	public int id() default 0;
	public String desc() default ""; 
}

IO流

Java.io.File

文件和目录名的抽象表现形式,但是File不能直接访问文件内容本身,如果需要访问本身,则需要使用输入/输出流

  • 两种常见构造方法
    public File(String pathname)
    public File(String parent,String child)

在这里插入图片描述
mkdir()/mkdirs() 创建单层/多层目录

遍历文件


public void test(File file){
	if(file.isFile()){
		System.out.println(file.getAbsolutePath()+"是文件");
	}else{
		System.out.println(file.getAbsolutePath()+"是文件夹");
		File[] fs=file.listFiles();
		if(fs!=null &&fs.length>0){
			for(File ff:fs){
				test(ff);
			}
		}
	}
	
}

IO原理及流的分类

Java程序中,对数据的输入/输出操作以“流(stream)”的方式进行,

  • 数据单位分类:字节流(8 bit),字符流(16 bit)
  • 流向分类:输入流,输出流
  • 角色不同:节点流,处理流
抽象基类字节流字符流
输入流InputStreamReader
输出流OutputStreamWriter

JavaIO流共有40多个,基本上都是都由上述四个类派生出的,子类命名规则都是以其父类名作为子类名后缀
在这里插入图片描述

文件流

分为字节流和字符流,字符流只适合字符文件。
在写入时,目录下同名文件将被覆盖

//文件字节输入流
public static void testFileInputStream(String filename){
	try{
		FileInputStream in = new FileInputStream(filename);
		byte[] b = new byte[10];
		int len = 0;
		while( (len=in.read(b))! = -1  ){
			System.out.println(new String(b,0,len));
		}
		in.close();
	}catch (Exception e){
	//
	}

}
//文件字节输出流
public static void testFileOutputStream(String filename){
	try{
		FileOutputStream out= new FileOutputStream(filename);
		String str= "sssssss";
		out.write(str.getBytes());//把数据写到内存中
		out.flush();//把内存中数据刷新到硬盘中
		out.close();
	}catch(Exception e){
	//
	}
}
//复制文件
	public static void copyFile() {
		try {
			FileInputStream in= new FileInputStream("/Users/xlzz/Desktop/java/abc/tt.txt");
			FileOutputStream out= new FileOutputStream("/Users/xlzz/Desktop/java/abc/cc/tt1.txt");
			byte[] b=new byte[100];
			int len=0;
			while((len=in.read(b))!=-1){
				out.write(b,0,len);
			}
			out.flush();
			out.close();
			in.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	//文件字符输入流
	public static void testFileReader(String inPath){
		try{
			FileReader fr=new FileReader(inPath);
			char[] c=new char[10];
			int len=0;
			while((len=fr.read(c))!=-1){
				System.out.println(new String(c,0,len));
			}
			fr.close();
		}catch(Exception e){
		
		}
	}
	//文件字符输出流
	public static void testFileWriter(String text,String outPath){
		try{
			FileWriter fw=new FileWriter(outPath);
			fw.write(text);
			fw.flush();
			fw.close();
		}catch(){
		
		}
	}
	
	public static void copyFile(String inPath,String outPath){
		try{
			FileReader fr=new FileReader(inPath);
			FileWriter fw=new FileWriter(outPath);
			char[] c=new char(100);
			int len=0;
			while((len=fr.read(c))!=-1){
				fw.write(c,0,len);
			}
			fw.flush();
			fw.close()
			fr.close();
		}catch{

		}
	}

缓冲流(基于内存的操作)

缓冲流就是先把数据缓冲到内存里,在内存中去做io操作,基于内存的IO操作大概能比基于硬盘的操作快75000倍,主要有

  • BufferedInputStream
  • BufferedOutputStream
  • BufferedReader
  • BufferedWriter
    缓冲流要套在相应的节点流上,对读写数据提供了缓冲的功能。
//缓冲字节输入流
 FileInputStream in = new FileInputStream();
 BufferedInputStream br = new BufferedInputStream(in);
 byte[] b = new byte[10];
 int len = 0;
 while((len=br.read(b))!=-1){
	System.out.println(new String(b,0,len));
 }
 //关闭流的时候,最晚开的最早关
 br.close();
 in.close();

//缓冲字节输出流
FileOutputStream out = new FileOutputStream("");
BufferedOutputStream bo= new BufferedOutputStream(out);
String s="hello world";
bo.write(s.getBytes());//写到内存中
bo.flush();
bo.close();
out.close(); 

转换流

转换流提供了在字节流和字符流之间的转换,当数据流的数据都是字符时,转化为字符流操作会更高效,三种编码格式

  • ISO8859-1,西欧纯英文编码
  • GBK
  • UTF-8
FileInputStream fs=new FileInputStream("");
FileOutputStream fo=new FileOutputStream("");
//参数一字节流,参数二字符流
//将字节流转换为字符流
InputStreamReader in=new InputStreamReader(fs,"GBK");
OutputStreamWriter out= new OutputStreamWriter(out,"UTF-8");

标准输入输出流

System.in和System.out分别代表了系统标准的输入和输出设备

	public static void testSystemIn() throws IOException {
		InputStreamReader is= new InputStreamReader(System.in);
		BufferedReader br=new BufferedReader(is);
		String str="";
		while((str=br.readLine())!=null) {
			System.out.println(str);
		}
		
	}
	
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test5 {
	public static void main(String[] args) throws IOException {
		String path= "/Users/xlzz/eclipse-workspace/demo/src/test1/info.txt";
		BufferedReader br=new BufferedReader(new FileReader(path));
		String name=br.readLine();
		String password=br.readLine();
		br.close();
		
		System.out.println("请输入账号和密码");
		br=new BufferedReader(new InputStreamReader(System.in));
		String name1=br.readLine();
		String password2=br.readLine();
		br.close();
		if(name.equals(name1)&&password.equals(password2)) {
			System.out.println("登录成功!");
		}else {
			System.out.println("密码或账号错误!");
		}
	}
}

打印流

PrintStream和PrintWriter重写了一系列print和println方法,用于多种数据的输出,System.out返回的是PrintStream的实例

数据流

读取和写出基本数据类型,有DataInputStream和DataOutputStream两个类

对象流

  • 将对象存储于硬盘中
  • 将对象通过网络传输
    对象的序列化机制允许把内存中的Java对象换成平台无关的二进制流,要想类对象可序列化,则必须实现一下两个接口之一:
  • Serializable:实现此接口必须有一个表示序列化版本标识符的静态变量
    private static final long serialVersionUID;
  • Externalizable
public class Person implements Serializable{
	private static final long serialVersionUID=1L;
	String name;
	int age;
}

ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(""));
Person p=new Person();
p.age=11;
p.name="111";
out.writeObject(p);
out.flush();

ObjectInputStream in = new ObjectInputStream(new FileInputStream(""));
Person p=(Person)in.readObject();

随机存取流

RandomAccessFile对象包含一个记录指针,用于表示当前读写处的位置

  1. 构造方法:
    - public RandomAccessFile(File file,String mode)
    - public RandomAccessFile(String name,String mode)
    - mode: r rw rwd rws
  2. 实例代码
   	RandomAccessFile ra=new RandomAccessFile("","r");
   	ra.seek(0);//设置读取文件内容的起始点
   	//如果是在文件开头或中间的某个位置开始写的话,就会用写的内容覆盖掉等长度的原内容
   	byte[] b = new byte[1024];
   	int len=0;
   	while((len=ra.read(b))!=-1){
   		System.out.println(new String(b,0,len));
   	}
   	ra.close();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值