File file = new File("D:"+File.separator+"MyJavaProgram"+File.separator+"hello.java");
file.createNewFile();
|
File file = new File("D:"+File.separator+"MyJavaProgram"+File.separator+"hello.java");
file.delete ();
|
File file = new File("D:"+File.separator+"MyJavaProgram"+File.separator+"hello");
file.mkdir();
|
public String[] list()
public File[] listFiles()
public class FileDemo2{
public static void main(String[] args){
File file = new File("D:"+File.separator+"MyJavaProgram"+File.separator);
String[] str = file.list();
for(int i =0; i< str.length;i++){
System.out.println(str[i]);
}
}
}
|
import java.io.*;
public class FileDemo2{
public static void main(String[] args){
File file = new File("D:"+File.separator+"MyJavaProgram"+File.separator);
File[] str = file.listFiles();
for(int i =0; i< str.length;i++){
System.out.println(str[i]);
}
}
}
|
import java.io.*;
public class RandomAccessFileDemo{
public static void main (String args[])throws Exception{
File file = new File("d:"+File.separator+"MyJavaProgram"+File.separator+"test.txt");
//读写模式,如果文件不存在则自动创建
RandomAccessFile ramdomFile = new RandomAccessFile(file,"rw");
//写入2个数据
String name = "girl";
int love = 25257758;
//写字符串用writeBytes方法
ramdomFile.writeBytes(name);
//写整型数,用writeInt方法
ramdomFile.writeInt(love);
//对文件的读写操作完成后一定要记得关闭
ramdomFile.close();
}
}
|
import java.io.File ;
import java.io.RandomAccessFile ;
public class RandomAccessFileDemo{
public static void main(String args[]) throws Exception{
File file = new File("d:"+File.separator+"MyJavaProgram"+File.separator+"test.txt");
//读写模式,如果文件不存在则自动创建
RandomAccessFile randomFile = new RandomAccessFile(file,"rw");
//读取字符串后面的数字,得先跳过前面的字符串(占4个字节)
randomFile.skipBytes(4);
int i = randomFile.readInt();
//输出所读到的值
System.out.println("读到的数字是:"+i);
//再回头读字符串,用seek方法设置指针位置
randomFile.seek(0);
byte[] temp = new byte[4];
for(int j = 0;j< temp.length;j++){
temp[j] = randomFile.readByte();
}
//转化为字符串
String s = new String(temp);
System.out.println("读到的字符串是:"+ s);
//System.out.println("读到的字符串是"+s);
//对文件的读写操作完成后一定要记得关闭
randomFile.close();
}
};
|
原文地址:http://blog.csdn.net/linfeng24/article/details/35635395