File类

  构造方法

    File(String path);

    FIle(String parent, String child);

       File(File parent, String child)

  功能:

    创建功能    boolean createNewFile();

              删除功能   boolean delect();

        判断功能   返回值为boolean     

         判断是否为绝对路径  isAbsolute();

         判断是否为文件夹     isdirectory();

         判断是否为文件        isFile();

    获取功能

        //获取文件绝对路径

       File getAbsoluteFile();

       String  getAbsolutePath();

        //获取传递路径

          String getPath();

        //获取父路径

        String  getParent();

        //获取文件大小

          long  length();

public class FileTest {
public static void main(String[] args) throws IOException {
File file1=new File("b.txt");
long l = file1.length();
long m = file1.lastModified();
Date d=new Date(m);
System.out.println(d.toLocaleString());
System.out.println(l);
}
}

  

输出结果:
2017-9-2 13:39:34
3

  

        //获取文件最后一次的修改时间

          long lastModified();  返回值为毫秒值

        

public class FileTest {
public static void main(String[] args) throws IOException {
File file1=new File("b.txt");
File file2=new File("a\\b.txt");
File file3=new File("C:\\a.txt");
//获取绝对路径
System.out.println("获取绝对路径");
File absoluteFile1= file1.getAbsoluteFile();
File absoluteFile2 = file2.getAbsoluteFile();
File absoluteFile3 = file3.getAbsoluteFile();
String absolutePath1 = file1.getAbsolutePath();
String absolutePath2 = file2.getAbsolutePath();
String absolutePath3 = file3.getAbsolutePath();
System.out.println(absoluteFile1);
System.out.println(absoluteFile2);
System.out.println(absoluteFile3);
System.out.println(absolutePath1);
System.out.println(absolutePath2);
System.out.println(absolutePath3);
System.out.println("------------");
//获取传递路径
System.out.println("获取传递路径");
String path1 = file1.getPath();
String path2 = file2.getPath();
String path3 = file3.getPath();
System.out.println(path1);
System.out.println(path2);
System.out.println(path3);
System.out.println("------------");
//获取父路径(按照传递进去的文件路径获取的)
System.out.println("获取父路径(按照传递进去的文件路径获取的)");
String parent1 = file1.getParent();
String parent2 = file2.getParent();
String parent3 = file3.getParent();
System.out.println(parent1);
System.out.println(parent2);
System.out.println(parent3);
	}
}

  

输出结果:
获取绝对路径
D:\java\myeclipse\ITEM\JYBDemo\b.txt
D:\java\myeclipse\ITEM\JYBDemo\a\b.txt
C:\a.txt
D:\java\myeclipse\ITEM\JYBDemo\b.txt
D:\java\myeclipse\ITEM\JYBDemo\a\b.txt
C:\a.txt
------------
获取传递路径
b.txt
a\b.txt
C:\a.txt
------------
获取父路径(按照传递进去的文件路径获取的)
null
a
C:\

  

字节流   (和字符流的用法一样,字符流只能读文本文件。)

    输出字节流(写数据) OutputStream(抽象类)----FileOutputStream(实现类)

    输入字节流(读数据) InputStream(抽象类)-----FileInputStream(实现类)

      eg:  复制文件

public class FileTest {
public static void main(String[] args) throws IOException {
FileInputStream fis=new FileInputStream("b.txt"); //读文件
FileOutputStream fos=new FileOutputStream("a\\b.txt");//写文件
int len;
byte[] bt=new byte[1024*1024*5];//5M
while ((len=fis.read(bt))!=-1) {
fos.write(bt,0,len);
}
fis.close();
fos.close();
}
}

      eg02  复制文件夹 

public class FileCopyNew {
public static void main(String[] args) throws IOException {
String src="D:\\java\\myeclipse\\ITEM\\JYBDemo\\a";//d:\\str 要复制的文件夹路径
String dsc="D:\\java\\myeclipse\\ITEM\\JYBDemo\\aa";//c:\\g 要复制到的文件夹路径
copy(dsc,src);
} private static void copy(String dsc, String src) throws IOException {
File df=new File(dsc);//目的
File sf=new File(src);//源文件夹
String s=df.getAbsolutePath()+"\\"+sf.getAbsolutePath().substring(sf.getParent().length());
File file=new File(s);
if (sf.isDirectory()) {
file.mkdir();//给目的文件夹创建文件夹
File[] files = sf.listFiles();
for (File file2 : files) {
copy(s, file2.toString());
}
}else{
//file.createNewFile();
chuanShu(sf.getAbsolutePath(),file.toString());
} }
private static void chuanShu(String des,String src) throws IOException {
FileInputStream fis=new FileInputStream(des);
FileOutputStream fos=new FileOutputStream(src);
int len;
byte[] b=new byte[1024*1024*50];
while ((len=fis.read(b))!=-1) {
fos.write(b,0,len);//一次读一个字节数组
}
fis.close();
fos.close();
} }

  

 过滤器

public class MyFileFilter implements FileFilter{//自定义过滤器

	@Override
public boolean accept(File f) {
if (f.getName().endsWith(".java")) {
return true;
}
if (f.isDirectory()) {
return true;
}
return false;
}
}

  

