• io流读写及相关内容


    列出某个目录下的所有文件:

    File file = new File("e:\总结");
    File[] files = file.listFiles();
    for(int i=0; i<files.length; i++){
     if(files[i].isFile()) System.out.println(files[i]);
    }

    列出某个目录下的所有子目录:

    File file = new File("e:\总结");
    File[] files = file.listFiles();
    for(int i=0; i<files.length; i++){
     if(files[i].isDirectory()) 
     System.out.println(files[i]);
    }

    如何读写文件,代码如下,有读取,复制,写入:

      1 public class ReadWriteCopy {
      2     public static void main(String args[]) {
      3         String from = "h:\Android\readtext.txt";
      4         String to = "h:\Android\writetext.txt";
      5         readline(from);
      6         copyText(from,to);
      7         writeText(to);
      8         //deletefile(from);
      9     }
     10 
     11     public static void deletefile(String from){
     12         File fromFile=new File(from);
     13         if(fromFile.exists()){
     14             fromFile.delete();
     15         }        
     16     }
     17     public static void readline(String from) {
     18         File fromFile = new File(from);
     19         BufferedReader reader=null;    
     20         //FileReader fr=null;//可以用这个
     21         if (fromFile.exists()) {
     22             try {        
     23                 //InputStreamReader isr=new InputStreamReader(new DataInputStream(new FileInputStream(fromFile)),"gb2312");
     24                 //或者
     25                 InputStreamReader isr=new InputStreamReader(new BufferedInputStream(new FileInputStream(fromFile),1024),"gb2312");
     26                 reader=new BufferedReader(isr);
     27                 List<users> userList=new ArrayList<users>();
     28                 String row="";
     29                 while ((row=reader.readLine()) != null) {    
     30                     users u=new users();
     31                     String[] text=row.split(",");
     32                     u.setUserName(text[0]);
     33                     u.setUserId(text[1]);
     34                     u.setUserDate(text[2]);
     35                     userList.add(u);
     36                 }
     37                 for(users u:userList){
     38                     System.out.println("用户名:"+u.getUserName()+"id:"+u.getUserId()+"date:"+u.getUserDate());
     39                 }
     40             }catch (IOException e1) {
     41                 // TODO Auto-generated catch block
     42                 e1.printStackTrace();
     43             }finally{
     44                 try {
     45                     reader.close();
     46                 } catch (IOException e) {
     47                     // TODO Auto-generated catch block
     48                     e.printStackTrace();
     49                 }
     50             } 
     51         }
     52     }
     53     
     54     public static void copyText(String from,String to){
     55         File fromFile = new File(from);
     56         File toFile = new File(to);
     57         BufferedInputStream fis=null;
     58         BufferedOutputStream fos=null;
     59         try {
     60             fis=new BufferedInputStream(new FileInputStream(fromFile));
     61             fos=new BufferedOutputStream(new FileOutputStream(toFile));
     62             
     63             int num;
     64             while((num=fis.read())!=-1){
     65                 fos.write(num);
     66             }
     67         } catch (FileNotFoundException e) {
     68             // TODO Auto-generated catch block
     69             e.printStackTrace();
     70         } catch (IOException e) {
     71             // TODO Auto-generated catch block
     72             e.printStackTrace();
     73         }finally{
     74             try {
     75                 fis.close();
     76                 fos.close();
     77             } catch (IOException e) {
     78                 // TODO Auto-generated catch block
     79                 e.printStackTrace();
     80             }            
     81         }
     82         
     83     }
     84     
     85     public static void writeText(String to){
     86         String text="往txt文件里写入该字符串";
     87         File toFile = new File(to);
     88         FileOutputStream fos=null;
     89         FileWriter fw=null;
     90         try {
     91             fos=new FileOutputStream(toFile);
     92             fos.write(text.getBytes());
     93             
     94             fw=new FileWriter(toFile);
     95             fw.write(text);
     96         } catch (FileNotFoundException e) {
     97             // TODO Auto-generated catch block
     98             e.printStackTrace();
     99         } catch (IOException e) {
    100             // TODO Auto-generated catch block
    101             e.printStackTrace();
    102         }
    103     }
    104 }
  • 相关阅读:
    如何在Infraworks中创建多树种组成的森林
    Autodesk 2013开发者日(DevDays)又要来了 -- 北京(2013年11月7日)和上海(2013年11月11日)
    Mac下的Parallel Windows忘记密码怎么办?
    几个有用的JSON工具
    使用Autodesk OAuth服务在用户认证的示例
    ElasticSearch(九)e代驾使用Elasticsearch流程设计(Yii1版本)
    ElasticSearch(八)Elasticsearch-head 连接不上Elasticsearch的原因和解决方案
    ElasticSearch(七) Elasticsearch在Centos下搭建可视化服务
    Yii1自定义 CGridView 中的操作按钮中 CButtonColumn 选项
    Mysql BLOB、BLOB与TEXT区别及性能影响、将BLOB类型转换成VARCHAR类型
  • 原文地址:https://www.cnblogs.com/jiuqing/p/4138633.html
Copyright © 2020-2023  润新知