一.获得控制台用户输入的信息

     public String getInputMessage() throws IOException...{
         System.out.println("请输入您的命令∶");
         byte buffer[]=new byte[1024];
         int count=System.in.read(buffer);
         char[] ch=new char[count-2];//最后两位为结束符,删去不要
         for(int i=0;i<count-2;i++)
             ch[i]=(char)buffer[i];
         String str=new String(ch);
         return str;
     }
     可以返回用户输入的信息,不足之处在于不支持中文输入,有待进一步改进。

     二.复制文件
     1.以文件流的方式复制文件

public void copyFile(String src,String dest) throws IOException...{
         FileInputStream in=new FileInputStream(src);
         File file=new File(dest);
         if(!file.exists())
             file.createNewFile();
         FileOutputStream out=new FileOutputStream(file);
         int c;
         byte buffer[]=new byte[1024];
         while((c=in.read(buffer))!=-1)...{
             for(int i=0;i<c;i++)
                 out.write(buffer[i]);        
         }
         in.close();
         out.close();
     }
     该方法经过测试,支持中文处理,并且可以复制多种类型,比如txt,xml,jpg,doc等多种格式

     三.写文件

1.利用PrintStream写文件

public void PrintStreamDemo()...{
         try ...{
             FileOutputStream out=new FileOutputStream("D:/test.txt");
             PrintStream p=new PrintStream(out);
             for(int i=0;i<10;i++)
                 p.println("This is "+i+" line");
         } catch (FileNotFoundException e) ...{
             e.printStackTrace();
         }
     }
     2.利用StringBuffer写文件
public void StringBufferDemo() throws IOException......{
         File file=new File("/root/sms.log");
         if(!file.exists())
             file.createNewFile();
         FileOutputStream out=new FileOutputStream(file,true);        
         for(int i=0;i<10000;i++)......{
             StringBuffer sb=new StringBuffer();
             sb.append("这是第"+i+"行:前面介绍的各种方法都不关用,为什么总是奇怪的问题 ");
             out.write(sb.toString().getBytes("utf-8"));
         }        
         out.close();
     }
     该方法可以设定使用何种编码,有效解决中文问题。
四.文件重命名
    
     public void renameFile(String path,String oldname,String newname)...{
         if(!oldname.equals(newname))...{//新的文件名和以前文件名不同时,才有必要进行重命名
             File oldfile=new File(path+"/"+oldname);
             File newfile=new File(path+"/"+newname);
             if(newfile.exists())//若在该目录下已经有一个文件和新文件名相同,则不允许重命名
                 System.out.println(newname+"已经存在!");
             else...{
                 oldfile.renameTo(newfile);
             }
         }         
     }

五.转移文件目录
     转移文件目录不等同于复制文件,复制文件是复制后两个目录都存在该文件,而转移文件目录则是转移后,只有新目录中存在该文件。
    
     public void changeDirectory(String filename,String oldpath,String newpath,boolean cover)...{
         if(!oldpath.equals(newpath))...{
             File oldfile=new File(oldpath+"/"+filename);
             File newfile=new File(newpath+"/"+filename);
             if(newfile.exists())...{//若在待转移目录下,已经存在待转移文件
                 if(cover)//覆盖
                     oldfile.renameTo(newfile);
                 else
                     System.out.println("在新目录下已经存在:"+filename);
             }
             else...{
                 oldfile.renameTo(newfile);
             }
         }       
     }
     六.读文件
     1.利用FileInputStream读取文件

    
     public String FileInputStreamDemo(String path) throws IOException...{
         File file=new File(path);
         if(!file.exists()||file.isDirectory())
             throw new FileNotFoundException();
         FileInputStream fis=new FileInputStream(file);
         byte[] buf = new byte[1024];
         StringBuffer sb=new StringBuffer();
         while((fis.read(buf))!=-1)...{
             sb.append(new String(buf));    
             buf=new byte[1024];//重新生成,避免和上次读取的数据重复
         }
         return sb.toString();
     }
2.利用BufferedReader读取

在IO操作,利用BufferedReader和BufferedWriter效率会更高一点

public String BufferedReaderDemo(String path) throws IOException...{
         File file=new File(path);
         if(!file.exists()||file.isDirectory())
             throw new FileNotFoundException();
         BufferedReader br=new BufferedReader(new FileReader(file));
         String temp=null;
         StringBuffer sb=new StringBuffer();
         temp=br.readLine();
         while(temp!=null)...{
             sb.append(temp+" ");
             temp=br.readLine();
         }
         return sb.toString();
     }

   3.利用dom4j读取xml文件

public Document readXml(String path) throws DocumentException, IOException...{
         File file=new File(path);
         BufferedReader bufferedreader = new BufferedReader(new FileReader(file));
         SAXReader saxreader = new SAXReader();
         Document document = (Document)saxreader.read(bufferedreader);
         bufferedreader.close();
         return document;
     }
     七.创建文件(文件夹)

1.创建文件夹  
     public void createDir(String path)...{
         File dir=new File(path);
         if(!dir.exists())
             dir.mkdir();
     }
