1.主要就是一些File基本的api操作,比较简单:
package file;
import java.io.File;
import java.io.IOException;
public class fileTest {
public static void main(String[] args) {
File file = new File("E:\\javaio");
System.out.println(file.exists());
System.out.println(file.isDirectory());
System.out.println(file.isFile());
if(!file.exists()){
file.mkdir();
}else{
file.delete();
}
File file2 = new File("e:\\javaio\\test.txt");
if(!file2.exists()){
try{
file2.createNewFile();
}catch(Exception e){
e.printStackTrace();
}
}else{
file2.delete();
}
//常用File对象API
System.out.println(file);
System.out.println(file.getAbsolutePath());
System.out.println(file.getName());
System.out.println(file.getParent());
System.out.println(file.getParentFile().getAbsolutePath());
}
}
2。file过滤,遍历等常用操作
package file;
import java.io.File;
import java.io.IOException;
public class FileUtils {
public static void listDirectory(File dir)throws IOException{
if(!dir.exists()){
try {
throw new IllegalAccessException("目录:"+dir+"不存在");
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//String[] fileNames = dir.list();
//没有遍历子目录
//for(String fileName:fileNames){
// System.out.println(fileName);
//}
//遍历子目录,递归操作
File[] files = dir.listFiles();
if(files != null && files.length > 0){
for(File file:files){
if(file.isDirectory())
{
listDirectory(file);
}else{
System.out.println(file);
}
}
}
}
}
3.RandomAccessFile
java提供的对文件内容的访问,既可以读文件,也可以写文件。RandomAccessFile可以随机访问文件,可以访问文件的任意位置。
java文件模型:byte的数据集合。
打开文件有两种模式:“rw”,“r”.注意文件读写完成后一定要关闭。
可以看看writeInt的源码:
/**
* Writes an {@code int} to the file as four bytes, high byte first.
* The write starts at the current position of the file pointer.
*
* @param v an {@code int} to be written.
* @exception IOException if an I/O error occurs.
*/
public final void writeInt(int v) throws IOException {
write((v >>> 24) & 0xFF);
write((v >>> 16) & 0xFF);
write((v >>> 8) & 0xFF);
write((v >>> 0) & 0xFF);
//written += 4;
}
一些简单的操作代码:
package file;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.RandomAccess;
public class RafDemo {
public static void main(String[] args) throws IOException {
File demo = new File("demo");
if(!demo.exists()){
demo.mkdir();
}
File file = new File(demo,"raf.dat");
if(!file.exists()){
file.createNewFile();
}
RandomAccessFile raf = new RandomAccessFile(file, "rw");
//指针的位置
System.out.println(raf.getFilePointer());
raf.write('A');
System.out.println(raf.getFilePointer());
raf.write('B');
int i = 0x7ffffff;
//用write每次只能写一个byte,写i的话要写4次
raf.write(i>>>24);
raf.write(i>>>16);
raf.write(i>>>8);
raf.write(i);
//或者可以直接用api writeInt
raf.writeInt(i);
//中文
String s = "中国";
byte[] bytes = s.getBytes("gbk");
//可以直接写字节数组
raf.write(bytes);
System.out.println(raf.length());
//指针回到头部
raf.seek(0);
//一次性读取
byte[] buffer = new byte[(int)raf.length()];
raf.read(buffer);
//int输出
System.out.println(Arrays.toString(buffer));
//16进制输出
for(byte b:buffer){
System.out.print(Integer.toHexString(b&0xff) + " ");
}
raf.close();
}
}