<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      鐘海清

      導(dǎo)航

      互評(píng)作業(yè)

      1. 字節(jié)流與二進(jìn)制文件
        ==========================

      我的代碼

      class Student {
      	private int id;
      	private String name;
      	private int age;
      	private double grade;
      
      	public Student() {
      
      	}
      	public Student(int id, String name, int age, double grade) {
      		this.id = id;
      		this.setName(name);
      		this.setAge(age);
      		this.setGrade(grade);
      	}
      	public int getId() {
      		return id;
      	}
      	public void setId(int id) {
      		this.id = id;
      	}
      	public String getName() {
      		return name;
      	}
      	public void setName(String name) {
      		if (name.length() > 10) {
      			throw new IllegalArgumentException("name's length should <=10 " + name.length());
      		}
      		this.name = name;
      	}
      	public int getAge() {
      		return age;
      	}
      	public void setAge(int age) {
      		if (age <= 0) {
      			throw new IllegalArgumentException("age should >0 " + age);
      		}
      		this.age = age;
      	}
      	public double getGrade() {
      		return grade;
      	}
      	public void setGrade(double grade) {
      		if (grade < 0 || grade > 100) {
      			throw new IllegalArgumentException("grade should be in [0,100] " + grade);
      		}
      		this.grade = grade;
      	}
      	@Override
      	public String toString() {
      		return "Student [id=" + id + ", name=" + name + ", age=" + age + ", grade=" + grade + "]";
      	}
      }
      
      public class Main {
      	public static void main(String[] args)
                {         
                   String fileName="d:\\student.data";
                    try(DataOutputStream dos=new DataOutputStream(new FileOutputStream(fileName)))
                   {
                        Student[] stu=new Student[3];
                        stu[0]=new Student(1,"zhangsan",19,65.0);
                        stu[1]=new Student(2,"lisi",19,75.0);
                        stu[2]=new Student(3,"wangwu",20,85.0);
                        for(Student stu1:stu) {
                           dos.writeInt(stu1.getId());
                           dos.writeUTF(stu1.getName());
                           dos.writeInt(stu1.getAge());
                           dos.writeDouble(stu1.getGrade());
                      }
                       
                  } catch (FileNotFoundException e) {
                      e.printStackTrace();
                       System.out.println("1");
                  } catch (IOException e) {
                       e.printStackTrace();
                     System.out.println("2");
                  }
                  try(DataInputStream dis=new DataInputStream(new FileInputStream(fileName)))
                   {
                      while(dis!=null) {
                          int id=dis.readInt();
                          String name=dis.readUTF();
                          int age=dis.readInt();
                           double grade=dis.readDouble();
                           Student stu=new Student(id,name,age,grade);
                          System.out.println(stu);
                      }
                       
                      
                  } catch (FileNotFoundException e) {
                      e.printStackTrace();
                       System.out.println("3");
                  } catch (IOException e) {
                      e.printStackTrace();
                      System.out.println("4");
                 }
                   
               }
      

      我的總結(jié)

      學(xué)會(huì)了使用try..with...resouces關(guān)閉資源,原來(lái)可以用用finally來(lái)關(guān)閉文件,但也要判斷是否可以關(guān)閉資源的問題。而用try…with…resource可以解決該問題,當(dāng)try語(yǔ)句塊運(yùn)行結(jié)束時(shí),相應(yīng)資源會(huì)被自動(dòng)關(guān)閉。這是因?yàn)镕ileInputStream 實(shí)現(xiàn)了java中的java.lang.AutoCloseable。所有實(shí)現(xiàn)了這個(gè)接口的類都可以在try-with-resources結(jié)構(gòu)中使用。 
      
      1. 字符流與文本文件
        ==========================

      我的代碼

      public class Main {
      	public static void main(String[] args) throws IOException {
      		String FileName = "D:\\TSBrowserDownloads\\Students.txt";
      		BufferedReader br = null;
      		try {
      			br = new BufferedReader(new InputStreamReader(new FileInputStream(FileName), "UTF-8"));
      			String line = null;
      			while ((line = br.readLine()) != null)
      				System.out.println(line);
      		} finally {
      			if (br != null) {
      				br.close();
      			}
      		}
      	}
      
      }
      public static void ListreadStudents(String fileName){
                   ArrayList<Student> StudentList=new ArrayList<Student>();
                  BufferedReader br = null;
                  try {
                      br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName),"UTF-8"));
                     while(br!=null) {
                          String line=br.readLine();
                          String[] stu=line.split("\\s+");
                          int id=Integer.parseInt(stu[0]);
                           String name=stu[1];
                           int age=Integer.parseInt(stu[2]);
                           double grade=Double.parseDouble(stu[3]);
                           Student Stu=new Student(id,name,age,grade);
                          StudentList.add(Stu);          
                      }                
                  } finally{
                      if (br!=null){
                          br.close();
                       }
                   }
       }
      

      我的總結(jié)

      要以UTF-8打開文件,否則會(huì)亂碼。BufferedReader只用read和ReadLine方法,得用實(shí)驗(yàn)提到的"\\s+"的方法將讀到的一整行用分隔符來(lái)分開,再用類型轉(zhuǎn)換來(lái)讀。
      

      2.緩沖流

      我的代碼

      public class test {
      	@Test
      	public void test() {
      		String FILENAME = "test.txt";
      		long begin = System.currentTimeMillis();
      		Scanner scanner = null;
      		try {
      			scanner = new Scanner(new File(FILENAME));
      			while (scanner.hasNextLine()) {// 只是讀出每一行,不做任何處理
      				scanner.nextLine();
      			}
      		} catch (FileNotFoundException e) {
      			e.printStackTrace();
      		} finally {
      			scanner.close();
      		}
      		long end = System.currentTimeMillis();
      		System.out.println("last " + (end - begin));
      		System.out.println("read using Scanner done");
      	}
      
      	@Test
      	public void Bufftest() {
      		String FILENAME = "test.txt";
      		long begin = System.currentTimeMillis();
      		BufferedReader br = null;
      		try {
      			br = new BufferedReader(new FileReader(new File(FILENAME)));
      			while (br.readLine() != null) {
      			}
      			;// 只是讀出,不進(jìn)行任何處理
      		} catch (FileNotFoundException e) {
      			e.printStackTrace();
      		} catch (IOException e) {
      			e.printStackTrace();
      		} finally {
      			try {
      				br.close();
      			} catch (IOException e) {
      				e.printStackTrace();
      			}
      		}
      		long end = System.currentTimeMillis();
      		System.out.println("last " + (end - begin));
      		System.out.println("read using BufferedReader done");
      	}
      }
      

      我的總結(jié)

      緩沖流BufferedReader的方法要比Scanner的方法快很多
      

      posted on 2019-11-26 22:24  haiqingz  閱讀(265)  評(píng)論(0)    收藏  舉報(bào)

      主站蜘蛛池模板: 激情97综合亚洲色婷婷五| 国产精品高清一区二区三区| 好紧好爽午夜视频| 五月天国产成人av免费观看| 久久婷婷五月综合色精品| 一本色道久久—综合亚洲| 龙井市| 亚洲av永久无码精品水牛影视| 四虎成人在线观看免费| 亚洲男女羞羞无遮挡久久丫| 国产欧亚州美日韩综合区| 久久被窝亚洲精品爽爽爽| 超清无码一区二区三区| 国产精品国产精品国产专区不卡 | 美乳丰满人妻无码视频| 国产亚洲精品日韩av在| 国产福利姬喷水福利在线观看| 日本边添边摸边做边爱| 欧美成人片一区二区三区| 日本区二区三区不卡视频| 亚洲中文字幕国产精品| 国语自产精品视频在线看| 日韩人妻精品中文字幕| 精品自拍偷拍一区二区三区| 国产精品va在线观看无码| 国产精品夜夜春夜夜爽久久小说| 亚洲熟妇熟女久久精品综合| 果冻传媒色av国产在线播放 | 亚欧美闷骚院| 久久精品免视看成人国产| 中文字幕精品人妻丝袜| 亚洲成av人片无码天堂下载| 内射视频福利在线观看| 免费观看在线A级毛片| 国产精品天堂蜜av在线播放| 精品国模一区二区三区| 国产精品亚洲二区在线看| 国产桃色在线成免费视频| 日韩精品一区二区亚洲专区| 亚洲成在人天堂一区二区| 日韩欧美卡一卡二卡新区|