java API 文件,I/O

File類的listFiles方法可以獲取到當前目錄下的所有內容。

import java.io.File;

public class testListFiles {
	public static void main(String[] args){
		File dir = new File(".");
		File[] subs = dir.listFiles();
		for(File sub:subs){
			System.out.println(sub);
		}
	}
}


FIileFilter用於抽象路徑名的過濾器。
輸出一個目錄中所有擴展名爲.txt的文件。

import java.io.File;
import java.io.FileFilter;

public class testFileFilter {
	public static void main(String[] args){
		File dir = new File(".");
		File[] subs = dir.listFiles(new FileFilter(){
			public boolean accept(File file){
				return file.getName().endsWith(".txt");
			}
		});
		for(File sub:subs){
			System.out.println(sub);
		}
	}
}
使用RandomAccessFile類進行文件的讀和寫:

import java.io.IOException;
import java.io.RandomAccessFile;

public class testRandomAccessFile {

	public static void main(String[] args) throws IOException {
		
		RandomAccessFile raf = new RandomAccessFile("raf.dat","rw");
		//寫出一個字節,寫的是int值的低八位
		raf.write(1);
		raf.close();
		
		RandomAccessFile raf2 = new RandomAccessFile("raf.dat","r");
		//讀取一個字節
		int d = raf2.read();
		System.out.println(d);
		raf.close();
	}
}

批量讀寫:

import java.io.IOException;
import java.io.RandomAccessFile;

public class testPILIANGDUXIE {
	public static void main(String[] args) throws IOException{
		RandomAccessFile raf = new RandomAccessFile("raf.dat","rw");
		//將字符串按默認編碼轉換爲字符數組
		byte[] buf = "helloworld".getBytes();
		//將字節數組中所有字節一次性寫出
		raf.write(buf);
		raf.close();
		RandomAccessFile raf2 = new RandomAccessFile("raf.dat","r");
		//創建長度爲10的字節數組
		byte[] buf2 = new byte[10];
		//嘗試讀取10個字節存入數組,返回值爲讀取的字節量
		int len = raf2.read(buf);
		System.out.println("讀取到了:"+ len + "字節");
		System.out.println(new String(buf));
		raf2.close();
	}
}

對象操作文件的指針:


讀取指針位置:

import java.io.IOException;
import java.io.RandomAccessFile;

public class testPointer {
	public static void main(String[] args) throws IOException{
		RandomAccessFile raf = new RandomAccessFile("raf.dat","r");
		//輸出指針位置,默認從0開始(文件的第一個字節位置)
		System.out.println("指針位置"+ raf.getFilePointer());
		raf.close();
	}
}

使用指針:

import java.io.IOException;
import java.io.RandomAccessFile;

public class testPointer {
	public static void main(String[] args) throws IOException{
		RandomAccessFile raf = new RandomAccessFile("raf.dat","r");
		//輸出指針位置,默認從0開始(文件的第一個字節位置)
		System.out.println("指針位置"+ raf.getFilePointer());
		
		//讀取world,需要將hello這5個字節跳過
		raf.skipBytes(5);
		//讀取
		System.out.println("指針位置"+ raf.getFilePointer());
		byte[] buf = new byte[5];
		raf.read(buf);
		System.out.println(new String(buf));
		System.out.println("指針位置"+ raf.getFilePointer());
		
		//將指針移動到文件開始位置
		raf.seek(0);
		System.out.println("指針位置"+ raf.getFilePointer());
		
		raf.close();
	}
}

字節流:

FileOutputStream

覆蓋寫:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class testFos {
	public static void main(String[] args) throws IOException {
		//創建文件字節輸出流
		//FileOutputStream fos = new FileOutputStream(new File("fos.dat"));
		FileOutputStream fos = new FileOutputStream("fos.dat");
		
		//寫出一組字節
		fos.write("helloworld".getBytes());
		fos.close();
	}
}

追加寫:

import java.io.FileOutputStream;
import java.io.IOException;

public class testFosByAppend {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException  {
		// TODO Auto-generated method stub
		FileOutputStream fos = new FileOutputStream("fos.dat",true);
		fos.write("helloworld".getBytes());
		fos.close();
	}

}

FileInputStream

import java.io.FileInputStream;
import java.io.IOException;

public class testFis {

	/**
	 * @throws IOException 
	 * @param args
	 */
	public static void main(String[] args) throws IOException   {
		// TODO Auto-generated method stub
		FileInputStream fis = new FileInputStream("fos.dat");
		int d  = -1;
		while((d = fis.read())!= -1){
			System.out.println((char)d  + " ");
		}
		fis.close();
	}

}

複製:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class testCopyFile {
	public static void main(String[] args) throws IOException{
		FileInputStream fis = new FileInputStream("fos.dat");
		FileOutputStream fos = new FileOutputStream("fos_copy.dat");
		int len = -1;
		byte[] buf = new byte[32];
		while((len = fis.read(buf)) != -1){
			fos.write(buf,0,len);
		}
		System.out.println("複製完畢");
		fis.close();
		fos.close();
	}
}

實現基於緩存的文件複製:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class testBisandBos {
	public static void main(String[] args) throws IOException{
		
		//創建緩衝字符輸入流
		FileInputStream fis = new FileInputStream("fos.bat");
		BufferedInputStream bis = new BufferedInputStream(fis);
		
		//創建緩衝字符輸出流
		FileOutputStream fos = new FileOutputStream("fos_copy2.dat");
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		
		int d = -1;
		while((d = bis.read())!= -1){
			bos.write(d);
		}
		System.out.println("複製完畢");
		bis.close();
		bos.close();
	}
}

對象序列化:

import java.io.Serializable;

public class emp implements Serializable{
	private String name;
	private int age;
	private int id;	
	public emp(String name, int age, int id) {
		this.name = name;
		this.age = age;
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	@Override
	public String toString() {
		return "emp [name=" + name + ", age=" + age + ", id=" + id + "]";
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + id;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		emp other = (emp) obj;
		if (age != other.age)
			return false;
		if (id != other.id)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
}



對象序列化:

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class testOOS {
	public static void main(String[] args) throws IOException{
		FileOutputStream fos = new FileOutputStream("emp.obj");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		emp e = new emp("小宇",20,1);
		oos.writeObject(e);
		System.out.println("序列化完畢");
		oos.close();
	}
}


對象反序列化:

import java.io.FileInputStream;
import java.io.ObjectInputStream;

public class testOIS {
	public static void main(String[] args) throws Exception{
		FileInputStream fis = new FileInputStream("emp.obj");
		ObjectInputStream ois = new ObjectInputStream(fis);
		emp e = (emp)ois.readObject();
		System.out.println("反序列化完畢");
		System.out.println(e);
		ois.close();
	}
}


字符流:

import java.io.IOException;
import java.io.PrintWriter;

public class testPrintWriter {
	public static void main(String[] args) throws IOException{		
		PrintWriter pw = new PrintWriter("pw.txt");
		pw.println("張磊是傻逼");
		pw.println("張娟愛張磊");
		pw.println("byebye");
		pw.close();
	}
}


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class testBufferedReader {
	public static void main(String[] args) throws IOException{
		FileInputStream fis = new FileInputStream("pw.txt");
		InputStreamReader isr = new InputStreamReader(fis);
		BufferedReader br = new BufferedReader(isr);
		String line = null;
		while((line = br.readLine())!= null){
			System.out.println(line);
		}
		br.close();
	}
}




未完


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章