public class FileTest {
public static void main(String[] args) throws IOException {
File f=new File("a");
method(f);
}
private static void method(File f) {
File[] Files = f.listFiles(new MyFileFilter()); for (File file : Files) {
if (file.isDirectory()) {
method(file);
}else{
System.out.println(file);}//输出文件类型为java文件
}
}
}

  

  

day08(File类 ,字节流)的更多相关文章

  1. java IO之 File类+字节流 (输入输出 缓冲流 异常处理)

    1. File类

  2. 第二十天File类、字节流

    File类.字节流 File类 File类介绍 File:它是描述持久设备上的文件或文件夹的.只要是在Java程序中操作文件或文件夹肯定需要使用File类完成. File类构造方法 /* * File ...

  3. JAVA IO分析一:File类、字节流、字符流、字节字符转换流

    因为工作事宜,又有一段时间没有写博客了,趁着今天不是很忙开始IO之路:IO往往是我们忽略但是却又非常重要的部分,在这个讲究人机交互体验的年代,IO问题渐渐成了核心问题. 一.File类 在讲解File ...

  4. IO流(File类,IO流的分类,字节流和字符流,转换流,缓冲流,对象序列化)

    1.File类 File类可以在程序中 操作文件和目录.File类是通过建立File类对象,在调用File类的对象来进行相关操作的. 示例: public class Demo01 { public  ...

  5. Java基础14-缓冲区字节流;File类

    作业解析 阐述BufferedReader和BufferedWriter的工作原理, 是否缓冲区读写器的性能恒大于非缓冲区读写器的性能,为什么,请举例说明? 答: BufferedReader对Rea ...

  6. -1-4 java io java流 常用流 分类 File类 文件 字节流 字符流 缓冲流 内存操作流 合并序列流

      File类 •文件和目录路径名的抽象表示形式 构造方法 •public File(String pathname) •public File(String parent,Stringchild) ...

  7. IO流--字符流与字节流--File类常用功能

    IO流的常用方法: 1: 文件的读取和写入图解: 2:字节流: 读写文件的方法: 一般效率读取: 读取文件:        FileInputStream(); 写数据:            Fil ...

  8. 关于Java的File类、字节流和字符流

    一.File类: 在Windows下的路径分隔符(\)和在Linux下的路径分隔符(/)是不一样的,当直接使用绝对路径时,跨平台会报No Such file or diretory异常. File中还 ...

  9. File类常用的方法与字节流类方法简介

    File类常用的方法 获取功能的方法 public String getAbsolutePath() :返回此File的绝对路径名字符串. public String getPath() :将此Fil ...

随机推荐

  1. Implementing the On Item Checked Event for the TListView Control

    The TListView Delphi control displays a list of items in a fashion similar to how Windows Explorer d ...

  2. WinForm多线程编程与Control.Invoke的应用浅谈

    在WinForm开发中,我们通常不希望当窗体上点了某个按钮执行某个业务的时候,窗体就被卡死了,直到该业务执行完毕后才缓过来.一个最直接的方法便是使用多线程.多线程编程的方式在WinForm开发中必不可 ...

  3. Linux运维就业技术指导(九)期末架构考核

    一,毕业架构设计考核筹备 1.1,架构图模板示例 1.1.1 架构图(一)概述 本架构是4层lvs负载均衡给后方7层nginx反向代理: 业务进行了动静分离: 数据库前端有memcached缓存组,降 ...

  4. Python 列表表达式 ,迭代器(1)

    python 环境 3.5 1.列表: s = []; for i in s: i = handleFunction(i); s.append(i) .列表 s=[handleFunction(i) ...

  5. mysql 数值与字符类型 长度梳理

    上述表格中的数值类型都是定长的,也就是说,无论你存的数值是多少,多大或者多小,占用的存储字节大小都是固定的.例如,设置int(1),虽然M值是1个字符,但是它所占用的空间大小永远都是4个字节的大小,换 ...

  6. Null Hypothesis and Alternate Hypothesis

    1.Null Hypothesis Overview 零假设,H0是普遍接受的事实;这与备择假设(alternate hypothesis)正好相反.研究人员努力否定.驳斥零假设.研究人员提出了另一种 ...

  7. go语言中make和new的区别

    make用于内建类型(map.slice 和channel)的内存分配.new用于各种类型的内存分配. 内建函数new本质上说跟其他语言中的同名函数功能一样:new(T)分配了零值填充的T类型的内存空 ...

  8. OC 线程操作2 - NSThread

        方法1 :直接创建 alloc init - (void)createNSThread111{ /* 参数1: (nonnull id) 目标对象 self 参数2:(nonnull SEL) ...

  9. 43-python-自己的词典

    可以用python实现一个自己的词典, 就是在网上下一个英汉词典,作为自己的词库,然后整理出一个json文件,存起来,查词时,直接读取查询: 处理时可以用正则表达式处理: https://www.cn ...

  10. ubuntu系统:插入优盘read-only file system

    http://sharadchhetri.com/2013/12/19/how-to-fix-read-only-usb-pen-drive-in-ubuntu/ To fix USB pen dri ...