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 }
时间: 2024-07-30 08:10:08

io流读写及相关内容的相关文章

Java IO流读写文件的几个注意点

平时写IO相关代码机会挺少的,但却都知道使用BufferedXXXX来读写效率高,没想到里面还有这么多陷阱,这两天突然被其中一个陷阱折腾一下:读一个文件,然后写到另外一个文件,前后两个文件居然不一样? 解决这个问题之后,总结了几个注意点. 注意点一:Reader/Writer读写二进制文件是有问题的 : public void copyFile1() { File srcFile = new File("E://atest//atest.txt"); File dstFile = ne

191108、Java IO流读写文件的几个注意点

平时写IO相关代码机会挺少的,但却都知道使用BufferedXXXX来读写效率高,没想到里面还有这么多陷阱,这两天突然被其中一个陷阱折腾一下:读一个文件,然后写到另外一个文件,前后两个文件居然不一样? 解决这个问题之后,总结了几个注意点. 注意点一:Reader/Writer读写二进制文件是有问题的 : 上面代码使用BufferedReader一行一行地读取一个文件,然后使用BufferedWriter把读取到的数据写到另外一个文件中.如果文件是ASCCII形式的,则内容还是能够正确读取的.但如

IO流 读写文件

读写文件 如前所述,一个流被定义为一个数据序列.输入流用于从源读取数据,输出流用于向目标写数据. 下图是一个描述输入流和输出流的类层次图. 下面将要讨论的两个重要的流是 FileInputStream 和 FileOutputStream: FileInputStream 该流用于从文件读取数据,它的对象可以用关键字 new 来创建. 有多种构造方法可用来创建对象. 可以使用字符串类型的文件名来创建一个输入流对象来读取文件: InputStream f = new FileInputStream

文件批量生成IO流读写

/// <summary> /// 生成文件的 /// </summary> /// <param name="calssName"></param> public void create(string calssName) { //获取程序集 var createClass = Assembly.Load("CloudInnovate.Backstage.Entity"); //反射出所有的类 List<Typ

IO流-递归删除带内容的目录

/* * 需求:递归删除带内容的目录 * * 目录:demo * ---------------------------------------------------------------------------------- * 具体的demo目录如下: * demo\aaaa\a1.txt * demo\aaaa\a2.txt * demo\bbbb\b1.txt * demo\bbbb\b2.txt * 具体的删除结果如下:(删除顺序:先删某文件夹下的文件,再删该文件) * a1.tx

java io流清空txt文件内容及填充文件内容

File file = new File(this.getClass().getResource("").getPath());System.out.println(file.getPath());String path=file.getPath();String filename="test1.txt";//path=path.substring(0,path.indexOf("WEB-INF"))+"upload\\Excel&qu

java IO流文件的读写具体实例(转载)

引言: 关于java IO流的操作是非常常见的,基本上每个项目都会用到,每次遇到都是去网上找一找就行了,屡试不爽.上次突然一个同事问了我java文件的读取,我一下子就懵了第一反应就是去网上找,虽然也能找到,但自己总感觉不是很踏实,所以今天就抽空看了看java IO流的一些操作,感觉还是很有收获的,顺便总结些资料,方便以后进一步的学习... IO流的分类:1.根据流的数据对象来分:高端流:所有的内存中的流都是高端流,比如:InputStreamReader  低端流:所有的外界设备中的流都是低端流

IO流--字符流写数据

IO流是用来处理设备之间的数据传输的,诸如:文件的复制,上传下载文件 Java中的流可以从不同的角度进行分类: - 按照流的方向不同:分为输入流和输出流. - 按照处理数据单位的不同:分为字节流和字符流. - 按照功能不同:分为节点流和处理流 要区分字符流和字节流,我们可以从类名来区分 类名中包含Reader:字符流  输入流 类名中包含Writer:字符流  输出流 类名中包含Input:字节流  输入流 类名中包含Output:字节流  输出流 包含Stream:字节流 今天着重来看下字符流

IO流相关01

File类的常用静态方法: void AppendAllText(string path, string contents),将文本contents附加到文件path中 bool Exists(string path)判断文件path是否存在 string ReadAllText(string path) 读取文本文件到字符串中 string[] ReadAllLines(string path) 读取文本文件到字符串数组中 void WriteAllText(string path, stri