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);
}
}
}
输出一个目录中所有扩展名为.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();
}
}
未完