zoukankan      html  css  js  c++  java
  • 文件操作一

    一、             File

    File是整个java.io包中一个独立的类。此类的功能是完成与整个java平台无关的文件操作。

    创建文件示例代码如下:

    import java.io.File;

    public class FileDemo1 {

       public static void main(String[] args) throws Exception{

         // TODO Auto-generated method stub

         File file=new File("c:"+File.separator+"hi.demo");

         if(!file.exists()) file.createNewFile();

       }

    }

    二、             字节输出流:

    字节和字符最大的区别在于,字节不需要缓存,而字符需要缓存。

    字节输出流最大的父类是:OutputStream。需要子类实例化,示例代码如下:

    import java.io.File;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.io.OutputStream;

    public class FileOutputStreamDemo {

       public static void main(String[] args) throws IOException {

         // TODO Auto-generated method stub

         File file=new File("c:"+File.separator+"hi.txt");

         if(!file.exists()) file.createNewFile();

         OutputStream out=new FileOutputStream(file);

         String str="hello world";

         byte[] bytes=str.getBytes();

         out.write(bytes);

         out.close();

       }

    }

    三、             字节输入流

    最大的父类是:InputStream,示例代码如下:

    import java.io.File;

    import java.io.FileInputStream;

    import java.io.FileNotFoundException;

    import java.io.InputStream;

    public class FileInputStreamDemo {

       /**

        * @param args

        * @throws FileNotFoundException 

        */

       public static void main(String[] args) throws Exception {

         File f=new File("c:"+File.separator+"hi.txt");

         InputStream in=new FileInputStream(f);

         byte[]  b=new byte[(int) f.length()];

         int len=in.read(b);

         System.out.println(new String(b,0,len));

         in.close();

       }

    }

    四、             字符输出流

    Writer:字符输出流,示例代码如下:

    import java.io.File;

    import java.io.FileWriter;

    import java.io.Writer;

    public class WriterDemo {

       public static void main(String[] args) throws Exception {

         // TODO Auto-generated method stub

         File file=new File("c:"+File.separator+"hi.txt");

         Writer w=new FileWriter(file,true);

         w.write("this is is writer");

         w.close();

       }

    }

  • 相关阅读:
    C# 中ArrayList强制转化为数组【转】
    [转]rdlc和rdl的区别
    用EditPlus打造C#编程环境
    aspnet_wp.exe 未能启动
    手工打造C#IDE环境(一):万事开头难
    不让Hashtable排序
    Javascript实现把网页中table的内容导入到excel中的几种方法
    jQuery UI draggable+droppable+resizable+selectable+sortable
    CodeIgniter整理
    PHP文件 文件夹操作
  • 原文地址:https://www.cnblogs.com/itfenqing/p/4429546.html
Copyright © 2011-2022 走看看