第14章 输人输出 档案 位串流 字符串流
第14章 • 输入 输出 – 档案 – 位串流 – 字符串流
File类别 不同的操作系统对于文件系统路径的设定 各有差别 Windows "C:\Workspace\\CH1 4\\ Linux /home /justin/workspace/ch14
File类别 • 不同的操作系统对于文件系统路径的设定 各有差别 • Windows • Linux "C:\\Workspace\\CH14\\" "/home/justin/workspace/ch14
File类别 File实例用作一个档案或目录的抽象表示 Filefile new File(args[o]) if(fi1e.isFi1e())(/是否为档案 System.out. println(args[0]+"檔案"); System. out. print( fi1e. canRead()?"可读":"不可读"); System. out. print( file. canWrite()?"可写":"不可写"); System. out. println fi1e. length()+"位組");
File类别 • File实例用作一个档案或目录的抽象表示 File file = new File(args[0]); if(file.isFile()) { //是否为档案 System.out.println(args[0] + "檔案"); System.out.print( file.canRead() ?"可读" :"不可读"); System.out.print( file.canWrite() ?"可写" :"不可写"); System.out.println( file.length() +"位組"); }
File类别 else //列出所有的档案及目录 File[ files = file.listFileso Arraylist filelist ew Arraylist( for(int i=0; i< files. length i++)i //先列出目录 if( files[i]. isDirectory()){//是否为目录 /取得路径名 System. out. printin("[+ files [i]. getPath()+ else //档案先存人fi1 elist’待会再列出 filelist. add(files[i])
File类别 else { //列出所有的档案及目录 File[] files = file.listFiles(); ArrayList fileList = new ArrayList(); for(int i = 0; i < files.length; i++) { //先列出目录 if(files[i].isDirectory()) {//是否为目录 //取得路径名 System.out.println("[" + files[i].getPath() + "]"); } else { //档案先存入fileList,待会再列出 fileList.add(files[i]); } }
Fle类别 //列出档案 for(file f: filelist System. out. println(f tostring ())i System. out. println()i
File类别 //列出档案 for(File f: fileList) { System.out.println(f.toString()); } System.out.println(); }
RandomAccessFile类另 File file =new File(args [0] //建立 Randomaccessfile实例并以读写模式开后档案 Randomaccess file randomaccessFile new RandomAccessFile(file,"rw") for (int i=0; i< students. length; i++) //使用对应的 write方法写人数据 randomAccessFile writechars(students[i]. getName())i randomAccessFile. writeInt (students[i]. getscore())i
RandomAccessFile类别 File file = new File(args[0]); //建立RandomAccessFile实例并以读写模式开启档案 RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); for(int i = 0; i < students.length; i++) { //使用对应的write方法写入数据 randomAccessFile.writeChars(students[i].getName()); randomAccessFile.writeInt(students[i].getScore()); }
RandomAccessFile类别 //使用sek()方法操作存取位置 randomAccessfile seek((num-1)* Student size)i Student student new Student ( //使用对应的read方法读出数据 student setName(readName(randomAccessfile))i student. setscore(randomAccessFile readInt() System. out. println("*E: " student getName())i System. out. println("*:"+ student. getscore())i //设定矣闭档案 randomAccessFile close()i
RandomAccessFile类别 //使用seek()方法操作存取位置 randomAccessFile.seek((num-1) * Student.size()); Student student = new Student(); //使用对应的read方法读出数据 student.setName(readName(randomAccessFile)); student.setScore(randomAccessFile.readInt()); System.out.println("姓名:" + student.getName()); System.out.println("分数:" + student.getScore()); //设定关闭档案 randomAccessFile.close();
RandomAccessFile类另 private static String readName(RandomAccessFilerandomAccessfile throws IOException char[ name new char [l5] for(int i=0; i< namelength; i++) name [i]= randomAccessfile readchar()i //捋空字符取代为空格符并传回 return new String(name).replace('\0,Ii)i
RandomAccessFile类别 private static String readName(RandomAccessFile randomAccessfile) throws IOException { char[] name = new char[15]; for(int i = 0; i < name.length; i++) name[i] = randomAccessfile.readChar(); //将空字符取代为空格符并传回 return new String(name).replace('\0', ' '); }
RandomAccessFile类另 读写档案时几个必要的流程 开后档案并指定读写方式 一使用对应的写入方法 使用对应的读出方法 尖闭档案
RandomAccessFile类别 • 读写档案时几个必要的流程 – 开启档案并指定读写方式 – 使用对应的写入方法 – 使用对应的读出方法 – 关闭档案
Inputstream Outputstream 数据流动抽象化为一个串流( Strean) 檔案 HuLC 體 0 00
InputStream、OutputStream • 数据流动抽象化为一个串流(Stream)