日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

异常,Map,File

發(fā)布時間:2023/12/20 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 异常,Map,File 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

JAVA知識點(diǎn)

  • 異常
    • Throwable
    • throw和throws
    • 集合使用步驟
    • list
  • Set
    • 哈希值
    • TreeSet
  • 案列:不重復(fù)的隨機(jī)數(shù)
  • 泛型方法
    • 類型通配符
    • 可變參數(shù)
    • 可變參數(shù)使用
  • Map
    • 集合的獲取
    • Map集合遍歷
    • ArrayList嵌套HashMap
    • HashMap嵌套ArrayList
    • 案列:統(tǒng)計字符串中每個字符出現(xiàn)的次數(shù)
  • File
    • 遍歷目錄
    • 字節(jié)流
    • 字節(jié)流讀數(shù)據(jù)
    • 字節(jié)緩沖流
    • 字符串中編碼解碼
    • 字符流
    • 字符緩沖流
      • 字符緩沖流特有功能
    • IO流小結(jié)
    • 案例:點(diǎn)名器
    • 復(fù)制單級文件夾
    • 復(fù)制多級文件夾
    • 序列化和反序列化

異常

Throwable

throw和throws

集合使用步驟

list

public class ListDemo {public static void main(String[] args) {List<Student> list = new ArrayList<>();Student s1 = new Student("張大帥", 30);Student s2 = new Student("林丹", 32);Student s3 = new Student("張一位", 25);list.add(s1);list.add(s2);list.add(s3);//迭代器:集合特有的遍歷方式Iterator<Student> it = list.iterator();while (it.hasNext()) {Student s = it.next();System.out.println(s.getName() + "," + s.getAge());}//普通for:帶有索引的遍歷方式for (int i = 0; i < list.size(); i++) {Student s = list.get(i);System.out.println(s.getName() + "," + s.getAge());}//增強(qiáng)for:最方便的遍歷方式for (Student s :list){System.out.println(s.getName()+","+s.getAge());}} }

運(yùn)行結(jié)果

張大帥,30 林丹,32 張一位,25

Set

哈希值

TreeSet

(不包含重復(fù)元素)

public class TreeSetDemo {public static void main(String[] args) {TreeSet<Integer> treeSet = new TreeSet<>();treeSet.add(12);treeSet.add(56);treeSet.add(14);treeSet.add(25);treeSet.add(12);for (Integer i:treeSet){System.out.println(i);}} }

結(jié)果

12 14 25 56

案列:無參構(gòu)造

student類

public class Student implements Comparable<Student>{private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic int compareTo(Student a) {//從小到大排序int num = this.age-a.age;//年齡相同,按照姓名子母排序int num2=num==0?this.name.compareTo(a.name):num;return num2;} } ----------------- public class TreeSetDemo {public static void main(String[] args) {TreeSet<Student> ts = new TreeSet<Student>();Student s1 = new Student("haha", 23);Student s2 = new Student("than", 26);Student s3 = new Student("sasa", 12);Student s4 = new Student("haha", 32);Student s5 = new Student("haa", 32);ts.add(s1);ts.add(s2);ts.add(s3);ts.add(s4);ts.add(s5);for (Student s:ts){System.out.println(s.getName()+","+s.getAge());}} }

運(yùn)行結(jié)果

sasa,12 haha,23 than,26 haa,32 haha,32

有參構(gòu)造

public class TreeSetDemo_1 {public static void main(String[] args) {TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {@Overridepublic int compare(Student s1, Student s2) {int num = s1.getAge() - s2.getAge();int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;return num2;}});Student s1 = new Student("haha", 23);Student s2 = new Student("than", 26);Student s3 = new Student("sasa", 12);Student s4 = new Student("haha", 32);Student s5 = new Student("haa", 32);ts.add(s1);ts.add(s2);ts.add(s3);ts.add(s4);ts.add(s5);for (Student s:ts){System.out.println(s.getName()+","+s.getAge());}} }

運(yùn)行結(jié)果

sasa,12 haha,23 than,26 haa,32 haha,32

案列:不重復(fù)的隨機(jī)數(shù)

(無序)

public class SetDemo {public static void main(String[] args) {Set<Integer> integers = new HashSet<Integer>();Random r = new Random();while (integers.size()<10){int number=r.nextInt(20)+1;integers.add(number);}for (Integer i:integers){System.out.println(i);}} }

運(yùn)行結(jié)果

16 17 3 4 5 7 8 10 13 15

(有序)

public class SetDemo {public static void main(String[] args) {Set<Integer> integers = new TreeSet<Integer>();Random r = new Random();while (integers.size()<10){int number=r.nextInt(20)+1;integers.add(number);}for (Integer i:integers){System.out.println(i);}} }

運(yùn)行結(jié)果

1 2 3 6 8 10 12 13 15 17

泛型方法

Generic類

public class Generic<T> {public void show(T t){System.out.println(t);} }

GenericDemo類

public class GenericDemo {public static void main(String[] args) {Generic<String> g1 = new Generic<Sting>();g1.show("張森");Generic<Integer> g2 = new Generic<Integer>();g2.show(30);Generic<Boolean> g3 = new Generic<Boolean>();g3.show(true);} }

運(yùn)行結(jié)果

張森 30 true

改進(jìn)版

public class Generic<T> {public <T> void show(T t){System.out.println(t);} } ------------------- public class GenericDemo {public static void main(String[] args) {Generic g= new Generic();g.show("林丹");g.show(52);g.show(true);} }

類型通配符

可變參數(shù)

public class Demo {public static void main(String[] args) {System.out.println(sum(10,20,30,40));System.out.println(sum(10,20,30,40,50));System.out.println(sum(10,20,30,40,50,60));}public static int sum(int... a){int sum=0;for (int i:a){sum+=i;}return sum;} }

運(yùn)行結(jié)果

100 150 210

注意事項

