方法重写与常见类

方法重写

对于一个方法而言:区分同名方法,通过参数列表来确定方法
》方法重写:发生继承或实现关系,父类不满足子类需求,子类重写父类中的方法
》重写方法特点:*发生继承
             *方法名相同
             *参数列表要相同(顺序、个数、类型)
             *子类的返回类型<=父类的返回类型(引用),基本类型相同
             *子类的访问权限>=父类的访问权限(父类的修饰符不能为
              private)
             *子类抛出的异常<=父类抛出的异常(任意一个方法都会对外抛
              出运行时异常)
package com.mage.arrsy;

public class Test02 {
	public static void main(String[] args) {
		
	}
}

class Fath{
	public Fath  fun(Fath s) {
		System.out.println(1/0);
		return null;
	}
}

class Son extends Fath{
	@Override
	public Son  fun(Fath s) throws NullPointerException {
		return null;
	}
}

main方法

*public:共享的,保证能够调用到
*static:静态的,保证外部调用无需创建对象
*void:  JVM无需返回值
*String[] args:String类型的数组args,可以传入,调用main方法时,
               可以传入实际参数
package com.mage.arrsy;

public class Test01 {
	//dir		copy		md
	public static void main(String[] args) {
		System.out.println("数组的长度:"+args.length);
		//迭代 /遍历
		for(String arr:args) {
			System.out.println(arr);
		}
		
		String order = args[1];
		switch(order){
			case  "copy":
				copy();break;
			case "dir":
				dir();break;
			case "md":
				md();break;
				
		}
	}
	public static void copy() {
		System.out.println("Copy");
	}
	
	public static void dir() {//获取当前路径
		System.out.println("Dir");
	}
	
	public static void md() {
		System.out.println("Md");
	}

}

java中常见的类

Scanner

》Scanner:
	*hasNextXXX:获取扫描到的数据是否为XXX类型
	*nextXXX:获取下一个扫描的内容转为XXX类型(转换过程可能出错)
	》next():*一定要读取到有效字符才会结束输入;
		   *对遇到有效字符之前的空白next()会自动省略掉;
		   *只有输入有效的字符后碰见的空白才会作为结束符或分隔符;
		   *next()不能得到带有空格的字符串;
	》nextLine():*以回车键为结束符,也就是说nextLine()方法返回的是回车键之前的所有字符;
				*可以得到有空格的字符串;
package com.mage.scanner;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Test01 {
	public static void main(String[] args) throws FileNotFoundException{
		Scanner input = new Scanner(System.in);
		
		if(input.hasNext()) {
			System.out.println(input.next());
		}
		
		//重新创建Scanner对象,指定文件作为输入源
		Scanner sc = new Scanner(new File("C:\\Users\\Administrator\\Desktop"));
		System.out.println(sc.next());
		sc.close();
		input.close();
	}

}

String

》String:(字符串):一组字符序列(不可变得串)
	》创建String对象:
	*new String();
	*new String("abc");
	*"love";
	*new String(buf,2,3);//通过字节数组创建
》StringBuffer
》StringBuilder(jdk1.5之后才有)
package com.mage.string;

public class Test03 {
	public static void main(String[] args) {
		String str = "123";
		System.out.println(str);
		change(str);
		System.out.println(str);
	}
	
	public static void change(String str) {
		str = "356";
		System.out.println(str);
	}
}

package com.mage.string;

import java.io.UnsupportedEncodingException;

public class Test01 {
	public static void main(String[] args) throws UnsupportedEncodingException{
		
		//	String构造器
		String str1 = new String();		//	是一个" "对象
		System.out.println(str1);
		
		//	通过双引号直接创建对象
		//	通过字符串常量直接创建String对象
		String str2 = "I love study";
		
		//	通过字符串常量给str3赋值
		String str3  = " ";
		
		//比较str2和str3
		System.out.println(str1==str3);
		
		//	按照字节数组创建字符串对象
		byte[] bud = new byte[] {68,69,65,90,67};
		
		String str4 = new String(bud);
		System.out.println("通过字节数组创建字符串对象:"+str4);
		
		str4 = new String(bud,2,3);
		System.out.println("通过字节数组创建字符串对象:"+str4);
		
		str4 = new String(bud, 2,3,"utf-8");
		System.out.println("通过字节数组创建字符串对象:"+str4);
		
		//按照字符数组创建字符串对象
		char[] ch = new char[] {'L','O','V','E'};
		str4 = new String(ch);
		System.out.println("通过字符数组创建字符串对象:"+str4);
		
		str4 = new String(ch,0,4);
		System.out.println("通过字节数组创建字符串对象:"+str4);
		
		//按照代码点对象创造字符串对象
		int[] in = new int[] {66,77,88,99,111};
		str4 = new String(in,0,4);
		System.out.println("通过字节数组创建字符串对象:"+str4);
		
		//按照字符串对象创建字符串对象
		String str5 = new String ("I love study!");
		System.out.println("通过字符串对象创建字符串对象:"+str5);
	}
}

