/*** File类:文件的创建、删除、重命名、得到路径、创建时间等,是唯一与文件本身有关的操作类*/
public class Main {public static void main(String[] args) {//File.separator 表示分隔符File f1 = new File("c:" + File.separator + "fuck" + File.separator + "javaTest1.txt");String s1 = File.pathSeparator; //路径分隔符System.out.println(File.separator + "  " + s1);boolean b1 = f1.exists(); //文件是否存在
        System.out.println(b1);if (!b1) {try {boolean bt1 = f1.createNewFile(); //创建文件
                System.out.println(bt1);} catch (IOException e) {e.printStackTrace();}}System.out.println(f1.delete()); //删除文件//System.out.println(""); 快捷键:打sout,然后中按Tab键 System.out.println(f1.getParent()); //得到文件的上一级路径
System.out.println(f1.isDirectory()); //判断是否是目录
        File f2 = new File("c:" + File.separator + "fuck" + File.separator);String[] fname = f2.list(); //列出文件夹中的所有文件名for(String i:fname) System.out.println(i);File[] files = f2.listFiles(); //列出文件中的所有文件,以file数组返回for(File i:files) System.out.println(i.getName()+" "+i.length());File f3 = new File("c:\\fuck\\JavaTest1"); System.out.println(f3.mkdir()); //创建文件夹
        f3.delete();System.out.println(f3.renameTo(new File("c:\\fuck\\JavaTest2"))); //重命名
    }
}

/*** 在某个目录中找到某个扩展名的所有文件*/
public class Main {private static int num = 0;public static void findFile(File f,String extName){if(f==null)return;else{if(f.isDirectory()){File[] fs = f.listFiles();if(fs!=null){for(File i:fs)findFile(i,extName);}}else{String path = f.getPath().toLowerCase();if(path.endsWith(extName)){System.out.println(f.getPath());++num;}}}}public static void main(String[] args) {File  f = new File("c:\\fuck\\");String extName = ".cpp";findFile(f,extName);System.out.println(num);}
}

