文件

文件


一。基本知识

1.文件分类

文本文件、图片、视频、音频、压缩包、exe

1.字符文件

2.二进制文件

 

Java给我们提供了一个包用来进行对文件的操作,包名是:java.io

File是对文件进行创建,删除,修改和查找的操作的类

 

2.文件操作

1)文件创建,删除,修改:file()

2)文件查找

Window   \ ;  

Linux  / :

Mac  

Unix

如何确定window有多少个根目录呢? 根据盘符来决定的(C,D,E……)

Linux又有多少个根目录呢?只有一个根目录 root

3)查找我的工作空间中的所有文件:queryFile()

 

二、文件的增删查改以及读取文件名

1.实现

public class FileDemo {
//主函数调用
public static void main(String[] args) {
     file("src/file","test.txt");
queryFile("D:\\Java学习");
}
//创建文件
public static void file(String path, String fileName) {
File f = new File(path);// 建立一个文件夹
 
if (!f.exists()) {
boolean b = f.mkdirs();// 创建多重文件夹
if (b)
System.out.println("创建文件夹成功!");
else
System.out.println("创建文件夹失败!");
}
 
File file = new File(f, fileName);
 
if (file.exists()) {
System.out.println(file.getName());// 名字
System.out.println(file.getAbsolutePath());// 地址
file.setLastModified(System.currentTimeMillis());//创建时间
 
// boolean b = file.renameTo(new File("src/file/abc.txt"));//重命名
// System.out.println(b);
boolean b = file.delete();// 删除
System.out.println(b);
System.out.println("删除文件成功!");
} else {
try {
file.createNewFile();
System.out.println("文件创建成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 在文件夹中创建文件
public static void queryFile(String path) {
File f = new File(path);
 
File[] listFile = f.listFiles();
 
if (null == listFile)
return;
 
for (int i = 0; i < listFile.length; i++) {
File file = listFile[i];
if (file.isDirectory()) {// 如果file是一个文件夹,递归
queryFile(file.getAbsolutePath());
} else if (file.isFile()) {// 如果是一个文件,结束递归
System.out.println(file.getName());
}
}
}
 
}


2.运行结果

1.)文件的创建

 


 

2.)重命名

 


 

3.)删除

 

 

 

4.读取文件夹中的文件

 

三.文件的读写

1.实现功能

1.)读出文件内容

2)写入文件内容

3)在指定位置查找包含关键字的文件,并输出有几个,然后打开这个文件

2.具体功能实现

public class FileIoDemo {
public static void main(String[] args) {
readFile("src//file//FileIoDemo.java");
// writeFile("src//file//FileIoDemo.java", "src//file//outIoDemo.java");
}
//写入文件
public static void writeFile(String path, String copypath) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
 
fis = new FileInputStream(path);
fos = new FileOutputStream(copypath);
 
int in = 0;
while ((in = fis.read()) != -1) {
fos.write(in);
}
//
// String context = new String(b);
// System.out.print(context);
 
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
 
}
//读取文件
public static void readFile(String path) {
FileInputStream fis = null;
 
try {
 
fis = new FileInputStream(path);
 
int size = fis.available();
byte b[] = new byte[size];
int in, i = 0;
while ((in = fis.read()) != -1) {
b[i++] = (byte) in;
}
 
String context = new String(b);
// System.out.println(context);
ArrayList<Integer> list = new ArrayList<Integer>();
for (int j = 0; j < context.length(); j++) {
if (context.charAt(j) == '\n');
list.add(new Integer(j + 1));
}
Scanner sc = new Scanner(System.in);
System.out.println("输入要查找的");
String str = sc.next();
/************patterrn matcher类************/
Pattern p = Pattern.compile("(" + str + ")");
Matcher m = p.matcher(context);
int num = 0;
while (m.find()) {
System.out.println("找到了!");
num++;
}
System.out.println("关键字" + str + "一共有" + num + "个");
//Runtime.getRuntime().exec("cmd.exe/c start"+path);
java.awt.Desktop.getDesktop().open(new File(path));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
 
}
}


3.运行结果

1)读取文件内容

 

2)写入文件内容至一个新文件


创建的新文件及为outIoDemo.java

里面的内容为写入的内容

 

3)查找含有关键字的文件并输出有几个,然后打开指定文件

 

注意:

关于查找关键字及打开文件:

1.查找关键字需调用的类

/************patterrn matcher************/

Pattern p = Pattern.compile("(" + str + ")");

Matcher m = p.matcher(context);

      while (m.find()) {

System.out.println("找到了!");

num++;}

2.打开指定路径的文件方法。

java.awt.Desktop.getDesktop().open(new File(path));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值