java 文件IO流
定义: 文件io 在java中使用File 类来管理。
代码:File file = new File("文件全路径名"); 也可以写相对路径,则指定在安装jvm
的目录下。
java的文件操作的相关方法
file.createNewFile(); 创建文件,但是会抛出异常需要处理。
file.mkdir();创建目录
file.mkdirs();创建多级目录
file.delete();删除文件
file.renameTo(new File("E:/iotest/aaa.txt")); 文件必须存在 将一个
文件剪切,粘贴到指定的目录 ,可以完成修改文件名字。
file.getName(); 获取文件name
file.getParent(); 获取文件父路径
file.getPath(); 相对路径
file.getParentFile(); 父文件路径
file.getAbsolutePath(); 绝对路径
new Time(file.lastModified()); 最后一次修改的时间 使用new Time 转为为
时间类型
file.length(); 字节数
file.isFile(); 判断是否为文件
file.isDirectory();判断是否为目录
file.isHidden();判断是否文隐藏文件
查询目录下的所有文件,以及限定文件名或文件类型的查询:
使用的是递归调用:
public static void main(String[] args) {
File path = new File("E:/");//指定目录
listJavaByFileFilter(path);
}
private static void listJavaByFileFilter(File path) {
if (path != null && path.isDirectory()) {
File[] files = path.listFiles();
if(files != null && files.length > 0) {
for (File f : files) {
if (f.isDirectory()) {
listJavaByFileFilter(f);
} else if (f.isFile()) {
System.out.println(f.getName());
}
}
}
}
}
相关限定操作:
-------------------------------------------------------------------
List(FilenameFiter fiter)添加一个过滤规则,把符合规则的文件过滤出来
代码如下:需要实现FilenameFilter接口 重写下面的方法accept
class MyFileNameFiter implements FilenameFilter {
@Override
public boolean accept(File dir, String name) {//定义过滤规则
System.out.println(dir+name);
//普通文件
if(name.endsWith(".docx")){
return true;
}
return false;
}
}
----------------------------------------------------------------------
ListFiles(FilenameFiter fiter)添加一个过滤规则,把符合规则的文件过滤出来
class MyFiter implements FileFilter{
@Override
public boolean accept(File pathname) {//绝对地址加文件名
// System.out.println(pathname);
if(pathname.getName().endsWith(".docx")){
return true;
}
return false;
}
}
-------------------------------------------------------------------
ListFiles(Fiter fiter)
FilenameFiter和Fiter的区别:两个各有优点,第二个方便判断,name是以什么结尾。
第一个方便判断 是否为文件或目录
appect()方法的参数不一样:路径加文件名 全部的路径
java 分为字节流和字符流 里面又分别有输出流和输入流
输出和输入都是相对于java程序来说,所以对应的输入流就是读操作,输出流
就是写操作。
流的概念:有顺序有起点的和终点的字节的集合。是对数据传输的抽象。
区别:在不用知道文件内容的时候使用字节流,在需要知道文件内容的时候使用字符流。
字节流是以二进制字节为单位进行传输。
具体操作:输入流
public static void main(String[] args) {
File file = new File("E:/iotest/aa.txt");//文件操作
FileInputStream fileInputStream = null;
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}else {
try {
fileInputStream = new FileInputStream(file);
//--------------------第一种方法------------------------
// byte[] b = new byte[1024];
// int read = fileInputStream.read(b, 2, 4);//指定了起始位置 从2开始填入数据 并且指定了长度
//返回值是真正能读出的个数 len 是希望能读出的内容
//--------------------第二种方法-----------------------
byte[] b = new byte[2];
// int read = fileInputStream.read(b); //将数据拿出来放入数组
返回值 > 0 能够读出的字节的个数 -1 是结束标记
// System.out.println(Arrays.toString(b));
while (fileInputStream.read(b)!=-1){
System.out.print(Arrays.toString(b));
// System.out.print(new String(b,"gbk"));//将byte 转为 字符 ���� 两个类型不匹配 ANSI 就是gbk
}
// ----------------------第一种方法------------------
// int read =0;
// while ((read = fileInputStream.read())!=-1){
// System.out.print((char) read);//将非汉字的字节 转为字符
//
// }
// read = fileInputStream.read();//从文件里面读取一个字节的文件 返回值 就是这个字节的大小 byte--int 无符号 -128--127
//返回值为-1 则读到末尾
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {//关闭资源
if(fileInputStream!=null){
try {
fileInputStream.close();
fileInputStream = null; //防止 多线程情况下引发的异常
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
输出流:
File file = new File("E:/iotest/aa.txt");
FileOutputStream fileOutputStream = null;
try {
fileOutputStream= new FileOutputStream(file,true);
//true 保存文件原内容 默认 和 false 会覆盖
// fileOutputStream.write(448456154);//整形数字 是有范围的
如果是已存在的文件 会覆盖原内容 如果是不存在的文件 则会创建文件
// 如果是个负数 则不写入数据 如果大于范围 则是个看不懂的字符
String str= "二掌柜的酒";
byte[] bytes = str.getBytes("gbk");//改变乱码的形式
fileOutputStream.write(bytes);//如果是从java程序向
文件写入 会自动改变 编码格式
// fileOutputStream.write(bytes,2,4);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
由于字符流的操作与字节流基本类似,就留个读者自己开发,现在主要谈一下使用的时候回出现的问题。
有与io是很耗时的,所以出现了BufferedInputStream和BufferedOutputStream缓冲流,
缓冲流是提供了一块默认为8M的内存空间,在进行读写操作的时候,先将文件信息存入
缓存中,然后从缓存中直接拿去数据,效率更好,需要注意的是在写入操作时候,如果
写入的内容不足8M是不会自动存入文件的需要使用.close() 和 .flush()方法去手动存入,
区别:
close()是直接将流关闭了,若还需要存则需要在创建对象,而flush()支持后续的操作。
类型转换问题:
java代码默认的编码的格式为 utf-8,倘若读取的类型与utf-8不同则需要改变编码的格式;
字节流:
读:new String(byte[],"指定编码");
写:string对象.getbytes("指定编码");
转换流:
读:InputStreamReader(FileInputStream,“指定编码”);
写:OutputStreamWriter(FileOutputStream,“指定编码”);