zoukankan      html  css  js  c++  java
  • java:文件与IO

     1 package com.vince;
     2 
     3 import java.io.File;
     4 
     5 /**
     6  * 在指定的目录中查找文件
     7  * @author vince
     8  * @description
     9  */
    10 public class FileDemo2 {
    11 
    12     public static void main(String[] args) {
    13         findFile(new File("C:\\Users\\vince\\Downloads"),".jpg");
    14     }
    15     
    16     //查找文件的方法
    17     private static void findFile(File target,String ext){
    18         if(target==null)return;
    19         //如果文件是目录
    20         if(target.isDirectory()){
    21             File[] files = target.listFiles();
    22             if(files!=null){
    23                 for(File f: files){
    24                     findFile(f,ext);//递归调用
    25                 }
    26             }
    27         }else{
    28             //此处表示File是一个文件
    29             String name = target.getName().toLowerCase();
    30             //System.out.println(name);
    31             if(name.endsWith(ext)){
    32                 System.out.println(target.getAbsolutePath());
    33             }
    34         }
    35     }
    36 
    37 }
    FileDemo2.java

     

     1 package com.vince;
     2 
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.FileNotFoundException;
     6 import java.io.FileOutputStream;
     7 import java.io.IOException;
     8 import java.io.InputStream;
     9 import java.io.OutputStream;
    10 
    11 /**
    12  * 字节输出输入流
    13  * 输出流:超类 OutputStream,对文件的输出流使用子类FileOutputStream
    14  * 输入流:超类InputStream,对文件的输入流使用子类FileInputStream
    15  * 
    16  * 输入输出字节流操作原理,每次只会操作一个字节,(从文件中读取或写入 )
    17  * 字节操作流,默认每次执行写入操作会直接把数据写入文件
    18  * @author vince
    19  * @description
    20  */
    21 public class ByteStreamDemo {
    22     
    23     private static void in(){
    24         //0、确定目标文件
    25         File file = new File("c:\\test\\vince.txt");
    26         //1、构建 一个文件输入流对象
    27         try {
    28             InputStream in = new FileInputStream(file);
    29             
    30             byte[] bytes = new byte[10];
    31             StringBuilder buf = new StringBuilder();
    32             int len = -1;//表示每次读取的字节长度
    33             //把数据读入到数组中,并返回读取的字节数,当不等-1时,表示读取到数据,等于-1表示文件已经读完
    34             while((len = in.read(bytes))!=-1){
    35                 //根据读取到的字节数组,再转换为字符串内容,添加到StringBilder中
    36                 buf.append(new String(bytes,0,len));
    37             }
    38             //打印内容:
    39             System.out.println(buf);
    40             //关闭输入流
    41             in.close();
    42             
    43         } catch (FileNotFoundException e) {
    44             e.printStackTrace();
    45         } catch (IOException e) {
    46             e.printStackTrace();
    47         }    
    48     }
    49     
    50     private static void out(){
    51         //0、确定目标文件
    52         File file = new File("c:\\test\\vince.txt");
    53         //1、构建一个文件输出流对象
    54         try {
    55             OutputStream out = new FileOutputStream(file,true); //append 为true表示追加内容
    56             //2、输出的内容
    57             String info = "小河流水哗啦啦\r\n";// \r\n表示换行
    58             
    59             //String line = System.getProperty("line.separator");//获取换行符
    60             //3、把内容写入到文件
    61             out.write(info.getBytes());
    62             //4、关闭流
    63             out.close();
    64             System.out.println("write success.");
    65             
    66         } catch (FileNotFoundException e) {
    67             e.printStackTrace();
    68         } catch (IOException e) {
    69             e.printStackTrace();
    70         }
    71         
    72     }
    73 
    74     public static void main(String[] args) {
    75 
    76         out();
    77 //        in();
    78     }
    79 
    80 }
    ByteStreamDemo.java

     1 package com.vince;
     2 
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.FileNotFoundException;
     6 import java.io.FileOutputStream;
     7 import java.io.IOException;
     8 import java.io.InputStream;
     9 import java.io.OutputStream;
    10 
    11 /**
    12  * 文件的复制:
    13  * 从一个输入流中读取数据,然后通过输出流写入目标位置
    14  * 一边读一边写
    15  * @author vince
    16  * @description
    17  */
    18 public class CopyFileDemo {
    19 
    20     public static void main(String[] args) {
    21         System.out.println("start copy...");
    22         copy("c:\\mm.jpg","C:\\test\\mm.jpg");
    23         System.out.println("copy success.");
    24     }
    25     
    26     private static void copy(String src,String target){
    27         File srcFile = new File(src);
    28         File targetFile = new File(target);
    29         InputStream in = null;
    30         OutputStream out = null;
    31         
    32         try {
    33             in = new FileInputStream(srcFile);
    34             out = new FileOutputStream(targetFile);
    35             
    36             byte[] bytes = new byte[1024];
    37             int len = -1;
    38             while((len=in.read(bytes))!=-1){
    39                 out.write(bytes, 0, len);
    40             }
    41             
    42         } catch (FileNotFoundException e) {
    43             e.printStackTrace();
    44         } catch (IOException e) {
    45             e.printStackTrace();
    46         }finally{
    47             try {
    48                 if(in!=null)in.close();
    49                 if(out!=null)out.close();
    50             } catch (IOException e) {
    51                 e.printStackTrace();
    52             }
    53         }
    54     }
    55 
    56 }
    View Code

  • 相关阅读:
    PHP获取一周的日期
    关系型数据库和非关系型数据库的区别和特点
    关系型数据库和非关系型数据库的特性以及各自的优缺点
    什么是数据库?什么是关系数据库?什么是非关系型数据库?
    PHP中把对象转数组的几个方法
    验证银行卡号
    xss过滤方法
    PHP 随机字符
    计算两个日期相差年月日
    判断一个时间段是否包含周末
  • 原文地址:https://www.cnblogs.com/juham/p/15701509.html
Copyright © 2011-2022 走看看