java 知識點 2(基礎語法)

1、匿名對象

在這裏插入圖片描述

匿名對象只能使用一次

2、ArrayList集合

在這裏插入圖片描述
在這裏插入圖片描述
ArrayList存儲基本數據必須使用對應的包裝類
在這裏插入圖片描述

3、==

對於基本類型來說, == 是比較值

對於應用類型來說, ==是比較的地址值

4、權限的大小關係

在這裏插入圖片描述

5、super、this(後面帶括號時)

使用super()或this()只能在子類第一個構造方法的第一個分號前使用。且兩個不能同時使用。

6、多態中成員變量、成員方法

成員變量
在這裏插入圖片描述
成員方法
在這裏插入圖片描述

7、final

在這裏插入圖片描述
修飾類:這個類就是太監類,可以有父類,沒有子類。
修飾方法:這個方法就不能被覆蓋重寫

對於類和方法來說,abstract和final不能同時使用。因爲abstract必須要子類去覆蓋重寫

修飾局部變量:變量就不能改變值了
修飾成員變量:用了final之後必須手動複製,系統不會再給默認值了。

8、權限

在這裏插入圖片描述

9、throws和throw

throws例

public static void main(String[] args){
	ExceptionDemo ed = new ExceptionDemo();
	try{
		ed.aa(100, 1);
	}catch(Exception e){
 		...
	}
} 
public void aa(int a,int b) throws Exception{
	...
}

throw例

public static void main(String[] args){
	ExceptionDemo ed = new ExceptionDemo();
	try{
		ed.aa(100, 1);
	}catch(Exception e){
 		...
	}
} 
public void aa(int a,int b) throws Exception{
	throw new Exception("throw可以手動拋出異常語句")
}

10、java數據持久化,Properties配置文件

讀取配置文件

public String getValueByKey(String filePath, String key){
	Properties pps = new Properties();
	try{
		//BufferedInputStream對InputStream進行緩存區包裝,達到性能優化
		InputStream in = new BufferedInputStream(new FileInputStream(filePath));
		
		//從字節輸入流中讀取鍵值對
		pps.load(in);
		
		//獲取文件value
		String value = pps.getProperty(key);
		return value;
	} catch (IOException e){

		//在命令行打印異常信息在權程序中出錯的位置及原因
		e.printStackTrace();
		return null;
	}
}

往配置文件寫東西

public void w(String filePath, String key, String value) throws IOException{
	Properties pps = new Properties()

	//文件進入輸入流
	InputStream i = new FileInputStream(filePath);
	//從輸入流中讀取
	pps.load(i);
	//把輸出流n指向filePath
	OutputStream o = new FileOutputStream(filePath);
	//加載Properties表中的格式
	pps.setProperties(key, value);
	//將Properties表中的屬性列表寫入輸出流
	pps.store(o, "Update "+ key);
}

調用上述方法

public static void main(String[] args) throws IOException{
	//獲取jvm環境變量“user.dir”定義相對路徑
	String path = System.getProperty("user.dir")+ "\\data\\";

	ProperReadWrite prw = new ProperReadWrite();
	
	//讀取文件(key爲63.name)
	String value = prw.getValueByKey(path + "Users.properties", "63.name")
	System.out.println(value);
	prw.getAllProperties(path + "Users.properties");
	
	//寫入文件
	prw.writeProperties("Users.properties", "63.name", "chengdu");
}

11、java數據持久化,excel文件

下載jxl的jar包:https://www.onlinedown.net/soft/1163870.htm

在項目中新建lib文件夾,把包複製進去,然後’右擊項目-properties-java Build Path-Libraries-Add jar-apply

讀取excel

public static void readExcel(String fileName) {
		File file = new File(fileName);
		try {
			//輸入流:對文件數據以字節的形式進行讀取操作
			FileInputStream fis = new FileInputStream(file);
			//從字節流中讀取xls文件(不能讀xlsx)
			jxl.Workbook r = Workbook.getWorkbook(fis);
			//獲取excel中所有工作表
			Sheet[] sheet = r.getSheets();
			for (int i=0; i<sheet.length; i++) {
				Sheet s = r.getSheet(i);
				//遍歷當前工作表中的行
				for(int j=0; j<s.getRows(); j++) {
					Cell[] cells = s.getRow(j);
					//遍歷列
					for(int k=0; k<cells.length; k++) {
						System.out.print(cells[k].getContents());
						System.out.print("\t");
					}
					System.out.println();
				}
			}
			fis.close();
		}catch(Exception e) {
			//打印異常信息在程序中出錯的位置及原因
			e.printStackTrace();
		}
		
	}

調用讀取

public class a01 {
	public static void main(String[] args){
		String path = System.getProperty("user.dir");
		readExcel(path +"\\data\\Students.xls");
	}

寫入excel

	public static void writeExcel() {
		WritableWorkbook book = null;
		String info[] = {"序號","學號","姓名"};
		try {
			//創建excel表格
			String fileName = System.getProperty("user.dir")+"\\data\\NewStudent.xls";
			book = Workbook.createWorkbook(new File(fileName));
			//創建工作表
			WritableSheet sheet = book.createSheet("NewStudents.xls", 0);
			//添加表頭
			for(int j=0; j<7; j++) {
				Label label = new Label(j, 0 , info[j]);
				sheet.addCell(label);
			}
		
			//添加數據
			sheet.addCell(new Label(0, 1, "1"));
			sheet.addCell(new Label(1, 1, "20001"));
			sheet.addCell(new Label(2, 1, "法海"));
			
			//寫入並關閉
			book.write();
			book.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

修改excel

	private static void modifyExcel(){
		try {
			String fileName = System.getProperty("user.dir")+"\\data\\Students.xls";
			//獲得文件
			Workbook wb = Workbook.getWorkbook(new File(fileName));
			//打開一個副本文件,並指定數據寫回到原文件
			WritableWorkbook book = Workbook.createWorkbook(new File(fileName), wb);
			//打開目標工作表
			WritableSheet sheet = book.getSheet(0);
			sheet.addCell(new Label(0, 1, "100"));
			//關閉
			book.write();
			book.close();
		}catch(Exception e) {e.printStackTrace();}
	}

12、StringTokenizer對字符串分析

import java.util.StringTokenizer;

public class d02 {
	public static void main(String[] args) {
		String s = "[2020][06][01][18][03]";
		// 第一個參數是要分隔的s,第二個參數是分隔字符集合
		StringTokenizer st = new StringTokenizer(s, "[]");
		// hasMoreTokens返回是否還有分隔符
		while(st.hasMoreTokens()) {
			// 插入每個字符中間的空格
			System.out.print(st.nextToken()+" ");
		}
		
	}
}

13、Timer計時器

2秒後執行打印

import java.util.Timer;
import java.util.TimerTask;

public class d03 {
	//2000毫秒後執行打印
	public static void main(String[] args) {
		Timer timer = new Timer();
		timer.schedule(new TimerTask() {
			@Override
			public void run(){
				System.out.print("2秒後執行了");
			}
		}, 2000);
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章