java中输入输出的总括(初学必看) 3

xiaoxiao2022-06-12  36

{ //创建带通配符的文件名过滤器对象 FilenameFilter filter = new DirFilter("w*abc.txt"); File f1 = new File(""); File curdir = new File(f1.getAbsolutePath(),""); //当前目录 System.out.println(curdir.getAbsolutePath()); String[] str = curdir.list(filter); //列出带过滤器的文件名清单 for (int i=0;i<STR.LENGTH;I++) System.out.println("\t"+str[i]); } public boolean accept(File dir, String filename) { boolean yes = true; try { filename = filename.toLowerCase(); yes = (filename.startsWith(prefix)) & (filename.endsWith(suffix)); } catch(NullPointerException e) { } return yes; } } 程序运行时,列出当前目录中符合过滤条件“w*.txt“的文件名清单。结果如下: D:\myjava      Write1.txt      Write2.txt      文件对话框      随机文件操作 于InputStream 和OutputStream 来说,它们的实例都是顺序访问流,也就是说,只能对文件进行顺序地读/写。随机访问文件则允许对文件内容进行随机读/写。在java中,类 RandomAccessFile 提供了随机访问文件的方法。类RandomAccessFile的声明为: public class RandomAccessFile extends Object implements DataInput, DataOutput      File:以文件路径名的形式代表一个文件      FileDescriptor:代表一个打开文件的文件描述      FileFilter & FilenameFilter:用于列出满足条件的文件      File.list(FilenameFilter fnf)      File.listFiles(FileFilter ff)      FileDialog.setFilenameFilter(FilenameFilter fnf) •     FileInputStream & FileReader:顺序读文件 •     FileOutputStream & FileWriter:顺序写文件 •     RandomAccessFile:提供对文件的随机访问支持 类RandomAccessFile则允许对文件内容同时完成读和写操作,它直接继承Object,并且同时实现了接口DataInput和DataOutput,提供了支持随机文件操作的方法      DataInput和DataOutput中的方法 •     readInt(), writeDouble()…      int skipBytes(int n):将指针乡下移动若干字节      length():返回文件长度      long getFilePointer():返回指针当前位置      void seek(long pos):将指针调到所需位置      void setLength(long newLength):设定文件长度 构造方法: RandomAccessFile(File file, String mode)      RandomAccessFile(String name, String mode) mode 的取值 –     “r” 只读. 任何写操作都将抛出IOException。 –     “rw” 读写. 文件不存在时会创建该文件,文件存在时,原文件内容不变,通过写操作改变文件内容。 –     “rws” 同步读写. 等同于读写,但是任何协操作的内容都被直接写入物理文件,包括文件内容和文件属性。 –     “rwd” 数据同步读写. 等同于读写,但任何内容写操作都直接写到物理文件,对文件属性内容的修改不是这样。 例 8.6 随机文件操作。 本例对一个二进制整数文件实现访问操作当以可读写方式“rw“打开一个文件”prinmes.bin“时,如果文件不存在,将创建一个新文件。先将2作为最小素数写入文件,再依次测试100以内的奇数,将每次产生一个素数写入文件尾。 程序如下: import java.io.*; public class PrimesFile { RandomAccessFile raf; public static void main(String args[]) throws IOException { (new PrimesFile()). createprime(100); } public void createprime(int max) throws IOException { raf=new RandomAccessFile("primes.bin","rw");//创建文件对象 raf.seek(0); //文件指针为0 raf.writeInt(2); //写入整型 int k=3; while (k<=max) { if (isPrime(k)) raf.writeInt(k); k = k+2; } output(max); raf.close(); //关闭文件 } public boolean isPrime(int k) throws IOException { int i=0,j; boolean yes = true; try { raf.seek(0); int count = (int)(raf.length()/4); //返回文件字节长度 while ((i<=count) && yes) { if (k % raf.readInt()==0) //读取整型 yes = false; else i++; raf.seek(i*4); //移动文件指针 } } catch(EOFException e) { } //捕获到达文件尾异常 return yes; } public void output(int max) throws IOException { try { raf.seek(0); System.out.println("[2.."+max+"]中有 "+ (raf.length()/4)+" 个素数:"); for (int i=0;i<(int)(raf.length()/4);i++) { raf.seek(i*4); System.out.print(raf.readInt()+" "); if ((i+1)==0) System.out.println(); } } catch(EOFException e) { } System.out.println(); } } 程序运行时创建文件“primes.bin“,并将素数写入其中,结果如下: [2..100]中有 25 个素数: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 相关资源:敏捷开发V1.0.pptx
转载请注明原文地址: https://www.6miu.com/read-4933271.html

最新回复(0)