java学习笔记(十五)

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>//声明一个和文件大小一样的buf
		raf.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);//A
		out.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个byte
			out.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是按段读取,每段的第一个byte
		System.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
	}





你可能感兴趣的:(java学习笔记(十五))