能够使用File的对象操作文件,如:删除、获取文件信息、创建文件夹等。
public class FileDemo {
public static void main(String[] args) {
//加载的路径可以存在也可以不存在
File f=new File("..//000002.JPG");
System.out.println(f.getAbsoluteFile());
long size=f.length();
f.delete();
System.out.println(size);
//可以直接读取文件夹
File f1=new File("../111");
System.out.println(f1.length());
//判断是否存在
f1.exists();
f.exists();
//获取文件最后修改时间
long time=f.lastModified();
System.out.println(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(time));
}
}
File f2=new File("../../java");
String[] names=f2.list();
for (String name : names) {
System.out.println(name);
}
File[] files=f2.listFiles();
for (File file : files) {
System.out.println(file.getAbsoluteFile());
}
文件搜索案例
public class RecursionDemo {
public static void main(String[] args) {
searchfile(new File("/Users/fangxin/Documents/java" ),"logback.xml");
}
private static void searchfile(File dir, String name) {
if (dir!=null && dir.isDirectory()){
File[] files=dir.listFiles();
if (files!=null && files.length>0){
for (File file : files) {
if (file.isFile()){
if (file.getName().equals(name)){
System.out.println(file.getAbsoluteFile());
return;
}
}else {
searchfile(file,name);
}
}
}
}else{
System.out.println("当前搜索的不是文件夹");
}
}
}
IO流
字节输入流:以内存为基准,来自磁盘文件/网络中的数据以字节的形式读入到内存中去的流成为字节输入流。
字节输出流:以内存为基准,把内存中的数据以字节写出到磁盘文件或者网络中去的流成为字节输出流。
字符输入流:以内存为基准,来自磁盘文件/网络中的数据以字符的形式读入到内存中去的流称为字符输入流。
字符输出流:以内存为基准,把内存中的数据以字符写出到磁盘文件或者网络介质中去的流成为字符输出流。
IO流的作用:读写文件数据
a.txt:abs
public class InputStreamDemo1 {
public static void main(String[] args) throws IOException {
InputStream inputStream=new FileInputStream("../a.txt");
//每次读取一个字节
int a=inputStream.read();
System.out.println(a);
int b=inputStream.read();
System.out.println(b);
int c=inputStream.read();
System.out.println(c);
int w=inputStream.read();
System.out.println(w);
}
//使用循环
int a;
while ((a=inputStream.read())!=-1){
System.out.println(a);
}
}
//结果
97 98 115 -1
public class IputStreamDemo2 {
public static void main(String[] args) throws IOException {
InputStream inputStream=new FileInputStream("../a.txt");
//用字节数组存储
byte[] bytes=new byte[3];
// int len=inputStream.read(bytes);
// System.out.println(len);
// String rs=new String(bytes);
// System.out.println(rs);
//
// int len1=inputStream.read(bytes);
// System.out.println(len1);
// String rs1=new String(bytes);
// System.out.println(rs1);
//
// int len2=inputStream.read(bytes);
// System.out.println(len2);
// String rs2=new String(bytes);
// System.out.println(rs2);
//改进使用循环
byte[] buffer=new byte[3];
int len;
while ((length=inputStream.read(buffer))!=-1){
System.out.println(new String(buffer,0,len));
}
}
}
public class OutputStreamDemo1 {
public static void main(String[] args) throws IOException {
OutputStream os=new FileOutputStream("../a.txt",true);
//写数据出去
os.write('a');
os.write(99);
//写一个字节数组出去
byte[] bytes={'a',97,99};
os.write(bytes);
byte[] bytes1="我是".getBytes();
os.write(bytes1);
//写一个字节数组的一部分出去 write([],start,len)
os.write(bytes,0,2);
//写入换行符
os.write("rn".getBytes());
//让写出的数据生效需要刷新
os.flush();
//释放资源 包含了刷新
os.close();
}
}
资源释放的方式
public class FileReaderDemo1 {
public static void main(String[] args) throws IOException {
Reader fr=new FileReader("../b.txt");
// int code;
// while ((code=fr.read())!=-1){
// System.out.println((char) code);
//
// }
//
// char[] chars=new char[1024];
// int len;
// while ((len=fr.read(chars))!=1){
// String rs=new String(chars,0, len);
// System.out.println(rs);
//
// }
Writer wr=new FileWriter("../c.txt",true);
wr.write(98);
wr.write("A");
wr.write("徐");
wr.write("ssajajw生死");
char[] chars1="shakes".toCharArray();
wr.write(chars1);
wr.write(chars1,0,3);
wr.close();
}
}



