将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