文件和IO流
File类
-
文件创建
createNewFile():创建文本文件,里面仍然没有内容
public class Test {
public static void main(String[] args) {
File file=new File("D:\\代码练习\\Java从入门到入土\\file.txt");//在对应路径下创建一个File类对象
if(!file.exists()){ //判断文件是否存在,不存在则返回true
try {
file.createNewFile();
}catch (IOException e){
e.printStackTrace(); //要抛出IO异常
}
}
File file2=new File("D:\\代码练习\\Java从入门到入土\\My");
file2.mkdir();//创建文件夹,如果path中多个文件都没有,可以使用mkdirs()创建多个
}
} -
修改文件
file.renameTo(new File(path:"")):可以移动文件位置或者修改文件名
File f4=new File("newdile.txt");
f4.renameTo(new File("newfile.txt")); //文件移动和重命名 -
查看文件
file.getXXX():查看文件名或地址等
String[] list=file2.list(); //将文件夹中所有文件返回到String[],如果里面有文件夹会列出文件夹的名字
File[] f=file2.listFiles(); //将文件以File类型返回,可以进一步操作文件
for(File fs:f){
System.out.println(fs.getAbsolutePath()); //获取绝对路径
System.out.println(fs.getPath()); //获取相对路径
System.out.println(fs.length()); //获取文件长度
System.out.println(fs.isHidden()); //是否隐藏
System.out.println(fs.getName()); //获取文件名
System.out.println(fs.canRead()); //是否可读
Date date=new Date(fs.lastModified());
SimpleDateFormat fd=new SimpleDateFormat("yyyy/MM/dd");
System.out.println(fd.format(date)); //获取文件最后修改的日期
} -
删除文件
file.delete(); //删除文件
System.out.println("删除文件成功");
File file2=new File("D:\\代码练习\\Java从入门到入土\\My");
file2.mkdir();
file2.delete(); //当文件夹不为空时,不能被删除
System.out.println(file2); -
过滤筛选文件
File f5=new File("D:\\代码练习\\Java从入门到入土\\My");
File[] f6=f5.listFiles(new FileFilter() {
IO流
流:是一组有顺序,有起点和终点的字节集合,是对数据传输的总称或抽象
IO流:输入输出流
IO流分类:根据处理数据类型:字节流和字符流
根据数据流向分为: 输入流(读文件)和输出流(写文件)(输入输出针对的是程序)
字节流
-
对象使用字节数组byte[]
字节输出流
OutputStream类:表示输出字节流中所有类的超类,输出流接受输出字节并把这些字节发送给inputStream类的某个接收器。
FileOutputStream类:向文件中输出
使用步骤:
-
确定输出流的目标文件
-
创建字节输出流对象
-
确定输出内容
-
按字节输出内容
-
关闭流
public class File2{
public static void main(String[] args) {
String path;
path="test.txt";
out(path);
}
public static void out(String path){
//1.确定输出对象
File file=new File(path);
//2.创建字节输出流对象
try {
OutputStream out = new FileOutputStream(file);
//3.确定输出内容
String s="夕阳西下,断肠人在天涯";
//4.字节输出流
out.write(s.getBytes());
//5.关闭流
out.close();
System.out.println("输出流成功");
}catch (IOException e){
e.printStackTrace();
}
}
}
字节输入流
inputStream类:表示字节输入流的所有类的超类。
FileInputStream类:从文件中获取输入字节
使用步骤:
-
确定输入文件
-
新建一个输入流对象
-
新建字节数组对象,承接输入的内容
-
读取内容存储在StringBuilder对象里
-
关闭流
public class File3 {
public static void main(String[] args) {
in("test.txt");
}
public static void in(String path){
try{
//1.新建文件对象
File file=new File(path);
//2.创建文件输入流对象
InputStream in= new FileInputStream(file);
//3.创建字节数组
byte[] b=new byte[1024];
int len=-1;//读取长度
StringBuilder str=new StringBuilder();
//4.输入内容
while ((len= in.read(b))!=-1){
//5.将输入内容存在StringBuilder对象中
str.append(new String(b,0,len));
//注意,每次创建新的字符串时,新存入的会覆盖之前的,但是没有存入的部分仍然是之前的值,输出会出现重复问题,所以要添加有效字符串长
}
System.out.println(str);
in.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
-
输入输出字节流,操作时都是一个字节
字符流
当文件内容是中文(字节为2),而字节数组为1时,就会出现乱码现象,所以要使用字符流解决乱码问题
-
对象使用字符数组,char[]
字符输出流
Writer类:写入字符流的超类
FileWrite类:文件字符输出流
public static void out(String path){
File file2=new File(path);
try{
Writer out = new FileWriter(file2);
out.write("西安是个好城市");
System.out.println("字符输出流成功");
out.close();
}catch (IOException e) {
e.printStackTrace();
}
}
字符输入流
Reader类:读取字符流的超类
FileReader类:文件字符输入流
public static void in(String path){
File file1=new File(path);
try {
Reader in = new FileReader(file1);
char[] c=new char[3]; //创建字符数组
int len=-1;
StringBuilder s=new StringBuilder();
while((len=in.read(c))!=-1){
s.append(new String(c,0,len));
}
System.out.println(s);
in.close();
} catch (IOException e){
e.printStackTrace();
}
}
字节字符转换流
转换流:可以将一个字节流转换为字符流,也可以将一个字符流转化为字节流
OutputStreamWriter类:可以将输出的字符流转化为字节流的形式输出
程序-->OutputStreamWriter-->OutputStream-->文件
public static void write(OutputStream out){
Writer w=new OutputStreamWriter(out);
try {
w.write("hahaha");
w.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
InputStreamReader类:可以将输入的字节流转化为字符流输入形式
文件--->InputStream--->InputStreamReader-->程序
public static void in(InputStream in){
Reader n=new InputStreamReader(in, Charset.defaultCharset());
char[] set=new char[1024];
int len=-1;
try {
while((len=n.read(set))!=-1) {
System.out.println(new String(set,0,len));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
缓冲流
解决频繁读写文件造成的效率低,性能差的问题
先把第一次要写入的内容存在缓冲流中,当所有内容写完关闭缓冲流的时候,统一将缓冲流中的全部内容写进文件中。
字节缓冲流:BufferedOutputStream
BufferedInputStream
//字节输出缓冲流
public static void ByteWrite(){
File file1=new File("test.txt");
try {
OutputStream out=new FileOutputStream(file1);
//创建缓冲流
BufferedOutputStream bos=new BufferedOutputStream(out);
//从控制台通过缓冲流写入
int i=0;
while(i<10) {
System.out.println("输入城市");
Scanner in=new Scanner(System.in);
String s = in.next();
bos.write(s.getBytes());
i++;
}
bos.close();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
//字节输入缓冲流
public static void ByteRead(){
File file2=new File("out.txt");
try {
InputStream in=new FileInputStream(file2);
BufferedInputStream bosi=new BufferedInputStream(in);
byte[] b=new byte[1024];
int len=-1;
while((len=bosi.read(b))!=-1) {
System.out.println(new String(b,0,len));
}
bosi.close();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
字符缓冲流:BufferedWriter
BufferedReader
//字符输入缓冲流
public static void CharReader(){
File file1=new File("test.txt");
try {
Reader read=new FileReader(file1);
BufferedReader coi=new BufferedReader(read);
char[] c=new char[1024];
int len=-1;
while((len=coi.read(c))!=-1){
System.out.println(new String(c,0,len));
}
coi.close();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
//字符输出缓冲流
public static void CharWriter(){
File file2=new File("out.txt");
try {
Writer write=new FileWriter(file2);
BufferedWriter cois=new BufferedWriter(write);
cois.write("aabbccddeeffgg");
cois.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
打印流
目的:很方便的进行输出,增强打印
字节打印流:PrintStream
字符打印流:PrintWriter
public static void charPrint(){
File file1=new File("out.txt");
try {
Writer write=new FileWriter(file1);
BufferedWriter cos=new BufferedWriter(write);
PrintWriter ps=new PrintWriter(cos);
ps.println("天之大,地之大");
ps.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
文件加密
加密:
-
加密后文件大小不会发生改变
-
加密后的文件打不开
while((len=bib.read(b))!=-1){
//修改字节数据,加密
for(int i=0;i<b.length;i++){
b[i]^=5;
}
//使用增强for循环不行,因为修改的是新定义的变量,不是本身的数据
bob.write(b,0,len);
}

浙公网安备 33010602011771号