文件操作的相关方法:
String getName() 获取文件的名字
boolean canRead() 判断文件是否可读
boolean canWrite() 判断文件是否被写入
boolean exits() 判断文件是否存在
long length() 获取文件的长度(字节)
String getAbsolutePath() 文件的绝对路径
String getParent() 获取文件的父目录
boolean isFile() 判断文件是否是正常文件,而不是目录。
boolean isDirectory() 判断文件是否是一个目录
boolean isHidden() 判断文件是否隐藏文件
long lastModified() 获取文件最后修改时间
boolean mkdir() 创建一个目录
String[] list() 返回目录下全部文件 (拿出文件的名字,只限一层)
File[] listFiles() 返回目录下全部文件 (若对文件还有进一步操作则用它)
boolean createNewFile() 创建文件
boolean delete 删除文件
JAVABEANS是符合某种规范的类:
1、实现接口 Serializable接口
2、私有成员变量
3、提供set/get方法
4、公共的无参构造方法
使用序列化的场景:
1.当两个进程在进行远程通信时,彼此可以发送各种类型的数据。 无论是何种类型的数据,都会以二进制序列的形式在网络上传送。
2. 把内存中的对象进行输出流操作保存在永久性存储介质中,即在其它介质中存储对象的副本。
代码总结如下:
import java.io.*;
public class Test {
static String s = null;
public static void main(String[] args) throws Exception {
// input();
// output();
// bufferedInputStream();
bufferedOutputStream();
// reader();
// writer();
// bufferReader();
// bufferedWriter();
// saveAll();
// ObjectOutputStream();
// ObjectOutInStream();
}
//将一个文件夹里面的所有 文件夹里的文件 和 文件 放到另一个文件夹里面。
public static void saveAll() throws Exception{
File file = new File("F:\\");
//是文件夹才进行下一步的操作
if(file.isDirectory())
getFile(file);
}
//深度搜索
public static void getFile(File file) throws Exception{
File[] files = file.listFiles();
if(files==null){
return;
}
int len = files.length;
for(int i=0;i<len;i++){
if(files[i].isFile()){
//获取绝对路径
File f1 = new File(files[i].getAbsolutePath());
//存储文件的位置
File f2 = new File("F:\\new\\"+files[i].getName());
output(f1, f2);
}else{
getFile(files[i]);
}
}
}
//字节输入流 操作
public static void input() throws Exception{
File file = new File("F:\\A.txt");
InputStream in = new FileInputStream(file);
int a = 0;
byte[] b = new byte[1024];
while((a = in.read(b, 0, 10)) !=-1){
System.out.println(new String(b,0,a));
}
in.close();
}
//字节输出流操作
public static void output(File f1,File f2) throws Exception{
//此处的路径可以写文件,图片,音频,视频等。
// File file = new File("F:\\A.txt");
// //如果没有这个文件,则会新建一个文件,来作为要拷贝的文件。
// File file2 = new File("F:\\B.txt");
InputStream in = new FileInputStream(f1);
OutputStream out = new FileOutputStream(f2);
int a = 0;
byte[] b = new byte[1024];
//每次读固定的长度,0-10
while((a = in.read(b, 0, 10)) != -1){
out.write(b, 0, a);//每次写固定长度,0-a的长度
}
in.close();
// out.flush();
out.close();//输出流关闭时自动会刷新。
}
//带缓冲的输入流
public static void bufferedInputStream() throws Exception{
String path = "F:\\A.txt";
BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(path)));
int a = 0;
byte[] b = new byte[1024];
while((a = in.read(b,0,10)) != -1){
System.out.print(new String(b,0,a));
}
in.close();
}
//带缓冲的字节输出流
public static void bufferedOutputStream() throws Exception{
String path = "F:\\A.txt";
BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(path)));
String path2 = "F:\\B.txt";
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(path2)));
int a = 0;
byte[] b = new byte[1024];
while((a = in.read(b, 0, 10)) != -1){
out.write(b, 0, a);
}
in.close();
out.close();
}
//字符操作 字符输入流
public static void reader() throws Exception{
Reader reader = new FileReader(new File("F:\\A.txt"));
int a = 0;
char[] c = new char[1024];
while((a=reader.read(c, 0, 10)) != -1){
System.out.print(new String(c,0,a));
}
reader.close();
}
//字符输出流
public static void writer() throws Exception{
Reader reader = new FileReader("F:\\A.txt");
Writer writer = new FileWriter("F:\\C.txt");
int a = 0;
char[] c = new char[1024];
while((a = reader.read(c, 0, 10)) != -1){
writer.write(c, 0, a);
}
reader.close();
writer.close();
}
//带缓冲字符输入流
public static void bufferReader() throws Exception{
Reader reader = new FileReader("F:\\A.txt");
BufferedReader bf = new BufferedReader(reader);
String str ;
while((str = bf.readLine()) != null){//一行一行的读
System.out.println(str);
}
bf.close();
reader.close();
}
//带缓冲字符输出流
public static void bufferedWriter() throws Exception{
BufferedReader br = new BufferedReader(new FileReader(new File("F:\\A.txt")));
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("F:\\D.txt")));
String s;
while((s = br.readLine()) != null){
bw.write(s);
bw.newLine();
}
br.close();
bw.close();
}
//对象序列化
public static void ObjectOutputStream() throws Exception{
// //方法一:
// File file = new File("F:\\P.txt");
// FileOutputStream out = new FileOutputStream(file);
// ObjectOutputStream oos = new ObjectOutputStream(out);
// oos.writeObject(new Person());
// oos.close();
//方法二
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("F:\\P.txt"));
oos.writeObject(new Person("张三疯",18));
oos.close();
}
//反序列化
public static void ObjectOutInStream() throws Exception{
ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File("F://P.txt")));
Person person = (Person) in.readObject();
person.method();
in.close();
}
}
//JavaBean
class Person implements Serializable{
private String name;
private int age;
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 Person(){}
public Person(String name,int age){
this.name = name;
this.age = age;
}
public void method(){
System.out.println(this.name + "一天疯三次,所以他叫"+this.name);
System.out.println("今年他"+this.age+"了!!!");
}
}