Java学习笔记之IO流

IO : input ouput 流 相对于java(内存)

File: 文件,是JAVA中的对象,用来映射硬盘中的文件或者目录

注意:java中‘\’是转义字符,所以‘\\’才能表示目录

File常用方法:

 1         File file=new File("D:\\File\\a");
 2         System.out.println("此抽象路径名表示的文件是否可执行---->"+file.canExecute());
 3         System.out.println("此抽象路径名表示的文件是否可读---->"+file.canRead());
 4         System.out.println("此抽象路径名表示的文件是否可写---->"+file.canWrite());
 5         System.out.println("此抽象路径名表示的文件是否存在---->"+file.exists());
 6         System.out.println("此抽象路径名表示的文件是否是一个目录---->"+file.isDirectory());
 7         System.out.println("此抽象路径名表示的文件是否是一个文件---->"+file.isFile());
 8         System.out.println("此抽象路径名表示的文件是否是一个隐藏文件---->"+file.isHidden());
 9         System.out.println("getName-->"+file.getName());// 获取文件的名字
10         System.out.println("getName-->"+file.getPath());// 获取文件的路径

获取目录下所有文件并遍历:

 1         //放进一个字符串数组
 2                 String [] a=file.list();
 3         for (String s : a) {
 4             System.out.println(s);
 5         }
 6         
 7         //放进一个File数组
 8         File [] b=file.listFiles();
 9         for (File f : b) {
10             System.out.println(f);
11         }

列出目录下的所有子文件(递归算法)

扫描二维码关注公众号,回复: 7621681 查看本文章
 1 public static void showAllFiles(File file){
 2         //用一个文件数组存放目录中的文件
 3         File [] files=file.listFiles();
 4         //遍历
 5         for (File f : files) {
 6             //文件为空,终止程序
 7             if(f==null){
 8                 return;
 9             }
10             //文件为目录形式,递归调用本身
11             if(f.isDirectory()){
12                 showAllFiles(f);
13             //是文件就打印路径名和文件名
14             }else{
15                 if(f.getName().endsWith(".exe") || f.getName().endsWith(".tmp")){
16                     System.out.println(f.getPath() + f.getName());
17                 }
18             }
19         }
20     }
21         //测试
22 //        File file1=new File("D:\\");
23 //        showAllFiles(file1);

字节流:

 1 /**
 2      * 字节流传输数据
 3      * @param copyFile      输入目标文件
 4      * @param targetFile    输出目标文件
 5      */
 6     public static void inputAndOutput(File copyFile,File targetFile){
 7         
 8         //定义输入流
 9         FileInputStream  fis=null;
10         //定义输出流
11         FileOutputStream  fos=null;
12         try {
13             //创建输入流对象
14             fis=new FileInputStream(copyFile);
15             //创建输出流对象
16             fos=new FileOutputStream(targetFile);
17             
18             //定义byte数组存放字节流(自定义大小)
19             byte [] b=new byte[1024];
20             //返回一次流对象传输的字节大小
21             @SuppressWarnings("unused")
22             int i=0;
23             //当为空时会返回-1,以此终止流的传输
24             while((i=fis.read(b))>0){
25                 fos.write(b);
26             }
27             
28         } catch (IOException e) {
29             e.printStackTrace();
30         } finally{
31             //最后释放流对象
32             try {
33                 if(fis!=null){
34                     fis.close();
35                 }
36                 if(fos!=null){
37                     fos.close();
38                 }
39                 
40             } catch (IOException e) {
41                 e.printStackTrace();
42             }
43         }
44         
45     }
46         //字节流传输数据测试
47 //        File copyFile =new File("D:\\File\\a\\b\\Integer.png");
48 //        File targetFile=new File("D:\\File\\a\\Integer.png");
49 //        inputAndOutput(copyFile,targetFile);

字符流

字符流输入:

 1 /**
 2      * 读取文件内容到控制台
 3      * @param file
 4      */
 5     public static void input(File file) {
 6         //字符流对象
 7         FileReader fr = null;
 8         //包装字符流对象
 9         BufferedReader br = null;
10         try {
11             fr = new FileReader(file);
12             br = new BufferedReader(fr);
13             String s=null;
14             do {
15                 //按行读取
16                 s=br.readLine();
17                 if(s!=null){
18                     System.out.println(s);
19                 }
20             } while (s!=null);
21             
22         } catch (IOException e) {
23             e.printStackTrace();
24         } finally {
25             //关闭流,先外后内
26             try {
27                 if (br != null) {
28                     br.close();
29                 }
30                 if (fr != null) {
31                     fr.close();
32                 }
33             } catch (IOException e) {
34                 e.printStackTrace();
35             }
36         }
37 
38     }

字符流输出:

 1 //字符流输出
 2     public static void Output(File file){
 3         FileWriter fw=null;
 4         BufferedWriter bw =null;
 5         try {
 6             fw=new FileWriter(file,true);//true 表示在文件中追加,false表示覆盖
 7             bw= new BufferedWriter(fw);
 8             bw.write("你好呀");
 9             bw.newLine();//换行
10             bw.write("世界");
11             bw.flush();
12         } catch (IOException e) {
13             e.printStackTrace();
14         } finally {
15             //关闭流,先外后内
16             try {
17                 if (bw != null) {
18                     bw.close();
19                 }
20                 if (fw != null) {
21                     fw.close();
22                 }
23             } catch (IOException e) {
24                 e.printStackTrace();
25             }
26         }
27     
28     }

测试:

1 public static void main(String[] args) {
2         File file = new File("D:\\File\\a\\a.txt");
3         //
4         input(file);
5         Output(file);
6 
7     }

猜你喜欢

转载自www.cnblogs.com/sunzhiqiang/p/11742946.html