/*** IO流:输入输出流* 流:一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。* 流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。* 根据处理数据类型的不同分为:1.字符流      2.字节流* 根据数据流向不同分为:1.输入流(程序从外部读取)       2.输出流(程序将数据写到外部)* * OutputStream类:接受输出字节并将这些字节发送到某个接收器*/
public class Main {public static void main(String[] args) {//write1();
        write2();System.out.println("finished.");}/*** 字节输出流方式一:每次输出一个字节*/public static void write1(){OutputStream out = null;try {out = new FileOutputStream("c:\\fuck\\javaTest1.txt");String info = "helloIO";byte[] bs = info.getBytes();for(int i=0;i<bs.length;++i)out.write(bs[i]);} catch (FileNotFoundException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();} finally {try {out.close();} catch (IOException ex) {ex.printStackTrace();}}}/*** 字节输出流方式二:每次输出指定大小字节*/public static void write2(){OutputStream out = null;try {out = new FileOutputStream("c:\\fuck\\javaTest1.txt",true); //参数true表示追加输出 String info = "hello fish7";byte[] bs = info.getBytes();//out.write(bs);out.write(bs,0,5);} catch (FileNotFoundException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();} finally {try {out.close();} catch (IOException ex) {ex.printStackTrace();}}}
}

public class Main {public static void main(String[] args) {//read1();
//        read2();
        read3();System.out.println("finished.");}/*** 字节输入流的读取方式一:每次读取一个字节*/public static void read1(){InputStream  in = null;try {in = new FileInputStream("c:\\fuck\\javaTest1.txt");int bs = -1; //定义一个字节,-1表示没有数据while((bs = in.read())!=-1){System.out.print((char)bs);     }System.out.println("");} catch (FileNotFoundException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();} finally{try {in.close();} catch (IOException ex) {ex.printStackTrace();}}}/*** 字节输入流的读取方式二:一次性读取所有字节(适合不太大的文件)*/public static void read2(){InputStream in = null;try {File f = new File("c:\\fuck\\javaTest1.txt");in = new FileInputStream(f);byte[] bs = new byte[(int)f.length()]; //根据文件大小构造字节数组int len = in.read(bs);System.out.println(new String(bs));System.out.println("len = "+len);} catch (FileNotFoundException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();} finally{try {in.close();} catch (IOException ex) {ex.printStackTrace();}}}/*** 字节输入流的读取方式三:每次读取指定大小的字节(折中的方法)*/public static void read3(){InputStream in = null;try {File f = new File("c:\\fuck\\javaTest1.txt");in = new FileInputStream(f);byte[] bs = new byte[5];int len = -1; //每次读取的实际长度StringBuilder sb = new StringBuilder();while((len=in.read(bs))!=-1){sb.append(new String(bs,0,len));}System.out.println(sb);} catch (FileNotFoundException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();} finally{try {in.close();} catch (IOException ex) {ex.printStackTrace();}}}
}

/*** 字符流:底层也是用字节流实现的*/
public class Main {public static void main(String[] args) {writer1();System.out.println("finished.");}/*** 字符输出流方式一:以字符数组方式输出*/public static void writer1(){Writer out = null;try {File f = new File("c:\\fuck\\javaTest1.txt");out = new FileWriter(f,true); //true表示追加方式 String info = "good good study, day day up.";//out.write(info.toCharArray());
            out.write(info);} catch (IOException ex) {ex.printStackTrace();} finally{try{out.close();} catch (IOException ex) {ex.printStackTrace();}}}
}

/*** 字符流:底层也是用字节流实现的*/
public class Main {public static void main(String[] args) {//byteReader(); //输出一堆乱码
        reader1();System.out.println("finished.");}/*** 字符输入流方式一:使用指定大小的字符数组输入*/public static void reader1(){File f = new File("c:\\fuck\\javaTest1.txt");try {Reader in = new FileReader(f);char[] cs = new char[10];int len = -1;StringBuilder sb = new StringBuilder();while((len=in.read(cs))!=-1){sb.append(new String(cs,0,len));}in.close();System.out.println(sb);} catch (FileNotFoundException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();}}/*** 使用字节流读取文本文件*/public static void byteReader(){File f = new File("c:\\fuck\\javaTest1.txt");try {InputStream in = new FileInputStream(f);byte[] bs = new byte[10];int len = -1;StringBuilder sb = new StringBuilder();while((len=in.read(bs))!=-1){sb.append(new String(bs,0,len));}in.close();System.out.println(sb);} catch (FileNotFoundException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();}}
}

/*** 字符流:底层也是用字节流实现的* :根据指定的编码,将1个或多个字节转化为java里的unicode字符,然后进行操作* 字符操作一般用Writer,Reader等,字节操作一般用InputStream,OutputStream以及各种包装类,比如:* BufferedInputStream和BufferedOutputStream等* 总结:如果你确认你要处理的流是可打印的字符,那么使用字符流会简单些,如果不确认,那么用字节流总是不会错的。*/
public class Main {public static void main(String[] args) {//byteReader(); //输出一堆乱码
        reader1();System.out.println("finished.");}/*** 字符输入流方式一:使用指定大小的字符数组输入*/public static void reader1(){File f = new File("c:\\fuck\\javaTest1.txt");try {Reader in = new FileReader(f);char[] cs = new char[10];int len = -1;StringBuilder sb = new StringBuilder();while((len=in.read(cs))!=-1){sb.append(new String(cs,0,len));}in.close();System.out.println(sb);} catch (FileNotFoundException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();}}/*** 使用字节流读取文本文件*/public static void byteReader(){File f = new File("c:\\fuck\\javaTest1.txt");try {InputStream in = new FileInputStream(f);byte[] bs = new byte[10];int len = -1;StringBuilder sb = new StringBuilder();while((len=in.read(bs))!=-1){sb.append(new String(bs,0,len));}in.close();System.out.println(sb);} catch (FileNotFoundException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();}}
}

/*** 指定一个盘符下的文件,把该文件复制到指定的目录下。*/
public class Main {public static void main(String[] args) {copyFile("c:\\fuck\\javaTest1.txt","c:\\fuck\\javaTest2.txt");copyFile("c:\\fuck\\ning.bmp","c:\\fuck\\ning2.txt"); //把.txt改成.bmp就可以看了System.out.println("finished.");}public static void copyFile(String src,String des){File f1 = new File(src);File f2 = new File(des);InputStream in = null;OutputStream out = null;try {in = new FileInputStream(f1);out = new FileOutputStream(f2);byte[] bs = new byte[105];int len = -1;while((len=in.read(bs))!=-1){out.write(bs,0,len);}} catch (FileNotFoundException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();} finally{try {in.close();out.close();} catch (IOException ex) {ex.printStackTrace();}}}
}

/*** 转换流:将一个字节流转换为字符流,也可以将一个字符流转换为字节流* OutputStreamWriter:可以将输出的字符流转换为字节流的输出形式,可使用指定的charset将要写入流*                     中的字符编码成字节。* InputStreamReader:将输入的字节流转换为字符流输入形式,使用指定的charset读取字节并将其解码为*                    字符。*/
public class Main {public static void main(String[] args) {//writer();
        reader();System.out.println("finished.");}public static void writer(){ //把字符流转成字节流try {OutputStream out = new FileOutputStream("c:\\fuck\\javaTest1.txt"); String info = "你好吗abc.";Writer w1 = new OutputStreamWriter(out); //通过字节输出流构造一个字符输出流
            w1.write(info);w1.close();out.close();} catch (FileNotFoundException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();}}public static void reader(){ //把字节流转成字符流try {InputStream in = new FileInputStream("c:\\fuck\\javaTest1.txt"); Reader r1 = new InputStreamReader(in);char[] cs = new char[105];int len = -1;StringBuilder sb = new StringBuilder();while((len=r1.read(cs))!=-1){sb.append(new String(cs,0,len));}r1.close();in.close();System.out.println(sb);} catch (FileNotFoundException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();}}
}

转载于:https://www.cnblogs.com/fish7/p/4149726.html

JAVA笔记11__File类/File类作业/字节输出流、输入流/字符输出流、输入流/文件复制/转换流...相关推荐

  1. java getfiles_Java基础教程——File类、Paths类、Files类

    File类 File类在java.io包中.io代表input和output,输入和输出. 代表与平台无关的文件和目录. 可以新建.删除.重命名,但不能访问文件内容. File类里的常量: impor ...

  2. java常用类--------File类基本用法

    package cn.zxg.PackgeUse; import java.io.File;import java.util.Date; /** * 测试File类的使用 */ public clas ...

  3. java 查看文件属性_java File类获取文件属性详解

    你知道java File类获取文件属性方法吗?下面的文章要给大家讲解的就是这个方面的内容,希望下面的内容可以对你有所帮助哦. 在Java中获取文件属性信息的第一步是先创建一个File类对象并指向一个已 ...

  4. java file_java开发之File类详细使用方法介绍

    File类简介 在 Java 中,File 类是 java.io 包中唯一代表磁盘文件本身的对象.File 类定义了一些与平台无关的方法来操作文件,File类主要用来获取或处理与磁盘文件相关的信息,像 ...

  5. java IO(一):File类

    1.File类简介 File类位于java.io包中.它面向文件层次级别操作.查看文件,而字节流.字符流操作数据时显然比之更底层. 学习File类包括以下几个重点:文件路径.文件分隔符.创建文件(目录 ...

  6. JAVA基础知识回顾-----File类-----实用

    所在包:java.io 1.文件 Java对文件管理,主要用到了IO包下的File类:主要针对文件及文件目录名称的管理,不包括文件的内容        ①声明 Java代码   public clas ...

  7. JAVA基础知识之File类

    一.File类定义 1. File类主要是JAVA为文件这块的操作(如删除.新增等)而设计的相关类 2. File类的包名是java.io,其实现了Serializable, Comparable两大 ...

  8. Java讲课笔记26:File类

    文章目录 零.本讲学习目标 1.熟悉File类的常用方法 2.掌握File类遍历目录文件的使用 3.掌握File类删除文件及目录的使用 一.File类概述 1.File的类继承图 2.File类的结构 ...

  9. 2018-08-21文件字节输出流OutputStream+文件字节输入流InputStream+字符输出流FileReader+字符输出流FileWriter...

    字节输出流OutputStream: OutputStream此抽象类,是表示输出字节流的所有类的超类!操作的数据都是字节,定义了输出字节流的基本共性功能方法! //输出流中定义都是写write方法, ...

最新文章

  1. datagrid分页问题(前后跳页)《控件版》
  2. bzoj 2179 FFT快速傅立叶
  3. TweenMax动画库学习(三)
  4. opencv对应python版本_【求问各位大佬python3.6怎么使用opencv,用哪个版本】python3 opencv...
  5. 【机器学习】一文速览机器学习的类别(Python代码)
  6. python连接各种数据库数据_Python连接各种数据库
  7. 【CSS】自定义checkbox样式
  8. Codeforces Codeforces Round #319 (Div. 2) A. Multiplication Table 水题
  9. NIOS生成Nios libaray
  10. 数学家看到就把持不住,高斯被它迷得神魂颠倒,2600年的数学史里的一个奇迹……...
  11. 学英语(1)---常用英语
  12. 【ArcGIS|空间分析|网络分析】9 使用位置分配选择最佳商店位置
  13. Inno Setup打包基本笔记
  14. excel函数去重_excel去重函数
  15. cocos2d-x 3.17.2 调用cocos studio发布的csb文件,遇到按钮图片显示不正常问题
  16. 使用微服务架构重构支付网关
  17. 转)const T 与T const的比较(const T vs.T const的翻译 Dan Saks)
  18. 【老九学堂】【初识C语言】常用字符串函数
  19. CMM是什么?什么是 “能力成熟度模型”?
  20. MessageBox弹框写删除功能

热门文章

  1. SpringTest2
  2. jeecg团队招新人(5人)
  3. Lesson_7 上课笔记_1 ----static关键字和导包
  4. C++ Primer学习随笔(一)
  5. WCF:Maximum number of items that can be serialized or deserialized in an object graph is '65536'.
  6. 汇编——NT中读取MBR内容
  7. 获取相机视口内物体在视线范围内某点的方法
  8. Spark之伪分布式搭建、伪分布式Hadoop、Hive安装
  9. 架构之各种参数对应表
  10. 小米、360、京东之后 阿里联手美的杀入智能家居市场