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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

本周学习总结JAVA

發布時間:2023/12/2 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 本周学习总结JAVA 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

6. 為如下代碼加上異常處理

byte[] content = null; FileInputStream fis = new FileInputStream("testfis.txt"); int bytesAvailabe = fis.available();//獲得該文件可用的字節數 if(bytesAvailabe>0){content = new byte[bytesAvailabe];//創建可容納文件大小的數組fis.read(content);//將文件內容讀入數組 } System.out.println(Arrays.toString(content));//打印數組內容

6.1 改正代碼,并增加如下功能。當找不到文件時,需提示用戶找不到文件xxx,請重新輸入文件名,然后嘗試重新打開。 如果是其他異常則提示打開或讀取文件失敗!。

注1:里面有多個方法均可能拋出異常。
功能2:需要添加finally關閉文件。無論上面的代碼是否產生異常,總要提示關閉文件ing。如果關閉文件失敗,提示關閉文件失敗!

public class Main {public static void main(String[] args) throws IOException {byte[] content = null;FileInputStream fis=null;try {fis = new FileInputStream("d:/testfis.txt");int bytesAvailabe = fis.available();//獲得該文件可用的字節數if(bytesAvailabe>0){content = new byte[bytesAvailabe];//創建可容納文件大小的數組fis.read(content);//將文件內容讀入數組}}catch(FileNotFoundException e){System.out.println("找不到文件xxx,請重新輸入文件名");}catch(Exception e){System.out.println("打開或讀取文件失敗!");}finally {try {System.out.println("關閉文件ing");//FileInputStream fis;fis.close();}catch(Exception e){System.out.println("關閉文件失敗!");}}System.out.println(Arrays.toString(content));//打印數組內容}}

6.2 結合題集6-2代碼,要將什么樣操作放在finally塊?為什么?使用finally關閉資源需要注意一些什么?

將必須要執行的操作放在finally塊,因為finally塊中代碼一定會執行,注意要進行try catch,因為關閉資源時可能出現異常

6.3 使用Java7中的try-with-resources來改寫上述代碼實現自動關閉資源。簡述這種方法有何好處?

public class Main {public static void main(String[] args) throws IOException {byte[] content = null;try(FileInputStream fis = new FileInputStream("testfis.txt")){int bytesAvailabe = fis.available();//獲得該文件可用的字節數if(bytesAvailabe>0){content = new byte[bytesAvailabe];//創建可容納文件大小的數組fis.read(content);//將文件內容讀入數組}}catch(Exception e){System.out.println(e);}System.out.println(Arrays.toString(content));//打印數組內容} }

使用try-with-resource語句,會自動調用close函數,關閉資源,相比前面的代碼,更加簡潔,方便

7. 讀取文件并組裝對象

實驗任務書中中的題目3:讀取文件并組裝對象

7.1 給出關鍵代碼(需出現你的學號)。額外要求:捕獲異常時,將錯誤的信息按照出錯原因:行號:該行內容格式輸出。

public class ReadFile201721123039 {private static Scanner in;public static void main(String[] args) {// TODO Auto-generated method stubArrayList<User>student=new ArrayList<User>();try{int count=0;Scanner sc = new Scanner(new File("D:/身份信息.txt"));while(sc.hasNextLine()){String line = sc.nextLine();count++;Scanner lineScanner = new Scanner(line);//為每一行建立一個掃描器lineScanner.useDelimiter(" ");try{String a1 = lineScanner.next();//姓名String a2 = lineScanner.next();//身份證號String a3 = lineScanner.next();//性別String a4 = lineScanner.next();//年齡String a5 = lineScanner.next();//地址System.out.println(a1+a2+a3+a4+a5);try {if (a1.length()==0)throw new Exception("姓名為空");if (a2.length()==0||a2.length()!=18)throw new Exception("身份證號格式錯誤");if (a3.length()==0)throw new Exception("性別未填寫");if (!a3.equals("男") && !a3.equals("女")) throw new Exception("性別格式錯誤");if (a4.length()==0)throw new Exception("年齡未填寫");if (a5.length()==0)throw new Exception("地址未填寫");} catch (Exception e) {System.out.println(e+":"+count+":"+line);}}catch(Exception e){System.out.println(e+":"+count+":"+line);}}}catch(FileNotFoundException e){System.out.println(e);}Collections.sort(student, (User o1,User o2)->{;return o1.getAge()-o2.getAge();});for(User user:student){System.out.println(user.toString());}}}

7.2 如果文件有上萬行文本,出錯的信息可能有很多行,如果將出錯信息直接輸出到控制臺容易被忽略,請問如何解決?

將直接輸出到控制臺改為拋出異常

轉載于:https://www.cnblogs.com/2223ch/p/9979340.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的本周学习总结JAVA的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。