File 是文件或者文件夹
//创建文件类
File file = new File("C:\\Users\\Administrator\\Desktop\\aa");
//判断文件是否可执行,可读,可写;返回值为Boolean值;
System.out.println(file.canExecute()+"\t"+
file.canRead()+"\t"+file.canWrite());
//返回此路径名的绝对路径名形式。
System.out.println(file.getAbsoluteFile());
//返回此路径名的绝对路径名字符串。
System.out.println(file.getAbsolutePath());
//返回此路径名的路径名字符串;
System.out.println(file.getPath());
//返回此路径名父目录的路径名字符串;如果此路径名没有指定父目录,则返回 null。
System.out.println(file.getParent());
//返回此路径名父目录的路径名,如果不存在父目录,则返回null;
System.out.println(file.getParentFile());
mkdir()创建指定目录
mkdirs() 创建指定目录,包含必须但不存在的父目录;
isDirectory() 判断是否是一个文件夹
exists()判断文件是否存在;
isFile()判断是否是一个文件;
length()返回该路径名所代表的文件的长度;
list() 返回一个字符串数组,这些字符串是此路径名表示的目录中的文件和目录的名字。
返回的是String的数组;
listFiles();返回的是File数组;是此路径名表示的目录中的文件和文件夹;
public static void main(String[] args) {
File file = new File("C:\\Users\\Administrator\\Desktop");
String[] list1 = file.list();
for(String list:list1) {
System.out.println(list);
}
}
File file = new File("C:\\Users\\Administrator\\Desktop");
File[] list2 = file.listFiles();
for(File list:list2) {
System.out.println(list);
}
当所给路径不是文件夹,创建文件夹,若存在删除文件夹,
package com.openlab.pp;
import java.io.File;
import java.io.IOException;
public class TestFile {
public static void main(String[] args) {
File file = new File("C:\\Users\\Administrator\\Desktop\\aa");
if(!file.exists()) {
//创建多级目录;
file.mkdirs();
}else{
file.delete();//注意删除是不放回回收站的,所以要谨慎使用;
System.out.println("删除成功");
}
}
}
判断所给是否为文件,若不是,判断父目录是否存在,不存在则创建,再创建文件,若是文件 则删除文件;
File file = new File("C:\\Users\\Administrator\\Desktop\\aa\\1.txt");
if(!file.isFile()) {
File parents = new File(file.getParent());
if(!parents.exists()) {
//如果父目录不存在,则创建目录;
file.mkdirs();
System.out.println("目录创建成功");
}
//接着创建文件
if(!file.exists()) {
try {
file.createNewFile();
System.out.println(file.getName()+"创建成功");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}else {
file.delete();
System.out.println("删除成功");
//注意删除是不放回回收站的,所以要谨慎使用;
}
注意:要想删除父目录,如果目录下还有子目录或是子文件,时不能删除的,只有它是空的才能被删除掉;
public class TestFile {
public static void main(String[] args) {
String file = "C:\\Users\\Administrator\\Desktop";
copyFile(file);
}
public static void copyFile(String path) {
File file = new File(path);
File[] files = file.listFiles();//获取到所给路径下的文件和文件夹;
for(File list : files) {
if(list.isDirectory()) {//利用递归将文件夹下的子孙文件输出;
copyFile(list.getAbsolutePath());
}else{
System.out.println(list.getName());
}
}
}
}
I/O是input/output的缩写,I /O技术是用于处理设备之间的数据传输。例如读写文件,网络通讯等等。在Java程序中,对于数据的输入输出都是以流的方式进行操作的。Java.io包下提供了各种流的类与接口,可以获取不同种类的数据,通过标准的方法输入输出数据。
输入input:读取外部数据到内存中;
输出output:将程序(内存)中的数据输出到磁盘或光盘中。
根据不同的站位,流的方向有所不同,我们是站在程序的方向进行判断。
按流的操作单位分 | 按照流的流向分 | 按照流的角色分 |
---|---|---|
字节流 | 输入流 | 节点流 |
字符流 | 输出流 | 处理流 |
基类 | 节点流 | 缓冲流 |
---|---|---|
InputStream | FileInputStream | BufferedInputStream |
OutputStream | FileOutputStream | BufferedInputStream |
Reader | FileReader | BufferedReader |
Writer | FileWriter | BufferedWriter |
package com.openlab.pp;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.junit.jupiter.api.Test;
public class TestRead {
//@Test
public void read01() {
File file = new File("E:\\EclipseWorkSpace\\zong\\src\\1.txt");//创建文件
FileReader reader = null;
try {//创建流
reader = new FileReader(file);
int data;//读取数据
while((data = reader.read()) != -1) {
System.out.print((char) data);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void read02() {
File file = new File("E:\\EclipseWorkSpace\\zong\\src\\1.txt");//创建文件
FileReader reader = null;
try {//创建流
reader = new FileReader(file);
char[] buf = new char[5];//读取数据
int len;
while((len = reader.read(buf)) != -1) {
// //方式一:
// //错误写法
// for(int i = 0;i
// System.out.print(buf[i]);
// }
//正确写法
// for(int i = 0;i
// System.out.print(buf[i]);
// }
//方式二:通过String
//它同方式一的错误一致,都是左后获取的字符不能完全覆盖buf,所以只修改了一部分,后面的按上一步的字符填充
System.out.print(new String(buf));
System.out.print(new String(buf,0,len));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
//@Test
public void writer() {
FileWriter writer = null;
try {//file和流的创建;如果所给文件存在,它会创建一个文件;
writer = new FileWriter("w.txt");
writer.write("我是一个程序员");
} catch (IOException e) {
e.printStackTrace();
}finally {
if(writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
方法一:使用FileReader和FileWriter
//@Test//实现文件的复制;
public void testcopy1() {
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader("E:\\\\EclipseWorkSpace\\\\zong\\\\src\\\\1.txt");
fw = new FileWriter("w.txt");
int len;
char[] buf = new char[5];
while((len = fr.read(buf)) != -1) {
fw.write(buf,0,len);
}
System.out.println("复制成功");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
方法二:使用FileInputStream和FileOutputStream
通过将文件设置为形参,可以实现指定文件的复制,这里有一个小点:使用字节流可以将文本文件复制到另一个文件中,那我们之前说使用字节,读取的有中文会乱码不就冲突了吗,这是因为我们将它在显示台输出的原因,你想它是读到几个输出几个,有的汉字读到一半自然会乱码;但我们进行复制完成后再在文件中查看的,所以没有出现乱码,在这个过程中,流充当的是搬运工的角色,并不会引起乱码。也就是字节流是万能流;
//实现指定文件的复制;
@Test//实现文件的复制;
public void testcopy1() {
String src = "E:\\EclipseWorkSpace\\zong\\src\\1.txt";
String dest = "w1.txt";
testcopy(src,dest);
}
public static void testcopy(String srcfile,String destfile) {
FileInputStream fis = null;
FileOutputStream fos = null;
File fileR = new File(srcfile);
File fileW = new File(destfile);
try {
fis = new FileInputStream(fileR);
fos = new FileOutputStream(fileW);
int len;
//获取到的是字节数组哦
byte[] buf = new byte[5];
while((len = fis.read(buf)) != -1) {
fos.write(buf,0,len);
}
System.out.println("复制成功");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test //实现图片,音频的复制;所以使用的是字节流
public void testcopy2() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("D:\\2345Downloads\\安排.jpg");
fos = new FileOutputStream("pp.jpg");
int len;
//获取到的是字节数组哦
byte[] buf = new byte[5];
while((len = fis.read(buf)) != -1) {
fos.write(buf,0,len);
}
System.out.println("复制成功");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
1.对于文本文件,我们使用字符流进行处理,如(.txt,.java,.c,.cpp)等;
2.对于非文本文件,我们使用字节流,如(.jpg,.mp3,.mp4,.avi,.doc,.ppt)等等;
3.字节流是万能流,如果我们只是读取文件将文件内容写到某个文件中,并不会将其打印到控制台,对于文本文件我们也能用字节流;
4.但是字符流时不能操作非文本文件的,即使你不在控制台输出也不行。
它就是为了提高读写的效率的,在没有缓冲下,它是读到多少输出多少;而有了缓冲以后,是将读到的数据放到缓冲区,等达到一定的容量,进行输出,提高了效率。
@Test
public void test() {
testBuffer("E:\\EclipseWorkSpace\\zong\\src\\1.txt","dd.txt");
}
public static void testBuffer(String srcFile,String destFile) {
File fileR = new File(srcFile);
File fileW = new File(destFile);
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
fis = new FileInputStream(fileR);
fos = new FileOutputStream(fileW);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
int len;
byte[] buf = new byte[5];
while((len = bis.read(buf)) != -1) {
bos.write(buf,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.openlab.pp;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.junit.jupiter.api.Test;
public class TestBuffer {
@Test
public void test() {
long start = System.currentTimeMillis();
testcopy("E:\\EclipseWorkSpace\\zong\\src\\小黑.mp3","w1.mp3");
//testBuffer("E:\\EclipseWorkSpace\\zong\\src\\小黑.mp3","w2.mp3");
long end = System.currentTimeMillis();
System.out.println(end - start);
}
public static void testBuffer(String srcFile,String destFile) {
File fileR = new File(srcFile);
File fileW = new File(destFile);
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
fis = new FileInputStream(fileR);
fos = new FileOutputStream(fileW);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
int len;
byte[] buf = new byte[5];
while((len = bis.read(buf)) != -1) {
bos.write(buf,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//实现指定文件的复制;
public static void testcopy(String srcfile,String destfile) {
FileInputStream fis = null;
FileOutputStream fos = null;
File fileR = new File(srcfile);
File fileW = new File(destfile);
try {
fis = new FileInputStream(fileR);
fos = new FileOutputStream(fileW);
int len;
//获取到的是字节数组哦
byte[] buf = new byte[5];
while((len = fis.read(buf)) != -1) {
fos.write(buf,0,len);
}
System.out.println("复制成功");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
普通流:
缓冲流:
可以看出缓冲流的效率比普通流高很多,并且我们只需要关闭缓冲流即可,它会自动关闭字节输入、输出流;
InputStreamReader:将字节流转换为字符流;OutputStreamWriter:将字符流转换为字节流,并且可以指定编码格式。
//转换流 实现读写
@Test
public void zhuan() {
//创建文件file
File file1 = new File("C:\\Users\\Administrator\\Desktop\\pt.txt");
File file2 = new File("pp.txt");
//创建字节流
FileInputStream fis = null;
FileOutputStream fos = null;
//创建转换流
InputStreamReader ir = null;
OutputStreamWriter ow = null;
try {
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
ir = new InputStreamReader(fis,"gbk");
ow = new OutputStreamWriter(fos,"gbk");
int len;
char[] buf = new char[1024];
while((len = ir.read(buf)) != -1) {
ow.write(buf,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(ow != null) {
try {
ow.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(ir != null) {
try {
ir.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
练习:从控制台输入字符串,当输入e或是exit 时,结束;
@Test//标准输入输出流;
public void test() {
//采用转换流;因为我们的BufferedReader 是字符流;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
while(true) {
try {
String data = br.readLine();
if("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data)) {
System.out.println("程序结束");
break;
}
System.out.println(data);
} catch (IOException e) {
e.printStackTrace();
}
}
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testPrint() {
PrintStream ps = null;
try {
FileOutputStream fos = new FileOutputStream(new File("cop.txt"));
//创建打印输出流,设置为自动刷新模式;
ps = new PrintStream(fos,true);
if(ps != null) {
System.setOut(ps);//将标准输出流换成文件流;
}
for(int i = 0;i<= 255;i++) {
System.out.print((char) i );
if(i % 50 == 0) {
System.out.println();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
if(ps != null) {
ps.close();
}
}
}
为了方便操作Java语言的基本数据类型和String的数据,使用数据流;分别是DataInputStream 和DataOutputStream;
主要作用是读取或者输出基本数据类型的变量或者字符串。
@Test//数据流的写入
public void testWriter() {
try {
DataOutputStream ds = new DataOutputStream(new FileOutputStream("dop.txt"));
ds.writeUTF("小黑");
ds.flush();
ds.writeInt(21);
ds.flush();
ds.writeBoolean(true);
ds.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
我们写入文件的数据可不是让我们打开文件去看的,那样有乱码;我们需要通过DataInputStream数据流进行读取,读取顺序要和我们写入的顺序一致;
@Test//数据流的读取,
public void testRead() {
DataInputStream di;
try {
di = new DataInputStream(new FileInputStream("dop.txt"));
String name = di.readUTF();
int age = di.readInt();
boolean gender = di.readBoolean();
System.out.println(name+"\t"+age+"\t"+gender);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//图片加密与解密操作,
//@Test 加密
public void testSecret() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("D:\\2345Downloads\\安排.jpg");
fos = new FileOutputStream("secret.jpg");
int len;
byte[] buf = new byte[1024];
while((len = fis.read(buf)) != -1) {
for(int i = 0;i<len;i++) {
buf[i] = (byte) (buf[i] ^ 5);
}
fos.write(buf,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//@Test 解密
public void testDelSecret() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("secret.jpg");
fos = new FileOutputStream("plusSecret.jpg");
int len;
byte[] buf = new byte[1024];
while((len = fis.read(buf)) != -1) {
for(int i = 0;i<len;i++) {
buf[i] = (byte) (buf[i] ^ 5);
}
fos.write(buf,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}