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. maven 的聚合

  2. maven package install deploy

    1.maven package:打包到本项目,一般是在项目target目录下. 如果a项目依赖于b项目,打包b项目时,只会打包到b项目下target下,编译a项目时就会报错,因为找不到所依赖的b项目, ...

  3. HttpClient 发送 HTTP、HTTPS

    首先说一下该类中需要引入的 jar 包,apache 的 httpclient 包,版本号为 4.5,如有需要的话,可以引一下.     代码 import org.apache.commons.io ...

  4. springboot分环境打包(maven动态选择环境)

    分环境打包核心点:spring.profiles.active pom.xml中添加: <profiles> <profile> <id>dev</id> ...

  5. REVERSE!REVERSE!REVERSE!

    形式汇总: 206. Reverse Linked List 92. Reverse Linked List II:Given a string and an integer k, you need ...

  6. [leetcode]179. Largest Number最大数

    Given a list of non negative integers, arrange them such that they form the largest number. Input: [ ...

  7. 15-js提交表单的简单检测实例

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  8. centos7下源码安装mysql5.7.16

    一.下载源码包下载mysql源码包 http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.16.tar.gz 二.安装约定: 用户名:mysql 安装目录 ...

  9. SqlDataHelper

    using System;using System.Data;using System.Configuration;using System.Linq;using System.Web;using S ...

  10. chrome 调试工具的使用

    Elements chrome devtools 中 Elements panel 是审查 dom 元素和 css 的, 可以实时修改 dom/css. windows: ctrl + shift + ...