file类的创建和判断
构造方法:
File(String pathname);
File(String parent,String child);//分别输入父路径和子路径,创建对象
File(File parent,String child); //根据给定的父目录对象,子路径
//成员方法
createNewFile();
mkdir();
mkdirs();
isDirectory();//判断file对象是否为目录
isFile();//判断file对象是否为文件
exists();//判断file对象是否存在
File类的获取
//File类的获取
getAbsolutePath();//获取绝对路径
getPath();//获取文件的相对路径
getName();//获取文件名
list();//获取指定目录下所有文件(夹)名称数组
listFiles();//获取指定目录下所有文件(夹)File数组
读取字符流
Reader reader = new FileReader("lib/1.txt");
int ch;
//重复执行reader.read()会自动读取下一个
while((ch = reader.read() )!= -1) {
System.out.println(ch);
}
reader.close();
}
写入字符流
void write(int ch);
void write(char[] chs,int index,int len);
void write(String str);
IO流拷贝文件
/**
一次读一个字符
1、创建字符输入流对象,关联数据源文件
2、创建字符输出流对象,关联目的地文件
3、定义变量,记录读取到的内容
4、循环读取只要条件满足就一直读,并将读取到的内容赋值给变量
5、将读取的数据写入到目的地文件中
6、释放资源
*/
FileReader fReader = new FileReader("lib/1.txt");
//如果目的地文件不存在,程序可以自动存在
FileWriter fWriter = new FileWriter("lib/2.txt");
int len;
while((len = fReader.read())!=-1) {
fWriter.write(len);
}
fReader.close();
fWriter.close();
//一次读一个数组
FileReader fReader = new FileReader("lib/1.txt");
FileWriter fWriter = new FileWriter("lib/3.txt");
char[] chs = new char[1024];
int len;
//先读取字符,再把读取到的有效字符数赋值给len,(len = fReader.read())整体和-1比较
while((len = fReader.read(chs)) != -1) {
fWriter.write(chs,0,len);
}
fReader.close();
fWriter.close();
字符缓冲流
/**
BufferedReader:
成员方法:public String readLine();
BufferedWriter:
成员方法:public void newLine();换行
*/
//读取一个字符
BufferedReader bufferedReader = new BufferedReader(new FileReader("lib/1.txt"));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("lib/4.txt"));
int len;
while((len = bufferedReader.read()) != -1) {
bufferedWriter.write(len);
}
bufferedReader.close();
bufferedWriter.close();
//一次读一行
BufferedReader br = new BufferedReader(new FileReader("lib/1.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("lib/5.txt"));
String str;
while((str = br.readLine())!= null) {
bw.write(str);
bw.newLine();
}
br.close();
bw.close();
字节流
//每次读取单个字节
InputStream iStream = new FileInputStream("lib/1.png");
OutputStream oStream = new FileOutputStream("lib/b.png");
int len;
while((len = iStream.read())!= -1) {
oStream.write(len);
}
iStream.close();
oStream.close();
//高效流
//每次读取一个数组
InputStream iStream = new FileInputStream("lib/1.png");
OutputStream oStream = new FileOutputStream("lib/b.png");
byte [] b = new byte[2048];
int len;
while((len = iStream.read(b))!= -1) {
oStream.write(b,0,len);
}
iStream.close();
oStream.close();
//高效流
BufferedInputStream bStream = new BufferedInputStream(new FileInputStream("lib/1.png"));
BufferedOutputStream oStream = new BufferedOutputStream(new FileOutputStream("lib/c.png") );
byte [] b = new byte[2048];
int len;
while((len = bStream.read(b))!= -1) {
oStream.write(b,0,len);
}
bStream.close();
oStream.close();
BufferedInputStream bStream = new BufferedInputStream(new FileInputStream("lib/1.png"));
BufferedOutputStream oStream = new BufferedOutputStream(new FileOutputStream("lib/d.png") );
int len;
while((len = bStream.read())!= -1) {
oStream.write(len);
}
bStream.close();
oStream.close();