import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
public class Test {
public static int ci = 0; //定義次數
public static void xitong() {
String path = "C:\\Users\\lenovo\\Desktop\\Test.txt";//檔案路徑
File file = new File(path);
file.delete();//删除原來檔案
File parentFile = file.getParentFile();//得到上一級檔案夾
if(!parentFile.exists())
{
parentFile.mkdirs();//建立目前檔案夾,以及父路徑不存在的檔案夾
}
if(!file.exists())
try
{
file.createNewFile();//建立一個空檔案
} catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
}
User[] user = new User[6];//客戶數組
user[0] = new User("admin", "admin123", "11111111111");
user[1] = new User("ABC", "abcabc", "11111111111");
user[2] = new User("BCD", "bcdbcd", "11111111111");
user[3] = new User("CDE", "cdecde", "11111111111");
user[4] = new User("DEF", "defdef", "11111111111");
user[5] = new User("EFG", "efgefg", "11111111111");
for (int i = 0; i < user.length; i++) {
writer(user[i], path);//将客戶資訊寫入檔案
ZhuJieMian jieMian = new ZhuJieMian();
boolean m = false;
while(!m)
jieMian.denglu();//輸出想要的字元串
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();//選擇功能
while (!(i == 1 || i == 2 ))
System.out.println("輸入錯誤,請重新輸入!");
i = scan.nextInt();
switch (i)
case 1:
ZhuCe ce = new ZhuCe();//注冊類
writer(ce.zhuce(), path);
m = false;
break;
case 2:
boolean b = true;
Scanner scan1 = new Scanner(System.in);
A:while (b)
{
DengLu(path, scan, scan1,file);//登陸方法
}
default:
}
private static void DengLu(String path, Scanner scan, Scanner scan1,File file) {
System.out.println("請輸入使用者名:");
String inputStr = scan1.nextLine();
FileReader fr = null;
StringBuffer buffer = null;
try
fr = new FileReader(path);
int len = 0;
char[] cbuf = new char[500];
buffer = new StringBuffer();
while(-1 != (len = fr.read(cbuf)))
buffer.append(new String(cbuf,0,len));//将檔案中的字元串寫入buffer
} catch (FileNotFoundException e)
e.printStackTrace();
} catch (IOException e)
String[] read = buffer.toString().split(";");//将buffer分解成數組
String[][] read1 = new String[read.length][];
for (int j = 0; j < read1.length; j++) {
read1[j] = read[j].split(",");
HashMap<String, User> hashMap = new HashMap<>();
hashMap.put(read1[j][0], new User(read1[j][0], read1[j][1], read1[j][2]));//将客戶資訊寫入hashMap容器
if(hashMap.get(inputStr) != null)
System.out.println("請輸入密碼:");
String inputStr1 = scan1.nextLine();
if(inputStr1.equals(hashMap.get(inputStr).password))//判斷密碼
if(inputStr1.equals("admin123"))//當客戶名為管理者的使用者名
boolean d = true;
while(d)
{
System.out.println("使用者資訊:");
Collection<User> values = hashMap.values();
// 第一種
// 擷取map中儲存key的容器
Set<String> keySet = hashMap.keySet();
Iterator<String> iterator = keySet.iterator();
while(iterator.hasNext())
{
String key = iterator.next();
User valus = hashMap.get(key);
if(valus.user_name.equals("admin"))
{
continue;
}
System.out.println("使用者名: "+valus.user_name+",密碼: "+valus.password+",電話: "+valus.tel);
}
System.out.println("你可以選擇删除功能,确認請輸入1,輸入2傳回上一級,其他輸入退出系統!");
String j = scan.next();
if(j.equals("1"))
boolean c = true;
while(c)
System.out.println("請輸入你要删除使用者的使用者名:");
String string1 = scan.next();
if(hashMap.get(string1) != null)
hashMap.remove(string1);
c = false;
}else
System.out.println("輸入錯誤,請重新輸入!");
}else if(j.equals("2"))
d = false;
}else
System.out.println("感謝您應用本系統,再見!");
file.delete();
}
}else
System.out.println("登入成功!");
file.delete();
else
}else
System.out.println("輸入錯誤,請重新輸入!");
public static void writer(User user,String path) //将客戶資訊寫入檔案
{
String str = user.toString();
FileWriter fw = null;
fw = new FileWriter(path,true);
fw.write(str);
fw.flush();
// TODO Auto-generated catch block
}finally
if(fw != null)
try
fw.close();
} catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
}
這是一個使用者登入注冊的作業,雖然看是簡單,當時需要一定的java基礎和邏輯能力,我們需要不斷練習,熟練代碼。
StringBuffer 代表可變的字元序列
//構造方法
StringBuffer buffer = new StringBuffer();//預設16
StringBuffer buffer1 = new StringBuffer("123");//字元串長度+16
StringBuffer buffer2 = new StringBuffer(20);//設定長度
buffer1.append(123).append(12.4).append(true).append("asd");
//如果添加的資料超出緩沖區的長度,他會自動擴充,擴充的長度為原長度的兩倍+ 2
System.out.println(buffer1.toString());
//StringBuffer特有的,對StringBuffer立面儲存的字元進行操作,但是不會産生新的對象
buffer1.insert(1, "=====");
System.out.println(buffer1.toString());
buffer1.delete(2, 5);//包左不包右
System.out.println(buffer1.toString());
buffer1.reverse();//反向字元串
System.out.println(buffer1.toString());
buffer1.replace(0, 5, "++++");//包左不包右
System.out.println(buffer1.toString());
//跟字元串類似
System.out.println(buffer1.capacity()+ "1");//空間長度
System.out.println(buffer1.length() + "a");
buffer1.setLength(10);
System.out.println(buffer1.capacity()+ "2");
System.out.println(buffer1.length() + "b");//實際字元長度
System.out.println(buffer1.toString());
System.out.println(buffer1.charAt(5));
Character 靜态方法:
|
|
确定指定字元是否為小寫字母。 |
确定指定字元是否為大寫字母。 |
單執行個體模式 單态模式 SingleTon
步驟:
1、私有化構造方法
2、聲明一個private final static 的成員變量,類型為目前類型,去執行個體化一個目前類型的對象。
3、聲明一個public static的方法使用者擷取目前類型的成員變量
異常捕獲:
try{
//可能出現異常的地方,
}catch(Exception e)
{
System.out.println(e.getMessage());//得到異常資訊
e.printStackTrace();//列印異常棧資訊
Exception
RuntimeException 運作時異常
運作時出現的錯誤 ,而且這類異常出現的機率比較高,是以一般我們是不會去捕獲
常見運作時異常:
ArithmeticException 數學運算異常
NullPointerException 空指針異常
StringIndexOutOfBoundsException 字元串索引越界
ArrayIndexOutOfBoundsException 數組下标越界
NegativeArraySizeException 數組定義異常
IllegalArgumentException 非法參數異常
注意: 不需要try{}catch(){}捕獲,要看的懂異常的原因,通過修改代碼區捕獲它。
Check Exception 檢查異常
在文法上需要去捕獲
IOException
try{}catch(){}catch(){}
try之後可以有多個catch 但是要保證catch之後捕獲的異常類型要有一定的順序(子類在上父類在下)
finally 在catch之後:不管try中代碼有沒有出現錯誤都必須執行
throws
在方法之後聲明調用該方法可能抛出某種異常,可以聲明抛出多個。
RuntimeException可以不用聲明被捕獲
throw
throw在具體的某個位置抛出某個異常,一旦被執行,以下的代碼就結束。
但是可以用try{}catch(){}直接捕獲
重寫中方法異常抛出的聲明:
1、重寫是可以不聲明抛出異常
2、重寫方法不可以抛出被重新方法聲明之外的異常(運作時異常除外)
自定義異常:
使用自定義異常一般有如下步驟:
1、通過繼承 java.lang.Exception 類聲明自己的異常類。
2、在方法适當的位置 生成自定義異常的執行個體,并用throw 語句抛出。
3、 在方法的聲明部分用throws 語句聲明該方法可能抛出的異常。
Set 接口
無序不可重複
無序:不按照添加先後,添加完之後有一定的順序
Set接口沒有新增的方法,所有的放都是Collection繼承過來的。
主要實作類:
HashSet
順序:按hash碼排列
List接口
有順序可以重複
主要實作類:
ArrayList
LinkedList
list新增了
remove(int index)//移除索引的元素
remove(Object o)//具體的元素
Iterator 疊代器
Collection接口 有聲明 iterator() 方法
ArrayList中 :
toArray() 把容器裡面的元素轉換成數組裡面的元素
toArray(T[] t)聲明一個數組,然後把容器裡面的元素按照原來順序指派到數組當中
移除清單中索引在 (包括)和 (不包括)之間的所有元素。(包級) |
注意: ArrayList中的常用API要熟練
容器:
LinkedList
将指定元素插入此清單的開頭。 |
将指定元素添加到此清單的結尾。 |
從此清單所表示的堆棧處彈出一個元素。 |
将元素推入此清單所表示的堆棧。 |
Map
key - value 儲存資料
1、key是不可以重複
2、value是可以重複的
HashMap
hashmap資料添加是無序
按照key的hashcode排序
作為key的類型,必須重寫hashcode equlse
map周遊
// 第一種
// 擷取map中儲存key的容器
Set<String> keySet = hashMap.keySet();
Iterator<String> iterator = keySet.iterator();
while(iterator.hasNext())
{
String key = iterator.next();
String valus = hashMap.get(key);
System.out.println("key:" + key + "- value:" + valus);
}
//第二種
Set<Entry<String, String>> entrySet = hashMap.entrySet();
Iterator<Entry<String, String>> iterator1 = entrySet.iterator();
while (iterator.hasNext())
Entry<String, String> next = iterator1.next();
String key = next.getKey();
String value = next.getValue();
System.out.println("key:" + key + "- value:" + value);
//建議使用第一種
工具類:
Arrays 數組的工具類
Collections
Comparable //讓實作這個接口的對象具有比較大小的能力
Compartor //比較者
File
抽象路徑 C: + File.separator + java + File.separator + day01
絕對路徑 E:\FA01
相對路徑: \day01
//檔案過濾器接口
class MyFilter implements FilenameFilter
public boolean isPPt(String name)
{
if(name.toLowerCase().endsWith(".ppt") || name.toLowerCase().endsWith(".pptx"))
return true;
return false;
}
@Override
public boolean accept(File dir, String name)
return isPPt(name);
位元組流:
節點流:
FileInputStream
FileOutPutStream
注意: 一般都去讀取圖檔、檔案
處理流: 套在其他節點流或者處理流之上的流
緩存流 BufferedOutputStream (前提:節點流存在 隻是性能上面有所提升)
字元流: 讀文本
節點流
FileWriter 字元輸出流
FileReader 字元輸入流
處理流: