1.文件写入
import java.io.*; public class MainClass{
public static void main(String[] args){
try{
BufferedWriter out = new BufferedWriter(new FileWriter("testwrite.txt"));
out.write("lalala");
out.close();
System.out.println("创建成功 !");
}catch(IOException e){
e.printStackTrace();
}
}
}
2.读取文件内容:
import java.io.*; public class MainClass{
public static void main(String[] args){
try{
BufferedReader rd = new BufferedReader(new FileReader("testwrite.txt"));
String str;
while((str=rd.readLine())!=null){
System.out.println(str);
}
rd.close();
System.out.println(str);
}catch(IOException e){
e.printStackTrace();
}
}
}
3.删除文件(File类的delete()方法)
import java.io.*; public class MainClass{
public static void main(String[] args){
try{
File file = new File("D:/Workspace/sqldemo/testwrite.txt");
if(file.delete()){
System.out.println(file.getName()+"文件已删除");
}else{
System.out.println("文件删除失败");
}
}catch(Exception e){
e.printStackTrace();
}
}
}
4.将文件内容复制取另一个文件
import java.io.*; public class MainClass{
public static void main(String[] args) throws Exception{
//向srcrile中写入数据
BufferedWriter out1 = new BufferedWriter(new FileWriter("srcfile"));
out1.write("string to be coped\n");
out1.close();
//将数据从srcfile复制到destfile文件中,以1024个字节数据为单位复制
InputStream in = new FileInputStream(new File("srcfile"));
OutputStream out = new FileOutputStream(new File("destfile"));
byte[] buf = new byte[1024];
int len;
while((len=in.read(buf)) >0){
out.write(buf);
}
in.close();
out.close();
//以每行的方式读取destfile文件中数据
BufferedReader in1 = new BufferedReader(new FileReader("destfile"));
String str;
while((str=in1.readLine())!=null){
System.out.println(str);
}
in1.close();
}
}
5.向文件中追加数据
import java.io.*; public class MainClass{
public static void main(String[] args) throws Exception{
BufferedWriter out = new BufferedWriter(new FileWriter("filename"));
out.write("astring 1\n");
out.close();
//true 追加 FileWriter构造方法
out = new BufferedWriter(new FileWriter("filename",true));
out.write("astring 2");
out.close(); BufferedReader in = new BufferedReader(new FileReader("filename"));
String str;
while((str = in.readLine())!=null){
System.out.println(str);
}
in.close();
}
}
6.创建临时文件
import java.io.*; public class MainClass{
public static void main(String[] args) throws Exception{
File tmp = File.createTempFile("testtemp",".txt");
System.out.println("文件路径"+tmp.getAbsolutePath());
//终止后删除临时文件
tmp.deleteOnExit();
BufferedWriter out = new BufferedWriter(new FileWriter(tmp));
out.write("astring");
System.out.println("临时文件已创建:");
out.close();
}
}
7.修改文件最后的修改日期
import java.io.*;
import java.util.Date; public class MainClass{
public static void main(String[] args) throws Exception{
File f = new File("c:/myjava.txt");
f.createNewFile();
//获取文件最后修改日期并打印出来
Date ftime = new Date(f.lastModified());
System.out.println(ftime.toString());
//更改文件最后修改日期并打印出来
System.out.println(f.setLastModified(System.currentTimeMillis()));
ftime = new Date(f.lastModified());
System.out.println(ftime.toString());
}
}
8.获取文件大小 ,使用File类的file.exists()和file.length()方法来获取文件大小
public class MainClass{
public static void main(String[] args) throws Exception{
long size = getSize("c:/install.log");
System.out.println("文件大小 为:"+size);
}
public static long getSize(String filename){
File file = new File(filename);
if(!file.exists()||!file.isFile()){
System.out.println("文件不存在");
return -1;
}
return file.length();
}
}
9.文件重命名
public class MainClass{
public static void main(String[] args) throws Exception{
File oldf = new File("c:/program.txt");
oldf.createNewFile();
File newf = new File("c:/java.txt");
if(oldf.renameTo(newf)){
System.out.println("已重命名");
}else{
System.out.println("error");
}
}
}
10.设置文件只读:f.setReadOnly()
public class MainClass{
public static void main(String[] args) throws Exception{
File f = new File("c:/java.txt");
System.out.println(f.setReadOnly());
System.out.println(f.canWrite());
}
}
11.在指定目录中创建文件
public class MainClass{
public static void main(String[] args) throws Exception{
File dir = new File("c:/");
File f = File.createTempFile("test", ".txt", dir);
System.out.println(f.getPath());
}
}
12.文件路径比较
public class MainClass{
public static void main(String[] args) throws Exception{
File dir1 = new File("c:/temp/test.txt");
File dir2 = new File("C:/java/test.txt");
if(dir1.compareTo(dir2)==0){
System.out.println("路径一致");
}else{
System.out.println("不一致");
}
}
}