文件读写常用类与方法
file
字节流与字符流的区别
1.字节流操作的基本单元为字节;字符流操作的基本单元为Unicode码元。
2.字节流默认不使用缓冲区;字符流使用缓冲区。
3.字节流通常用于处理二进制数据,实际上它可以处理任意类型的数据,但它不支持直接写入或读取Unicode码元;字符流通常处理文本数据,它支持写入及读取Unicode码元。
文件相关方法
//判断文件是否存在
myFile.exists()
//读取文件名称
myFile.getName()
//读取文件路径(相对路径)
myFile.getPath()
//读取文件绝对路径
myFile.getAbsolutePath()
//读取文件的父级路径
new File(myFile.getAbsolutePath()).getParent()
//读取文件的大小
myFile.length()
//判断文件是否被隐藏
myFile.isHidden()
//判断文件是否可读
myFile.canRead()
//判断文件是否可写
myFile.canWrite()
//判断文件是否为文件夹
myFile.isDirectory()
创建/删除文件
String content = "Hello World";
// 第一种方法:根据文件路径和文件名
String path = "F:\\test";
String filename = "test.txt";
File myFile = new File(path,filename);
// 第二种方法
String file = "F:\\test\\test.txt";
File myFile = new File(file);
if (!myFile.exists()) {
// 创建文件(前提是目录已存在,若不在,需新建目录即文件夹)
myFile.createNewFile();
// 删除文件
myFile.delete();
}
文件写入
// 第一种:字节流FileOutputStream
FileOutputStream fop = new FileOutputStream(myFile);
byte[] contentInBytes = content.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
// 第二种:FileWriter(参数true为追加内容,若无则是覆盖内容)
FileWriter fw = new FileWriter(myFile,true);
fw.write(content);
fw.close();
// 第三种:BufferedWriter
BufferedWriter bw = new BufferedWriter(new FileWriter(myFile,true));
bw.write(content);
bw.flush();
bw.close();
// 第四种:打印流PrintStream和PrintWriter
// 字节打印流:PrintStream
// 字符打印流:PrintWriter
PrintWriter pw = new PrintWriter(new FileWriter(myFile,true));
pw.println(content); // 换行
pw.print(content); // 不换行
pw.close();
// 常用BufferedWriter和PrintWriter
读取文件
//读取内容
FileInputStream fis = new FileInputStream(file);
byte[] name = new byte[3];
fis.read(name);
System.out.println(new String(name));
fis.close();
FileReader fr = new FileReader(file);
char[] book = new char[4];
int count = fr.read(book);
fr.close();
System.out.println(count+ " "+new String(book));
// 第一种:以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
InputStream in = new FileInputStream(myFile);
// 一次读一个字节
int tempbyte;
while ((tempbyte = in.read()) != -1) {
System.out.write(tempbyte);
}
in.close();
// 一次读多个字节
int byteread = 0;
byte[] tempbytes = new byte[100];
ReadFromFile.showAvailableBytes(in);
while ((byteread = in.read(tempbytes)) != -1) {
System.out.write(tempbytes, 0, byteread);
}
// System.out.write()方法是字符流,System.out.println()方法是字节流
BufferedReader:
// 第三种:以行为单位读取文件,常用于读面向行的格式化文件
BufferedReader reader = new BufferedReader(new FileReader(myFile));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
// 常用BufferedReader
向文件里面存一个对象
//序列化 serializable
//保存的对象必须实现Serializable接口
//如果对象内部还有属性变量是其他类的对象 那个类也必须实现Serializable接口
Person xw =new Person();
xw.name = "wong";
xw.age = 20;
OutputStream os = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(xw);
oos.close();
//从文件里面读取一个对象
InputStream is =new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(is);
Person xw = (Person)ois.readObject();
System.out.println(xw.name+" "+xw.age);
ois.close();
将一个文件 copy到另外一个位置(通常是图片或视频/音频)
String sourcePath = "D:\\\\桌面\\\\pokerplaymind.png";
String desPath = "D:\\\\Android learning\\\\java2\\\\814Day9\\\\src\\\\main\\\\java\\\\swu\\\\w1nfred\\pokerplaymind.png";
InputStream is = new FileInputStream(sourcePath);
BufferedInputStream bis = new BufferedInputStream(is);
OutputStream os = new FileOutputStream(desPath);
BufferedOutputStream bos = new BufferedOutputStream(os);
byte[] in = new byte[1024];
int count = 0;
while((count = bis.read(in))!=-1){
bos.write(in,0,count);
}
bis.close();
bos.close();
}
}
Back soon