  • 這里的變量是一個數(shù)組

  • 如果一方法有多個參數(shù),包含可變參數(shù),可變參數(shù)要放在最后

可變參數(shù)使用

Map

集合的獲取

public class MapDemo {public static void main(String[] args) {Map<String, String> map = new HashMap<>();map.put("asd01","張森");map.put("asd02","誕生");map.put("asd03","科大");// System.out.println(map.get("asd01"));//獲取所有鍵的集合Set<String> keySet = map.keySet();for (String key : keySet){System.out.println(key);}**運(yùn)行結(jié)果** asd02asd03asd01//獲取所有值的集合Collection<String> values = map.values();for (String i :values){System.out.println(i);}**運(yùn)行結(jié)果** 誕生科大張森} }

Map集合遍歷

方法一 Set<String> keySet = map.keySet();for (String key:keySet){System.out.println(key+","+ map.get(key));} 方法二 Set<Map.Entry<String, String>> entrySet = map.entrySet();for (Map.Entry<String,String> i:entrySet){String key = i.getKey();String value = i.getValue();System.out.println(key+","+value);

ArrayList嵌套HashMap

public class MapDemo_1 {public static void main(String[] args) {ArrayList<HashMap<String, String>> arrayList = new ArrayList<>();HashMap<String, String> hm1 = new HashMap<>();hm1.put("張飛", "翼德");arrayList.add(hm1);HashMap<String, String> hm2 = new HashMap<>();hm1.put("關(guān)羽", "武帝");arrayList.add(hm2);HashMap<String, String> hm3 = new HashMap<>();hm1.put("趙云", "子龍");arrayList.add(hm3);for (HashMap<String, String> hm : arrayList) {Set<String> keySet = hm.keySet();for (String key : keySet) {String value = hm.get(key);System.out.println(key + "," + value);}}} } ---------------- 關(guān)羽,武帝 張飛,翼德 趙云,子龍

HashMap嵌套ArrayList

public class HashMap_include_ArrayList {public static void main(String[] args) {HashMap<String, ArrayList<String>> hashMap = new HashMap<>();ArrayList<String> sg = new ArrayList<>();sg.add("趙云");hashMap.put("三國演義", sg);ArrayList<String> sh = new ArrayList<>();sh.add("宋江");hashMap.put("水滸傳", sh);ArrayList<String> xy = new ArrayList<>();xy.add("孫悟空");hashMap.put("西游記", xy);Set<String> keySet = hashMap.keySet();for (String key:keySet){System.out.println(key);ArrayList<String> value = hashMap.get(key);for (String s :value){System.out.println('\t'+s);}}} } ----------------------- 水滸傳宋江 三國演義趙云 西游記孫悟空

案列:統(tǒng)計字符串中每個字符出現(xiàn)的次數(shù)

public class MapDemo_1 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("輸入字符串");String line = sc.nextLine();// HashMap<Character, Integer> hashMap = new HashMap<>();TreeMap<Character, Integer> hashMap = new TreeMap<>();for (int i=0;i<line.length();i++){char key = line.charAt(i);Integer value = hashMap.get(key);if(value==null){hashMap.put(key,1);}else {value++;hashMap.put(key,value);}}//進(jìn)行拼接StringBuilder sb = new StringBuilder();Set<Character> keySet = hashMap.keySet();for (Character key:keySet){Integer value = hashMap.get(key);sb.append(key).append("(").append(value).append(")");}String result = sb.toString();System.out.println(result);} } --------------- 輸入字符串 aassdfg a(2)d(1)f(1)g(1)s(2)

File

遍歷目錄

字節(jié)流

實(shí)現(xiàn)換行

window:\r\n linux:\n mac:\r

追加寫入

public FileOutputStream(String name,boolean append)

字節(jié)流讀數(shù)據(jù)

FileInputStream file = new FileInputStream("idea_test\\dox.txt");int by;while ((by=file.read())!=-1){System.out.print((char)by);}file.close();

字節(jié)緩沖流

字節(jié)緩沖流復(fù)制

public class BufferedDemo {public static void main(String[] args) throws IOException {BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\網(wǎng)易云\\夢一場.jpg"));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("idea_test\\夢一場.jpg"));byte[] bys = new byte[1024];int len;while ((len = bis.read(bys)) != -1) {bos.write(bys, 0, len);}bis.close();bos.close();} }

字符串中編碼解碼

public static void main(String[] args) throws UnsupportedEncodingException {//編碼String s ="中國";byte[] bytes = s.getBytes("UTF-8");System.out.println(Arrays.toString(bytes));}[-28, -72, -83, -27, -101, -67]//解碼String s ="中國";byte[] bytes = s.getBytes("UTF-8");String ss=new String(bytes,"UTF-8");System.out.println(Arrays.toString(bytes));System.out.println(ss);[-28, -72, -83, -27, -101, -67]中國

字符流

寫數(shù)據(jù)

public static void main(String[] args)throws IOException {OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("idea_test\\oso.txt"));outputStreamWriter.write(97);outputStreamWriter.flush();//刷新流可以繼續(xù)寫數(shù)據(jù)outputStreamWriter.close();//關(guān)閉流不可以再寫數(shù)據(jù)}

讀數(shù)據(jù)

InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("idea_test\\oso.txt"));char[] chars = new char[1024];int len;while ((len=inputStreamReader.read(chars))!=-1){System.out.println(new String(chars,0,len));}inputStreamReader.close();}

字符緩沖流

BufferedReader reader = new BufferedReader(new FileReader("idea_test\\oso.txt"));char[] chars = new char[1024];int len;while ((len= reader.read(chars))!=-1){System.out.println(new String(chars,0,len));}

字符緩沖流特有功能

void newLine( ); //寫一行行分隔符
public String readLine( ); //讀一行文字,結(jié)果包含行的內(nèi)容的字符串,不包括任何行終止字符,如果字符流結(jié)尾已到達(dá),則為null。

IO流小結(jié)


案例:點(diǎn)名器

public static void main(String[] args)throws IOException {BufferedReader buf = new BufferedReader(new FileReader("idea_test\\name.txt"));ArrayList<String> arrayList = new ArrayList<>();String line;while ((line=buf.readLine())!=null){arrayList.add(line);}buf.close();Random random = new Random();int i = random.nextInt(arrayList.size());String name = arrayList.get(i);System.out.println("幸運(yùn)者是:"+name);} } ----------------------- 幸運(yùn)者:歐文

復(fù)制單級文件夾

public static void main(String[] args) throws IOException {File srcFolder = new File("D:\\wondtest");String srcFolderName = srcFolder.getName();File destFolder = new File("idea_test", srcFolderName);if (!destFolder.exists()){destFolder.mkdir();}File[] listFiles = srcFolder.listFiles();for (File srcFile :listFiles){String srcFileName = srcFile.getName();File destFile = new File(destFolder, srcFileName);copyFile(srcFile,destFile);}}private static void copyFile(File srcFile, File destFile)throws IOException {BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));byte[] bys = new byte[1024];int len;while ((len=bis.read(bys))!=-1){bos.write(bys,0,len);}bos.close();bis.close();}

復(fù)制多級文件夾

public class CopyFolderDemo {public static void main(String[] args) throws IOException {File srcFile = new File("D:\\wondtest");File destFile = new File("F:\\");copyFolder(srcFile,destFile);}//復(fù)制文件夾private static void copyFolder(File srcFile, File destFile)throws IOException {//判斷數(shù)據(jù)源File是否是目錄if (srcFile.isDirectory()){String srcFileName = srcFile.getName();File newFolder = new File(destFile, srcFileName);if (!newFolder.exists()){newFolder.mkdir();}File[] fileArray = srcFile.listFiles();for (File file:fileArray){copyFolder(file,newFolder);}}else {File newFile = new File(destFile, srcFile.getName());copyFile(srcFile,newFile);}}//復(fù)制文件private static void copyFile(File srcFile, File desFile)throws IOException {BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desFile));byte[] bys = new byte[1024];int len;while ((len=bis.read(bys))!=-1){bos.write(bys,0,len);}bos.close();bis.close();} }

序列化和反序列化

public class Student implements Serializable {private static final long serialVersionUID =42L;private String name;private transient int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';} } public class ObjectStreamDemo {public static void main(String[] args) throws IOException, ClassNotFoundException {write();read();}//反序列化private static void read() throws IOException, ClassNotFoundException {ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("idea_test\\oso.txt"));Object object = inputStream.readObject();Student s=(Student)object;System.out.println(s.getName()+","+s.getAge());inputStream.close();}//序列化private static void write() throws IOException{ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("idea_test\\oso.txt"));Student s = new Student("張達(dá)", 12);outputStream.writeObject(s);outputStream.close();} }

總結(jié)

以上是生活随笔為你收集整理的异常,Map,File的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。