String常见方法

package com.mage.string;  
  
import java.util.Arrays;  
  
public class Test02 {  
    public static void main(String[] args) {  
        //  创建一个String对象  
        String str = "I love study!";  
          
        //  charAt(int index);  
        char ch = str.charAt(3);  
        System.out.println("返回当前索引位置上的指定字符:"+ch);  
          
        //  codePointAt(int index);  
        int num = str.codePointAt(1);  
        System.out.println("返回当前索引位置上元素的代码点:"+num);  
          
        //compareTo(String);  
        String cStr = "I love java!";  
        num = str.compareTo(cStr);  
        System.out.println("比较两个字符串的大小:"+num);  
          
        //  compareToIgronCase(String);  
        cStr = "I LOVE JAVA!";  
        num = str.compareToIgnoreCase(cStr);  
        System.out.println("比较两个字符串的大小(忽略大小写):"+num);  
          
        //  concat(String)  
        cStr = "and girl!";  
        str = str.concat(cStr);  
        System.out.println("拼接后的字符串为:"+str);  
          
        //  copyValueOf(char[])  
        char[] ch1 = new char[] {'I','L','O','V','E'};  
        str = String.copyValueOf(ch1);  
        System.out.println("创建新的字符串:"+str);  
          
        str = String.copyValueOf(ch1,0,5);  
        System.out.println("创建新的字符串:"+str);  
          
        //  endsWith(String)  
        str = "hreat.jpg";  
        boolean flag = str.endsWith(".jpg");  
        System.out.println("str是否是.jpg格式:"+flag);  
          
        //  getBytes  
        byte[] buf  = str.getBytes();  
        System.out.println("获取字符串的字节数组:"+Arrays.toString(buf));  
          
        //  getChars()  
        ch1 = new char[10];  
        //  从str的2索引处开始,到str的第6个索引结束(不包含),复制到ch1字符数组中,从第一个位置开始  
        str.getChars(2,6,ch1,0);  
        System.out.println("获取字符串的字符数组:"+Arrays.toString(ch1));  
          
        //  indexOf 返回负数代表未找到  
        str = "hehe.avi.jpg";  
        int index  = str.indexOf(".");  
        System.out.println(".在String中第一次出现的位置是:"+index);  
          
        index = str.indexOf(100);  
        System.out.println("e在String中第一次出现的位置是:"+index);  
          
        index = str.indexOf(".",5);  
        System.out.println(".在String中第五个索引位置开始计算,第一次出现的位置是:"+index);  
          
        index = str.lastIndexOf(".");  
        System.out.println(".在String中最后出现的位置是:"+index);  
          
        //  isEmpty()  
        System.out.println("查看数组是否为null:"+str.isEmpty());  
          
        //  replace()  
        str  = "hehe.avi.jpg";  
        str = str.replace(".","+");  
        System.out.println("替换后的结果:"+str);  
          
        //  splite()  
        str = "login? uname = zs && pwd = zs";  
        String[] strs = str.split("\\?");  
          
        for(String str2:strs) {       
            System.out.println(str2);  
        }  
        System.out.println("========");  
        String parm = strs[1];  
        strs = parm.split("&&");  
          
        for(String str2:strs) {  
            String[] ss = str2.split("=");  
            System.out.println(ss[1]);  
        }  
          
        //  大小写转换  
        str = "login? uname=zs&&pwd=zs";  
        str = str.toUpperCase();  
        System.out.println("转为大写:"+str);  
          
        str = str.toLowerCase();  
        System.out.println("转为小写:"+str);  
          
        //  通过静态方法创建String对象  
        String str1 = String .valueOf(123);  
        System.out.println(str1);  
  
        //  对象转字符串,其实就是调用当前对象的toString方法  
        User u = new User(10);  
        String str2 = String.valueOf(u);  
        System.out.println(str2);  
    }  
  
}  
  
