java学习笔记(十五)

来源:互联网 发布:吊带承重数据 编辑:程序博客网 时间:2024/05/21 08:44

1.java.io.File基本的API

File(String)

long length()

long lastModified()

String getName()

String getPath()

boolean exists()

boolean dir.isFile()

boolean dir.isDirectory()

boolean mkdir()//创建一个目录

boolean mkdirs()//创建路径上的很多不存在的目录

boolean delete()

boolean creatNewFile()throws IOexception

File[] listFile()

2.回调模式FileFilter

File[] listFile(FileFilter)

例子:

public static void main(String[] args) {File dir=new File(".");//File[] list=dir.listFiles(new MyFilter());File[] list=dir.listFiles(<span style="color:#ff0000;">new FileFilter(){//匿名内部类实现public boolean accept(File pathname){System.out.println(pathname.getName());return pathname.getName().startsWith(".");}});System.out.println(Arrays.toString(list));}/*static class MyFilter implements FileFilter{//静态内部类实现public boolean accept(File pathname){System.out.println(pathname.getName());//跟踪代码return pathname.getName().startsWith(".");}}*/

3.java.io.RandomAccessFile可以访问(读/写)一个文件中任意位置的字节信息

RandomAccessFile(File,String)throws FileNotFoundException

arg0表示要访问的文件

arg1表示访问的模式

RandomAccessFile维护一个指针,指向要读写的位置,指针会随着读写自动后移

int read()//每读完一个数据,指针就会向后移动一下下

seek()//可以用来移动指针的位置

void write(int)//写数据

long getFilePointer()//获取指针位置

例子:

public static void main(String[] args) throws IOException,FileNotFoundException{File file=new File("demo.txt");//File file=new File("src/demo.txt");RandomAccessFile raf=new RandomAccessFile(file,"rw");<span style="white-space:pre"></span>//以读写模式打开文件System.out.println(raf.getFilePointer());<span style="white-space:pre"></span>//打印目前指针的位置int b=raf.read();<span style="white-space:pre"></span>//读取目前指针指向位置的内容System.out.println(Integer.toHexString(b));raf.close();<span style="white-space:pre"></span>//关闭文件String mail="1,11";System.out.println(readMail("123.txt",mail));<span style="white-space:pre"></span>//调用readMail()函数lowcase("demo.txt");<span style="white-space:pre"></span>//将文件中的大写字母全部变成小写字母}
<span style="white-space:pre"></span>//按照code来读取相应位置的内容public static String readMail(String filename,String code)throws IOException{File file=new File(filename);RandomAccessFile raf=new RandomAccessFile(file,"r");<span style="white-space:pre"></span>//以读模式打开文件String[] idx=code.split(",");<span style="white-space:pre"></span>//对code进行切分StringBuilder buf=new StringBuilder();for(int i=0;i<idx.length;i++){String s=idx[i];raf.seek(Integer.parseInt(s));<span style="white-space:pre"></span>//移动指针int b=raf.read();<span style="white-space:pre"></span>//读出内容char c=(char)b;buf.append(c);}raf.close();return buf.toString();}//利用123.txt.加密一段原文,返回代码序列public static void lowcase(String filename)throws IOException{File file=new File(filename);RandomAccessFile raf=new RandomAccessFile(file,"rw");<span style="white-space:pre"></span>//以读写模式打开文件long pos=0;<span style="white-space:pre"></span>//文件指针位置while(pos<file.length()){<span style="white-space:pre"></span>//判断是否到达文件结尾int b=raf.read();if(b>='A'&&b<='Z'){<span style="white-space:pre"></span>//b是大写字母b=b-'A'+'a';<span style="white-space:pre"></span>//大写变小写raf.seek(raf.getFilePointer()-1);<span style="white-space:pre"></span>//指针回退raf.write(b);<span style="white-space:pre"></span>//执行完写操作,指针也会向下移动一个位置,因此就不用在应用seek来调整指<span style="white-space:pre"></span>//针的位置}pos++;<span style="white-space:pre"></span>//每处理一次要对已处理长度进行一次自增}raf.close();<span style="white-space:pre"></span>//关闭文件}
<span style="white-space:pre"></span>public static void main(String[] args) throws IOException{File file=new File("demo.txt");RandomAccessFile raf=new RandomAccessFile(file,"rw");<span style="white-space:pre"></span>//以读写模式打开文件byte[] buf=new byte[5];<span style="white-space:pre"></span>//设置buf的大小,也就是一次读取的大小//将文件中数据读取到byte缓冲区,返回读取数量int count=raf.read(buf);<span style="white-space:pre"></span>//调用带有参数的read方法,一次将buf大小的数据读入System.out.print(count+",");<span style="white-space:pre"></span>//count表示读取的数据大小System.out.println(Arrays.toString(buf));raf.seek(raf.length());<span style="white-space:pre"></span>//移动到文件尾raf.write(buf,1,3);<span style="white-space:pre"></span>//从第一个位置开始,将buf其后连续的3个英文字符写入到file中raf.close();<span style="white-space:pre"></span>//关闭文件}/*将一个小文件读取到内存的byte数组中*/public static byte[] read(String filename) throws IOException{File file=new File(filename);RandomAccessFile raf=new RandomAccessFile(file,"r");<span style="white-space:pre"></span>//以读模式打开文件int length=(int) raf.length();<span style="white-space:pre"></span>//得到文件长度byte[] buf=new byte[length];<span style="white-space:pre"></span>//声明一个和文件大小一样的bufraf.read(buf);<span style="white-space:pre"></span>//一次读取一批,一次就读完了raf.close();<span style="white-space:pre"></span>//关闭文件return buf;}

4.java.io.InputStream 输入流  读到文件尾返回-1

  java.io.OutputStream输出流 

例子:

输入流实例:

public static void main(String[] args) throws IOException{readDemo();readByBufferDemo();}private static void readByBufferDemo()throws IOException{InputStream in=new FileInputStream("demo2.txt");<span style="white-space:pre"></span>//输入流形式打开文件byte[] buf=new byte[10];//count 是读取的数据个数:1~10>0,如果-1到文件尾int count;while((count=in.read(buf))!=-1){<span style="white-space:pre"></span>//遍历文件System.out.println(toHexString(buf));}in.close();}
<span style="white-space:pre"></span>//以十六进制形式显示文件内容,只显示后16位private static String toHexString(byte[] ary){StringBuilder buf=new StringBuilder();for(int i=0;i<ary.length;i++){byte b=ary[i];<span style="white-space:pre"></span>//得到一个字节的内容int a=b & 0xff;<span style="white-space:pre"></span>//利用掩码运算,去除高24位String hex=Integer.toHexString(a);<span style="white-space:pre"></span>//将其转换成16进制buf.append(hex).append(" ");}return buf.toString();}public static void readDemo()throws IOException{InputStream in=new FileInputStream("demo.txt");<span style="white-space:pre"></span>//输入流打开文件//读取文件的一个byte,无符号数填充到int的低八位//0x000000ff~0x00000000//如果读到文件尾,返回-1//将一个文件迭代输出int b;while((b=in.read())!=-1){System.out.println(b);}in.close();<span style="white-space:pre"></span>//关闭文件}
输出流实例
public static void main(String[] args) throws IOException{writeDemo();}private static void writeDemo()throws IOException{OutputStream out=new FileOutputStream("test.txt");<span style="white-space:pre"></span>//以输出流打开文件//写出一个int的低八位out.write(65);//Aout.write(66);//汉字(GBK/GB2312)编码方式out.write(0xbd);out.write(0xd3);out.write(0xd6);out.write(0xd0);out.write(0xbf);out.write(0xda);//写一个byte//write的重载形式byte[] buf={98,99,(byte)0xd6,(byte)0xd0};out.write(buf);<span style="white-space:pre"></span>//向文件中写入buf中的内容out.write(buf, 2, 2);<span style="white-space:pre"></span>//向文件中写一个数组的一部分,buf中从第2个位置开始写两个//按照GBK编码将试试吧进行编码,然后写到buf中out.write("试试吧".getBytes("GBK"));<span style="white-space:pre"></span>//将“试试吧”以GBK编码方式写入文件中out.close();}

复制一个文件实例

public static void copy(String src,String dst)throws IOException{InputStream in=new FileInputStream(src);<span style="white-space:pre"></span>//原文件为输入流OutputStream out=new FileOutputStream(dst);<span style="white-space:pre"></span>//目的文件为输出流byte[] buf=new byte[1024];int c;while((c=in.read(buf))!=-1){<span style="white-space:pre"></span>//一次读1024个byteout.write(buf,0,c);<span style="white-space:pre"></span>//最后一次可能不满,这样写保险}in.close();<span style="white-space:pre"></span>//关闭文件out.close();}

<span style="white-space:pre"></span>//BufferedInputStream,BufferedOutputStream会提高读取的效率,但是也不是说所有的读写操作都要用这个public static void main(String[] args) throws IOException{//打开带有输入缓冲的流,当读取流中的数据时,BIS会将数据成块读取到内存数组,然后再读取出来BufferedInputStream in=new BufferedInputStream(new FileInputStream("demo.txt"));int b=in.read();<span style="white-space:pre"></span>//b是按段读取,每段的第一个byteSystem.out.println(b);in.close();//创建带有输出缓存的流,BufferedOutputStream可以为任意的流提供输出缓冲区管理,写出的数据先缓冲到BufferedOutputStream的byte数组中//在缓冲满了以后,一次性的写到目标流里面BufferedOutputStream out=<span style="white-space:pre"></span>//这里直接写OutputStream,下面两个都是它的子类new BufferedOutputStream(new FileOutputStream("test2.txt"));out.write(0x41);//写出到缓存中out.flush();//将流中的缓冲区强行进行写盘out.close();//关闭一个流时才会强行的将缓冲区中的东西写出去,关闭流时默认执行flush}





0 0