將Student對象(屬性:int id, String name,int age,double grade)寫入文件student.data、從文件讀出顯示。java
//201521123117 Student類: public void writeData(PrintWriter out) { out.println(id + "|" + name + "|" + age + "|" + grade ); } public void readData(Scanner in) { String line = in.nextLine(); String[] tokens = line.split("\\|"); id =Integer.parseInt(tokens[0]); name =tokens[1]; age = Integer.parseInt(tokens[2]); grade = Double.parseDouble(tokens[3]); } Main函數: try { // save all employee records to the file employee.txt PrintWriter out = new PrintWriter("student.txt"); writeData(stu, out); out.close(); // retrieve all records into a new array Scanner in = new Scanner(new FileReader("student.txt")); Student[] newStaff = readData(in); in.close(); for (Student e : newStaff) System.out.println(e); } catch (IOException exception) { exception.printStackTrace(); } } private static void writeData(Student[] students, PrintWriter out) throws IOException { out.println(students.length); for (Student e : students) e.writeData(out); } private static Student[] readData(Scanner in) { int n = in.nextInt(); in.nextLine(); Student[] students = new Student[n]; for (int i = 0; i < n; i++) { students[i] = new Student(); students[i].readData(in); } return students; }
生成文件大小:50字節 分析:第一行的「3」表明數組的大小,佔3個字節;一個id佔1個字節,共佔3個字節;英文字符串每一個字母佔1個字節,故共佔11個字節;age佔2個字節,共6個字節;grade佔4個字節,共佔12個字節;分隔符有3個,共佔3個字節;行末尾共佔2個字節。
0字節 close方法中會自動調用flush()函數,flush()函數做用爲清空緩衝區數據。若調用PrintWriter的println方法,但在後面不close的話,緩衝區中的數據就會丟失。
參考:本題具體要求見流與文件實驗任務書-題目1-2.1正則表達式
參考代碼:TextFileTest.java編程
BufferedReader是在字符輸入流中讀取文本,緩衝各個字符,從而提供字符、數組和行的高效讀取,使用緩衝能夠減小IO次數。讀取數據的速度要比Scanner塊。
使用BufferedWriter寫入文件的速度要更快;由於BufferedWriter使用了緩衝技術。
參考:本題具體要求見流與文件實驗任務書-題目1-2.2到2.3
參考代碼:BufferedReaderTest.java
JUnit4經常使用註解
JUnit4學習數組
出現亂碼;由於FileReader會按照系統默認的字符集(如GBK)來解碼,可是EncodeTest.txt 文件使用了UTF-8編碼。將UTF-8編碼的字符使用GBK編碼來解析,會出現亂碼狀況。
解決辦法編輯器
//201521123117 pubilc class InputStreamReaderTest{ public static void main(Srting[] args) throws Exception{ BuffererdReader br = null; try { br = new BufferedReader(new InputStreamReader(new FileInputStream("d:/EncodeTest.txt"),"UTF-8)); String line = null; while((line=br.readLine())!=null) System.out.println(line); }finally{ if (br!=null){ br.close(); } } } }
//201521123117 public static void main(Srting[] args) throws Exception{ String dst = "e:/dst.txt"; String src = "e:/src.txt"; convertGBK2UTF8( src, dst); } public static void convertGBK2UTF8(String src, String dst)throws IOException{ BufferedReader br = null; br = new BufferedReader(new FileReader(src)); OutputStreamWriter osw=new OutputStreamWriter(FlieOutputStream(dst),"UTF-8"); String line =null; while((line=br.readLine())!=null){ osw.write(line+"\n"); } br.close(); osw.close();
參考:InputStreamReaderTest.java與教學PPT函數
Student[] student=new Student[3]; student[0] = new Student(1,"zheng", 20,98); student[1] = new Student(2,"chen",19,80); student[2] = new Student(3,"li",21,85); //201521123117 DataOutputStream dos=null; try{ dos=new DataOutputStream(new BufferedOutputStream(new FileOutputStream("student2.txt"))); for(int i=0;i<3;i++){ dos.writeInt(student[i].getId()); dos.writeUTF(student[i].getName()); dos.writeInt(student[i].getAge()); dos.writeDouble(student[i].getGrade()); } }finally{ dos.close(); } //201521123117 DataInputStream dis=null; try{ dis=new DataInputStream(new BufferedInputStream(new FileInputStream("student2.txt"))); Student[] student2=new Student[3]; for(int i=0;i<3;i++){ Student student1=new Student(dis.readInt(),dis.readUTF(),dis.readInt(),dis.readDouble()); student2[i]=student1; } for(int i=0;i<3;i++) { System.out.println("student"+"[id=" + student2[i].getId() + ",name=" + student2[i].getName() + ",age=" + student2[i].getAge()+",grade="+ student2[i].getGrade()+"]"); } }catch(Exception e){ System.out.println(e); }finally{ dis.close(); }
生成文件大小:65字節 分析:一個int型4字節,id和age共24字節;一個double型8字節,grade共24字節;一個英文字符1字節,name共11字節;一個標識符2字節,共6字節;故文件大小爲65字節。 將該文件大小和題目1生成的文件對比是大了。由於DataOutputStream寫入文件是按照不一樣的數據類型寫入,每一個數據類型佔必定的大小,如int佔4個字節。
//201521123117 ObjectOutputStream out =new ObjectOutputStream(new FileOutputStream("student.txt")); out.writeObject(student); out.close(); ObjectInputStream in =new ObjectInputStream(new FileInputStream("student.txt")); Student[] newStudent =(Student[]) in.readObject(); in.close(); for(Student e: newStudent) System.out.println(e);
參考:本題具體要求見流與文件實驗任務書-題目1-1學習
編寫public static List
運行結果:
3d
FileInputStream:從文件中讀取數據; InputStreamReader:讀UTF-8格式的文件; BufferedReader:使用緩衝技術,讀取速度更快;
實驗文件:Students.txt
參考:TextFileTest目錄下TextFileTest.javacode
編寫一個程序,能夠根據指定目錄和文件名,搜索該目錄及子目錄下的全部文件,若是沒有找到指定文件名,則顯示無匹配,不然將全部找到的文件名與文件夾名顯示出來。
//201521123117 public class findFilename{ public static void main(String[] args){ //TODO Auto-generated method stub findFile("E:\\做業","src.txt"); } public static void findFile(String path,String filename) { //TODO Auto-generated method stub File file =new File(path); String[] fileName = file.list(); for(String string:fileName){ File files =new File(file.getAbsolutePath(),string); if(string.equals(filename)) System.out.println(files.getAbsolutePath()); if(files.isDirectory()) findFile(files.getAbsolutePath(),filename); } } }
運行結果:
關鍵代碼:
//201521123117 Queue<File> queue=new LinkedList<>(); queue.offer(new File(path)); while(!queue.isEmpty()){ File file =queue.poll(); String[] fileName = file.list(); for(String string:fileName){ File files =new File(file.getAbsolutePath(),string); if(string.equals(filename)) System.out.println(files.getAbsolutePath()); if(files.isDirectory()) queue.offer(files); } }
運行結果:
參考資料:判斷文件的編碼格式
//201521123117 Public class checknumber{ Public static void main (string[] aegs) { //TODO Atuto-generated method stub Scanner in =new Scanner(System.in); While(in.hasNext()){ String str=in.nextline(); System .out.printLn(checknumber(str)); } } public static boolean checknumber (string str){ String patternstring ="[+-]?[0-9]"; return pattern.matches(patternstring,str); } }
運行結果:
參考:本題具體要求見流與文件實驗任務書-題目3