class User{  
    private int age;  
    public User() {  
          
    }  
      
    public User(int age) {  
        super();  
        this.age =age;  
    }  
    public int getAge() {  
        return age;  
    }  
    public void setAge(int age) {  
        this.age = age;  
    }  
    public String toString() {  
        return "User[age="+age+"]";  
    }  
      
}  

StringBuffer

StringBuffer:
		之所以能够可变的原因,其底层就是一个动态字符数组
package com.mage.string;

public class Test04 {
	public static void main(String[] args) {
		//创建StringBuffer
		StringBuffer sb = new StringBuffer();
		//初始容量16
		System.out.println("通过空构造器创建的StringBuffer对象的初始容量:"+sb.length());
		
		//添加元素
		sb.append(true);
		sb.append(false).append(true).append(123).append("456");
		System.out.println(sb);
		System.out.println("当前sb对象中的字符串长度:"+sb.length());
		
		//添加元素
		sb.insert(3, "hehe");
		System.out.println(sb);
	}
}

StringBuilder

StringBuffer和StringBuilder都集成了AbstractStringBuilder
二者的区别:
	StringBuffer效率低于StringBuilder
	StringBuffer安全性要高于StringBuilder
	但一般情况下使用StringBuilder
	
jdk9之后,String中的存储数据通过byte数组存储+变量
jdk中对于String存储使得内存消耗极大地降低
package com.mage.string;

public class Test05 {
	public static void main(String[] args) {
		//StringBuilder
		StringBuilder sb = new StringBuilder();//构建一个16个长度的字符数组
		
		//添加元素
		sb.append("隔壁老王");
		sb.insert(4, "怕是个???");
		
		System.out.println(sb);
	}
}

file

File:抽象的路径以及文件
	1:创建对象是无需注意当前路径或者文件是否存在,都会创建出来
	2:和平台无关的路径分隔符
		File.pathSeparator:路径结束符
		File.separator:路径分割符
package com.mage.file;

import java.io.File;

public class Test01 {
	public static void main(String[]  args) {
		//	创建一个File对象
		File f = new File("C:\\Users\\Administrator\\Desktop\\a.txt");
		
		System.out.println(f);
		
		System.out.println(File.pathSeparator);
		System.out.println(File.pathSeparatorChar);
		System.out.println(File.separator);
		System.out.println(File.separatorChar);
		f = new File ("C:"+File.separator+"Users"+File.separatorChar+"Administrator"+File.separatorChar+"Desktop"+File.pathSeparatorChar);
		System.out.println(f);
	}
}

mkdir只能创建一级目录
package com.mage.file;

import java.io.File;
import java.io.IOException;

public class Test03 {
	public static void main(String[] args)throws IOException {
		//	创建File对象
		File file = new File("D:\\workspace");
		
		//	创建路径
		File filepath = new File ("D:\\workspace");
		filepath.mkdir();
		
		//	创建文件
		file.createNewFile();
		file.setReadOnly();
	}
}

package com.mage.file;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test04 {
	public static void main(String[] args) {
		// 创建file对象
		File file  = new File("C:\\");
		
		//	获取当前file中的所有目录对象
		File[] fs = file.listFiles();
		
		//	迭代所有file对象
		for(File f:fs) {
			String dateStr = new SimpleDateFormat("yyyy/MM/dd  hh:mm:ss").format(new Date(f.lastModified()));
			String isDirectory = f.isDirectory()?String.valueOf(f.length()):" ";
			String isFile = f.isFile()?String .valueOf(f.length()):" ";
			String FileName = f.getName();
			System.out.println(dateStr+"\t"+isDirectory+"\t"+isFile+"\t"+FileName);
			
		}
	}
}

IO

问题:1、file类本身只能针对文件或者是目录的元数据(除了内容本身)进行操作,不能对于文件中得内容做任何操作;
2、截止到目前,我们存储数据的手段是很有限的以及有问题的?
3、学习过的存储手段:
	变量	对象	数组	——>存储是在内存中的——>程序启动之后,数据存在,程序销毁之后
