目录
1.操作文件
(1)创建文件File
(2)按照字节处理
(3)按照字符处理
1.节点流操作文件
(1)创建文件File
new File(String pathname);//方法一,使用字符串写入路径和文件格式创建
new File(File parent,String pathname);
//方式二,先使用File创建父类路径,再使用字符串传入文件名字和格式
new File(String parent,String child);
//方式三,使用字符串创建父类路径和子类路径,传入即可
new File(String pathname);//方法一,使用字符串写入路径和文件格式创建 new File(File parent,String pathname); //方式二,先使用File创建父类路径,再使用字符串传入文件名字和格式 new File(String parent,String child); //方式三,使用字符串创建父类路径和子类路径,传入即可
常用获取文件信息的方法
//调用相应的方法,得到对应信息
System.out.println("文件名字=" + file.getName());
//getName、getAbsolutePath、getParent、length、exists、isFile、isDirectory
System.out.println("文件绝对路径=" + file.getAbsolutePath());
System.out.println("文件父级目录=" + file.getParent());
System.out.println("文件大小(字节)=" + file.length());
System.out.println("文件是否存在=" + file.exists());//T
System.out.println("是不是一个文件=" + file.isFile());//T
System.out.println("是不是一个目录=" + file.isDirectory());//F
//创建目录
file.mkdir();//一级目录
file.mkdirs();//多级目录
(2)按照字节处理
输入流文件:FileInputStream
输出流文件:FileOutputStream
两个类对文件按字节进行处理,具体实例看(3)中
(3)按照字符处理
输入流:FileReader
输出流:FileWriter
按字符进行文件处理,具体代码如下,使用方法与按字节处理的相同
FileReader
import com.sun.org.apache.xpath.internal.operations.String;
import org.junit.jupiter.api.Test;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class reader01 {
public static void main(String[] args) {
}
@Test
public void read1(){
java.lang.String str = "e:\file01.text";
FileReader fileReader = null;
try {
fileReader = new FileReader(str);
int get = 0;
while (!((get = fileReader.read()) == -1)){//fileReader.read()会返回每个字符的源码,当读取完所有字符,会返回-1
System.out.print((char)get + " ");//输出文档中的字符
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fileReader != null){//判断是否进行了操作
try {
fileReader.close();//关闭流
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void read2(){
java.lang.String str = "e:\file01.text";
FileReader fileReader1 = null;
char[] asl = new char[8];
int get = 0;
try {
fileReader1 = new FileReader(str);
while ((get = fileReader1.read(asl)) != -1){//fileReader1.read(asl)会返回读取的字符数组asl的长度,并且把字符传给asl
System.out.print(new java.lang.String(asl,0,get));//把字符数组中的字符放入字符串中,字符串长度取决于get的值
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fileReader1 != null){
try {
fileReader1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
FileWriter
import java.io.FileWriter;
import java.io.IOException;
public class writer01 {
public static void main(String[] args) {
String str = "e:\file01.text";//文件地址
int od = 0;
String word = "xxloss";
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(str);//创建一个文件对象,文件地址为str变量内容,如果文件已存在,则会覆盖存在的文件
fileWriter = new FileWriter(str,true);//使用增加法,即在创建的文件内增加输入而非覆盖
//写入的五种方法
fileWriter.write(15);//第一种,传入整数int类型,系统自动转成ASCII码值
fileWriter.write(word);//第二种,传入字符串
fileWriter.write(word,0,3);//第三种,传入字符串指定长度
fileWriter.write(word.toCharArray());//第四种,传入字符数组,toCharArray方法把字符串转为字符数组
fileWriter.write(word.toCharArray(),0,3);//第五种,传入字符数组,指定长度
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fileWriter != null) {
try {
fileWriter.close();//关闭流,必须要进行的操作,如果不关闭,数据传输不到文件中
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2.处理流操作文件
处理流是节点流的包装类,相较于节点流,功能更完善,使用起来更加便利。节点流属于底层文件操作代码,较为繁琐。
(1)字符处理流分为BufferedReader和BufferedWriter两种,这两种类只适合处理字符文本类型的文件,而图片视频音频等都是二进制编码类型,不宜使用。具体操作如下
import java.io.*;
public class BufferedWR_ {
public static void main(String[] args) {//使用方法与File相同
String stPath = "e:\new.java";
String writeWords = "How can i show you this beautiful world!";
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(stPath));
//不同的地方是要在此传入的是new FileWriter(stPath),如果想添加字符,则使用new FileWriter(stPath,ture)
bw.write(writeWords);
bw.newLine();//换行操作
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String stLine = "";
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader(stPath));
while ((stLine = bufferedReader.readLine()) != null){
//bufferedReader.readLine()会按行返回字符串内容,使用一个字符串接受,当读取完字符会返回Null
System.out.println(stLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
输出结果为
具体使用与File的类似
(2)字节处理流BufferedOutputStream和BufferedInputStream类,属于字节处理,非常适用于图片视频音频等二进制编码类型文件,而使用文本文件时要注意文本的编码格式,比如UTF-8中的汉字是三个字节,如果传入字节数设置不当,会造成乱码问题,须特别注意。
使用一个复制功能的方法来综合使用BufferedOutputStream和BufferedInputStream类
import java.io.*;
public class BufferedOutIn {
public static void main(String[] args) {
String staPath = "e:\wallhaven-o3j755_1920x1080.png";
//创建要读取的路径
String endPath = "e:\bizhi.png";
//创建要写入的路径
BufferedInputStream bis =null;
BufferedOutputStream bos =null;
byte[] by = new byte[1024];
//设置读取的字节数组,每次读取1024个字节,提高效率
int get = 0;
//设置读取的字符长度,起保护作用
try {
bis = new BufferedInputStream(new FileInputStream(staPath));
bos = new BufferedOutputStream(new FileOutputStream(endPath));
while ((get = bis.read(by)) != -1){
//如果读取到-1则表示已经读完
bos.write(by,0,get);
//将读到的数写入路径中
}
System.out.println("Finish");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bis.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
结束后便会把给出路径上的文件复制一份到指定路径。



