JavaSE IO流

1.文件操作:

在Java中如何操作磁盘中文件,java中把文件作为一个对象进行操作
  
  File类进行表示磁盘中的文件:
     路径:
          绝对路径:从磁盘的根目录进行表示 如:E:\vedio\20170713\JavaSE\T_day01\question\常见面试题_01.txt
          相对路径:  相对某一个参照物进行定位文件,参照物 :E:\vedio\20170713\JavaSE\T_day01   路径:question\常见面试题_01.txt
      
     路径的分隔符:包括 : / \
     File.separator  斜杠
     File.pathSeparatorChar  冒号 
     
     E:\vedio\a.txt -->E+File.pathSeparatorChar+File.separator+vedio+File.separator+a.txt

2.如何创建一个文件对象

File(File parent, String child)
          根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。
File(String pathname)
          通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。
File(String parent, String child)
          根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例

3.如何在磁盘使用Java创建一个新的文件      

 booleancreateNewFile()
          当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件。
booleandelete()
          删除此抽象路径名表示的文件或目录。
 booleanexists()
          测试此抽象路径名表示的文件或目录是否存在。
StringgetName()
          返回由此抽象路径名表示的文件或目录的名称。
 StringgetParent()
          返回此抽象路径名父目录的路径名字符串;如果此路径名没有指定父目录,则返回 null
 FilegetParentFile()
          返回此抽象路径名父目录的抽象路径名;如果此路径名没有指定父目录,则返回 null
 longlength()
          返回由此抽象路径名表示的文件的长度。
 String[]list()
          返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录。
StringtoString()
          返回此抽象路径名的路径名字符串。

创建新的目录

 File f = new File("d:\\video\\124.txt");                File f1 = new File("d:\\video","123.txt");

File(File parent, String child)
          根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。
File(String pathname)
          通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。

创建新的  文件夹(目录

new File("D:\\video\\123").mkdir;          创建名为123的文件夹,video文件夹必须有

new File("D:\\video2\\123").mkdir;        创建video和123文件夹

 booleanmkdir()
          创建此抽象路径名指定的目录。
 booleanmkdirs()
          创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。

        File f1 = new File("D:\\video6\\122\\1.txt");          

        if (f1.getParentFile().exists()) {
            f1.createNewFile();
        } else {
            f1.getParentFile().mkdirs();           //创建1.txt之前所有的文件夹
            f1.createNewFile();                      //再创建1.txt
        }     

IO流:数据的传输

    从方向上分 :输入   和   输出  (以计算机的内存)
    从数据上分 : 字节流 (以字节为单位读取)   字符流(以字符为单位)
    从功能上分 : 节点流   包装流(经过处理的流 ,目的提高效率)
    
   所有的流都来自于 四个抽象类:
   InputStream    OutputStream   ---》字节流  (读写任何文件)
   Reader         Writer         ---》字符流  (只能读写文本文件)

           
 字节流
     InputStream --》FileInputStream    InputStream in = new FileInputStream("D:\\video\\123.txt");
     OutputStream--》FileOutputStream
     
 字符流
      Reader  ---》FileReader
      Writer  ---》FileWriter
   

包装流:
      BufferedReader 
      BufferedWriter

29094246_L91E.jpg

一行一次读!!!!!
BufferedReader br = null;

BufferedWriter bw = null;

//创建FileReader对象,要用FileReader再转BufferedReader

FileReader fr = new FileReader("d:\\d.txt");

FileWriter fw = new FileWriter("d:\\dcopy.txt");

br = new BufferedReader(fr);

bw = new BufferedWriter(fw);

//循环读取文件

String s = "";

while((s=br.readLine()) != null)

{

  //按行读取哦。

  bw.write(s + "\r\n"); //这里写入不包括换行符,所以加上去就可以了

}

     数组字节流:是操作数组中的数据,和文件没有关系
     ByteArrayInputStream  
     ByteArrayOutputStream

 

字节流转换为字符流

public static void convertByteToChar() throws IOException{
        File file= new File( "d:/test.txt");
         //获得一个字节流
        InputStream is= new FileInputStream( file);
         //把字节流转换为字符流,其实就是把字符流和字节流组合的结果。
        Reader reader= new InputStreamReader( is);
         char [] byteArray= new char[( int) file.length()];
         int size= reader.read( byteArray);
        System. out.println( "大小:"+size +";内容:" +new String(byteArray));
         is.close();
         reader.close();
  }

       复制文件到另一个地方

        File f1 = new File("D:\\video\\024.txt");
        File f2 = new File("D:\\video7\\test");
        try {
            InputStream in = new FileInputStream(f1);

            byte[] bs = new byte[1024];
            if (!f2.getParentFile().exists()) {

                f2.getParentFile().mkdirs();
    
            }
            OutputStream ou = new FileOutputStream(f2);

           int len = 0;
            while (len=(in.read(bs, 0, bs.length)) != -1) {
                 ou.write(bs,0,bs.len); 
            }

            in.close();
            ou.close();

   
  
 
    
   
    

http://blog.csdn.net/yuebinghaoyuan/article/details/7388059

转载于:https://my.oschina.net/u/3611296/blog/1490767

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值