- IO流(input output)
- file类
import java.io.File;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
File f=new File("G:\\javastudy\\test\\tt.txt");//这个时候对象就是tt.txt文件
// File f1=new File("G:\\javastudy","test\\tt.txt");//一样的
// /文件中是路径的分隔符在java中在转移符,java中\\或者/才是分隔符
File f2=new File("G:"+File.separator+"javastudy/test/tt.txt");//File.separator也可以作为文件的分隔符
System.out.println(f.getName());
File f3=new File ( "G:\\javastudy\\idea\\work1\\src\\hellojava\\day12\\Test.java");
System.out.println(f.getPath());//new file的路径
System.out.println(f.getAbsolutePath());//获取绝对路径
System.out.println(f3);
System.out.println(f3.getAbsoluteFile());
System.out.println(f3.getParent());//返回父级
f.renameTo(new File("G:\\javastudy\\test\\tt1.txt"));//给文件重命名
File f4=new File ( "G:\\javastudy\\test1");
System.out.println(f4.exists());//判断文件或者文件夹是否存在
File f5=new File ( "G:\\javastudy\\test\\tt1.txt");
System.out.println(f5.canRead());//是否可读
System.out.println(f5.canWrite());//是否可写
System.out.println(f5.isFile());//判断是否是文件
System.out.println(f5.isDirectory());//是不是文件夹或者目录
System.out.println(f5.lastModified());//最后修改时间返回的是一个毫秒数
System.out.println(f5.length());//返回文件的长度
File f6=new File ( "G:\\javastudy\\test\\tt2.txt");
System.out.println(f6.exists());
if (!f6.exists()){
try{
f6.createNewFile();//创建文件
}catch (IOException e){
e.printStackTrace();
}
}
f6.delete();//z删除文件
File f7=new File ( "G:\\javastudy\\test\\cc");
f7.mkdir();//创建单层目录,如果要是使用这样的方法创建目录就得一层一层执行
File f8=new File ( "G:\\javastudy\\test\\b\\d\\c");
f8.mkdirs();//创建多层目录
File f9=new File ( "G:\\javastudy\\test");
String[] fl=f9.list();
for (String s :fl){//返回的是当前文件夹的子集,包括目录和文件
System.out.println(s);
}
File[] fs=f9.listFiles();
for (File ff:fs){//返回的是当前文件夹的子集的file对象,包括目录和文件
System.out.println(ff);
}
System.out.println();
}
}
例题
//遍历g盘test文件夹下所有文件夹无论多深
//使用递归的方式实现
File g=new File("G:\\javastudy\\test");
new Test().test(g);
}
public void test(File file){//递归遍历文件
if (file.isFile()){
System.out.println(file.getAbsolutePath()+"是文件夹");
}else{
System.out.println(file.getAbsolutePath()+"是文件夹");//如果是文件夹这个文件夹里就可能有子文件或者文件
File[] fq =file.listFiles();//获取当前文件夹下所有的子文件或者文件的file对象
if (fq!=null&&fq.length>0){
for(File ff :fq){
test(ff);//递归
// if (ff.isFile()){
// System.out.println(file.getAbsolutePath()+"是文件");
// }else{System.out.println(file.getAbsolutePath()+"是文件夹");
// File[] fq1 =file.listFiles();
//
// }
}
}
}
}
}
代码测试及输出:
tt.txt
G:\javastudy\test\tt.txt
G:\javastudy\test\tt.txt
G:\javastudy\idea\work1\src\hellojava\day12\Test.java
G:\javastudy\idea\work1\src\hellojava\day12\Test.java
G:\javastudy\idea\work1\src\hellojava\day12
false
true
true
true
false
1631909288614
4
false
b
cc
tt1.txt
G:\javastudy\test\b
G:\javastudy\test\cc
G:\javastudy\test\tt1.txt
G:\javastudy\test是文件夹
G:\javastudy\test\b是文件夹
G:\javastudy\test\b\d是文件夹
G:\javastudy\test\b\d\c是文件夹
G:\javastudy\test\cc是文件夹
G:\javastudy\test\tt1.txt是文件夹
Process finished with exit code 0
- JAVA io 原理;(input读取外部数据到程序内存中 output将程序内存数据输出到磁盘光盘等存储设备中)
- 文件字节流(输入输出流)
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Test1 {//文件字节输入流
public static void main(String[] args) {
Test1.testFileInputStream();
Test1.testFileOutputStream();
try{FileInputStream in=new FileInputStream("G:\\javastudy\\test\\tt1.txt");
byte[] b=new byte[10];//设置一个byte数组接收读取的文件的内容
int len=0;//设置一个读数据的长度
in.read(b);//int.red方法有一个返回值,返回值是读取的数据的长度,如果读取到一个数据,还会向后读一个
//也就意味着这个in.read返回值是-1的时候整个文件读取就读取完毕了
while ((len=in.read(b))!=-1){
System.out.println(new String(b,0,len));
//new String(b,0,len)参数1是缓冲数据数组,参数二是那个位置开始转换字符串,参数三是总共转换几个字符
}
System.out.println(new String(b) );
in.close();//在使用完成之后一定要关闭
}catch (Exception e){
e.printStackTrace();
}
}
public static void testFileInputStream(){}
//文件字节输出流
public static void testFileOutputStream(){
try{
FileOutputStream out=new FileOutputStream("G:\\javastudy\\test\\tt4.txt");//制定向tt4输出数据
String str="sdasdadsad";
out.write(str.getBytes());//把数据写到内存
out.flush();//把内存中的数据刷写到硬盘
out.close();//关闭
}catch (Exception e){
e.printStackTrace();
}
}
}
练习
字节流复制文件(文件可以为图片其他任何类型文件直接使用的二进制)
public static void copyFile(String inPath,String outPath){
try {
FileInputStream in=new FileInputStream(inPath);//读取的源文件
FileOutputStream out=new FileOutputStream(outPath);//复制到哪儿
byte[] b=new byte[100];
int len=0;
while ((len = in.read(b))!=-1){
out.write(b,0,len);//1参数写的是缓冲数组,2参数写的是从数组哪个位置开始,参数3是写的总长度
}
out.flush();//把写到内存的数据刷到硬盘
out.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Test1.copyFile("G:\\javastudy\\test\\tt1.txt","G:\\javastudy\\test\\cc\\tt1.txt");
文件的读写(同名目录会被覆盖原来的数据会被替换为新的数据,读取需要保证文件存在不然会报异常)
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Test2 {
public static void main(String[] args) {
// Test2.testFileReader("G:\\javastudy\\test\\tt1.txt");
// Test2.testFileWriter("hello world","G:\\javastudy\\test\\tt5.txt");
Test2.copyFile("G:\\javastudy\\test\\tt5.txt","G:\\javastudy\\test\\cc\\tt5.txt");
}
public static void testFileReader(String inPath){
try {
FileReader fr=new FileReader( inPath);//创建文件字符输入流的对象
char[]c=new char[10];//创建临时存数据的字符串数组
int len=0;//定义一个输入流的读取长度
while ((len=fr.read(c))!=-1){
System.out.println(new String(c,0,len));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void testFileWriter(String text,String outPath){//输出流
try {
FileWriter fw=new FileWriter(outPath);
fw.write(text);//写到内存中
fw.flush();//把内存中的数据刷到硬盘
fw.close();//关闭读
} catch (Exception e) {
e.printStackTrace();
}
}
public static void copyFile(String inPath,String outPath){
try {
FileReader fr=new FileReader(inPath);
FileWriter fw=new FileWriter(outPath);
char[] c=new char[100];
int len=0;
while ((len=fr.read(c))!=-1){//读取数据
fw.write(c,0,len);//写入数据
}
fw.flush();
fw.close();
fr.close();
} catch (Exception e) {
e.printStackTrace();
}
}