数据丢失
4、但是在后期整个编码、项目的过程中:
	我们对于数据一定要持久的存储起来,方便后期使用以及更新;
5、无法将数据持久化存储起来:
	解决办法:通过学习IO流的知识,将数据写入到文件中去,文件存储在磁盘上的,电脑关机后,只要文件保存,电脑开机,文件还存在;
	
IO:
	IO:input、output:输入、输出
	学习流的时候,流是有方向的,输入和输出是相对而言的;
IO流的分类:不是独立的分类而是相互的
	1、按照输出的数据类型不同:
		字节流:输入输出时,按照字节输入输出;
		字符流:输入输出时,按照字符输入输出;
	2、按照流的方向:
		输入流:
		输出流:
	3、按照处理功能:
		节点流:
		处理流:

字节输入流

读取数据:
	字节流:InputStream,是所有字节流的父类
	输入:
	子类:
		FileInputStream:文件字节输入流,数据源在文件中,读取按照字节读取,流的方向是输入;
package com.mage.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;


public class Test01 {
	public static void main(String[] args) throws IOException{
		//	创建对象
		File file = new File("C:\\Users\\Administrator\\Desktop\\a.txt");
		InputStream  is  = new FileInputStream(file);
		
		//读取数据
		//分析数据
		int num = 0;
		while((num=is.read())!=-1) {
			System.out.println("读取数据:"+(char)num);
		}
		
		//关闭资源
		is.close();	
	}
}

每次读取文件时,只能读取一个字节,在通过read方法读取数据时,读取到了文件的末尾,返回-1
Tips:可以循坏读取,FileInputStream创建时要保证文件存在

一次读取一个字节数组
package com.mage.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;



public class Test03 {
	public static void main(String[] args) {
		//	声明对象
		InputStream is = null;
		try {
			is = new FileInputStream("C:\\Users\\Administrator\\Desktop\\a.txt");
			
			//	读取数据
			byte[] by = new byte[1024];
			int size = is.read(by);
			
			//	分析数据
			String str = new String(by,0,size);
			System.out.println(str);
			
		}catch(FileNotFoundException e) {
			e.printStackTrace();
		}catch(IOException e) {
			e.printStackTrace();
		}finally {
			
			try{
				if(is != null) {}
				//关闭资源
				is.close();
			}catch(IOException e) {
				e.printStackTrace();
			}
		}
	}
}

字节输出流

字节输出流:所有的字节输出流的父类都是OutputStream
	子类:FileOutputStream:输出的目的地是文件,输出流,输出的类型是字节型
		输出时,如果文件不存在会创建一个文件,但不能创建目录
		
package com.mage.io;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Test04 {
	public static void main(String[] args) throws IOException{
		//	创建输出流对象
		OutputStream os = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\a.txt");
		
		//	声明并写出数据
		int num = 97;
		
		//	写出数据
		os.write(num);
		
		//	关闭资源
		os.close();
	}
}

创建FileOutputStream对象时,第二个参数boolean值,说明是否在文件后面追加内容,默认为false,不追加从而覆盖,true是追加不覆盖;
package com.mage.io;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Test05 {
	public static void main(String[] args) throws IOException {
		
		//	创建对象
		OutputStream os  = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\b.txt",true);
		
		//	声明写出的数据
		String str = "someone";
		
		//	写出数据
		os.write(str.getBytes(),0,5);
		
		//	关闭资源
		os.close();
	}
}

资源关闭顺序:先打开后关,后打开先关
package com.mage.io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/*
 * 
 *   先打开后关
 * 
 */
public class Test09 {
	public static void main(String[] args) throws IOException {
		CtrlCV();
	}
	
	public static void CtrlCV(String srcFile,String destFile) throws IOException{
		/*
		 * //1:声明复制和粘贴的文件对象 String srcFile = "C:\\Users\\wawjy\\Desktop\\1.jpg"; String
		 * destFile = "123.jpg";
		 */
		
		//2:声明对象
		InputStream is = new FileInputStream(srcFile);
		OutputStream os = new FileOutputStream(destFile);
		
		//3:读取文件
		int len = 0;
		while((len=is.read())!=-1) {
			//写出
			os.write(len);
		}
		//	关闭资源
		os.close();
		is.close();
		
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值