2.创建新文件
     public void createFile(String path,String filename) throws IOException...{
         File file=new File(path+"/"+filename);
         if(!file.exists())
             file.createNewFile();
     }
     八.删除文件(目录)
1.删除文件     
     public void delFile(String path,String filename)...{
         File file=new File(path+"/"+filename);
         if(file.exists()&&file.isFile())
             file.delete();
     }
2.删除目录
要利用File类的delete()方法删除目录时,必须保证该目录下没有文件或者子目录,否则删除失败,因此在实际应用中,我们要删除目录,必须利用递归删除该目录下的所有子目录和文件,然后再删除该目录。  
     public void delDir(String path)...{
         File dir=new File(path);
         if(dir.exists())...{
             File[] tmp=dir.listFiles();
             for(int i=0;i<tmp.length;i++)...{
                 if(tmp[i].isDirectory())...{
                     delDir(path+"/"+tmp[i].getName());
                 }
                 else...{
                     tmp[i].delete();
                 }
             }
             dir.delete();
         }
     }

java读写操作心得的更多相关文章

  1. java 读写操作大文件 BufferedReader和RandomAccessFile

    一 老问这问题,两个都答出来算加分项? 二 具体代码如下,没什么好说的直接说对比. BufferedReader和RandomAccessFile的区别RandomAccessFile 在数据越大,性 ...

  2. java 读写操作

    java代码: 写入: public void getNotice(HttpServletRequest request, String notice){ String message = JSON. ...

  3. Java读写文本文件操作

    package com.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; ...

  4. Java的文件读写操作

    file(内存)----输入流---->[程序]----输出流---->file(内存) 当我们读写文本文件的时候,采用Reader是非常方便的,比如FileReader,InputStr ...

  5. Java的文件读写操作 <转>

    目录: file内存----输入流----程序----输出流----file内存 java中多种方式读文件 判断文件是否存在不存在创建文件 判断文件夹是否存在不存在创建文件夹 java 写文件的三种方 ...

  6. Java 字节流实现文件读写操作(InputStream-OutputStream)

    Java 字节流实现文件读写操作(InputStream-OutputStream) 备注:字节流比字符流底层,但是效率底下. 字符流地址:http://pengyan5945.iteye.com/b ...

  7. Java 字符流实现文件读写操作(FileReader-FileWriter)

    Java 字符流实现文件读写操作(FileReader-FileWriter) 备注:字符流效率高,但是没有字节流底层 字节流地址:http://pengyan5945.iteye.com/blog/ ...

  8. 最全java的读写操作(转载)

    Java的I/O系统中的输入输出流为我们进行开发提供了很多便利,利用其强大的封装性,通过各种组合能够实现多种多样的功能.但是Java提供了很多输入输出流类,在概念和使用上有很多相似之处,所以给很多开发 ...

  9. java文件读写操作

    Java IO系统里读写文件使用Reader和Writer两个抽象类,Reader中read()和close()方法都是抽象方法.Writer中 write(),flush()和close()方法为抽 ...

随机推荐

  1. 深度强化学习介绍 【PPT】 Human-level control through deep reinforcement learning (DQN)

    这个是平时在实验室讲reinforcement learning 的时候用到PPT, 交期末作业.汇报都是一直用的这个,觉得比较不错,保存一下,也为分享,最早该PPT源于师弟汇报所做.

  2. TrueCrypt 7.1a Hashes

    Here are the SHA256, SHA1, and MD5 hashes of all TrueCrypt version 7.1a files. The signature of the ...

  3. ss server端配置

    关于ss server的配置,可以参考一个网址 关于服务器的购买可以上VIRMACH购买 和本地安装ss类似,首先安装ss,pip install shadowsocks 配置服务器参数,vim /e ...

  4. event.currentTarget和event.target的区别

    currentTarget始终是监听事件者,而target是事件的真正发出者.

  5. linux下简单制作iso,img镜像文件

    1. 如果你是直接从cd压制iso文件的,执行sudo umount /dev/cdromdd if=/dev/cdrom of=file.iso bs=1024 2. 如果你要把某个文件或者目录压到 ...

  6. C++学习(三十一)(C语言部分)之 栈和队列(括号匹配示例)

    括号匹配测试代码笔记如下: #include<stdio.h> #include<string.h> #include <stdlib.h> #define SIZ ...

  7. 滚动加载图片(懒加载)实现原理(这是旧实现,仅做为获取元素宽高api的参考)

    https://www.cnblogs.com/flyromance/p/5042187.html 本文主要通过以下几方面来说明懒加载技术的原理,个人前端小菜,有错误请多多指出 一.什么是图片滚动加载 ...

  8. ionic中 ng-repeat下使用ng-model获取不到选中数据问题:

    在这种情况下点击了单选框并不能获取$scope.unitcode的值: <ion-list> <ion-radio ng-repeat="item in userInfos ...

  9. apache常用配置文件讲解

    apache 的httpd.conf常用配置说明 # ServerRoot: The top of the directory tree under which the server's # conf ...

  10. 自定义简单的模板引擎-JS模板引擎

    http://www.cnblogs.com/52fhy/p/5393673.html