第20天-01-IO流(File概述)

File类:

  1. 用来将文件或者文件夹封装成对象, 方便进行操作.

  2. File对象可以作为参数, 传递给流对象的构造函数.

  3. 流对象不能操作文件夹; 流对象不能操作文件的属性信息(rwx等), 只能操作文件的数据.

构造方法:

File(File parent, String child)   Creates a new File instance from a parent abstract pathname and a child pathname string.

File(String pathname)   Creates a new File instance by converting the given pathname string into an abstract pathname.

File(String parent, String child)   Creates a new File instance from a parent pathname string and a child pathname string.

package bxd;

import java.io.File;

public class FileDemo {

    public static void consMethod() {
// 将a.txt封装成File对象, 可以将已存在的或者还未创建的"文件或文件夹"封装成对象.
File file1 = new File("a.txt");
File file2 = new File("/Users/Eric/Documents/IdeaProjects/Servlet_JSP/JDBCDemo/b.txt");
String parent = "/Users/Eric/Documents/IdeaProjects/Servlet_JSP/JDBCDemo";
File file3 = new File(parent, "c.txt"); /* static String separator
The system-dependent default name-separator character, represented as a string for convenience.
*/
File file4 = new File(File.separator + "abc" + File.separator + "xyz.txt"); // 会以字符串形式打印出创建File对象时传入的参数,不会改变.
sop("file1: " + file1);
sop("file2: " + file2);
sop("file3: " + file3);
sop("file4: " + file4);
} public static void main(String[] args) {
consMethod();
} public static void sop(Object object) {
System.out.println(object);
}
}

第20天-02-IO流(File对象功能-创建与删除)

boolean createNewFile()

  Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.

boolean mkdir()

  Creates the directory named by this abstract pathname.

boolean mkdirs()

  Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories.

static File createTempFile(String prefix, String suffix)

  Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.

static File createTempFile(String prefix, String suffix, File directory)

  Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name.

boolean delete()

  Deletes the file or directory denoted by this abstract pathname.

void deleteOnExit()

  Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates.

package bxd;

import java.io.File;
import java.io.IOException; /*
File类常见方法:
1. 创建
boolean createNewFile() 在指定位置创建文件. 如果创建成功则返回true; 如果创建失败或文件已经存在则返回false; 相比较, new一个输出流对象会马上创建文件, 且遇到文件已存在时会覆盖源文件.
boolean mkdir() 创建指定的目录, 已存在则返回false
boolean mkdirs() 创建指定的目录, 包括所有所需但不存在的父目录, 已存在则返回false
2. 删除
boolean delete() 删除失败则返回false.
void deleteOnExit() 文件有可能正被程序访问而无法删除, 此时可以将文件设置为程序退出时删除.
*/
public class FileDemo1 { public static void method_1() throws IOException {
File file = new File("file.txt");
sop("create file.txt: " + file.createNewFile());
} public static void method_2() throws IOException {
File dir = new File("dir_abc");
dir.mkdir();
} public static void method_3() {
File file = new File("file.txt");
sop("delete file.txt: " + file.delete());
} public static void main(String[] args) throws IOException {
method_2();
} public static void sop(Object object) {
System.out.println(object);
}
}

第20天-03-IO流(File对象功能-判断)

boolean canExecute()   Tests whether the application can execute the file denoted by this abstract pathname.

boolean canRead()   Tests whether the application can read the file denoted by this abstract pathname.

boolean canWrite()   Tests whether the application can modify the file denoted by this abstract pathname.

boolean exists()     Tests whether the file or directory denoted by this abstract pathname exists.

boolean isDirectory()   Tests whether the file denoted by this abstract pathname is a directory.

boolean isFile()     Tests whether the file denoted by this abstract pathname is a normal file.

boolean isHidden()   Tests whether the file named by this abstract pathname is a hidden file.

boolean isAbsolute()   Tests whether this abstract pathname is absolute.

package bxd;

import java.io.File;
import java.io.IOException; /*
File类常见方法:
3. 判断
boolean exists() 判断文件/文件夹是否存在
boolean canExecute() 判断文件是否可执行, 如果文件不存在也返回false
boolean isFile()
boolean isDirectory 判断File对象是否是文件/目录, 如果不存在也返回false
所以判断时要注意区分: 是/否/不存在 boolean isAbsolute() 判断File对象所对应的pathname是否是绝对路径(与文件/文件夹是否存在无关)
*/
public class FileDemo2 { public static void method_1() throws IOException {
File file = new File("file.txt");
sop("file.txt exists: " + file.exists());
sop("file.txt canExcute: " + file.canExecute());
} public static void method_2() {
File file = new File("file.txt");
sop("file.txt isFile: " + file.isFile());
sop("file.txt isDirectory: " + file.isDirectory());
} public static void main(String[] args) throws IOException {
method_2();
} public static void sop(Object object) {
System.out.println(object);
}
}

第20天-04-IO流(File对象功能-获取)

String getName()  Returns the name of the file or directory denoted by this abstract pathname.

String getPath()  Converts this abstract pathname into a pathname string.

String getAbsolutePath()  Returns the absolute pathname string of this abstract pathname.

String getParent()  Returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory.

long lastModified()  Returns the time that the file denoted by this abstract pathname was last modified.

long length()  Returns the length of the file denoted by this abstract pathname.

boolean renameTo(File dest)  Renames the file denoted by this abstract pathname.

package bxd;

import java.io.File;
import java.io.IOException; /*
File类常见方法:
4. 获取信息
String getName()
String getPath() 构造File时传入什么路径参数, 就返回该路径参数(种瓜得瓜)
String getAbsolutePath() 无论构造File时传入什么参数, 都返回对应的绝对路径参数.
String getParent() 如果构建File对象时传入的参数不包含父目录, 那么返回null
Returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory.
long lastModified()
long length()
boolean renameTo(File dest)
*/
public class FileDemo3 {
public static void method_1() {
File file = new File("file.txt");
sop("Path: " + file.getPath());
sop("abs Path: " + file.getAbsolutePath());
sop("parent: " + file.getParent());
} public static void method_2() {
File f1 = new File("f1.txt");
File f2 = new File("f2.txt");
// 将f1文件对象移动至f2, 看上去就像重命名
sop("rename: " + f1.renameTo(f2));
} public static void main(String[] args) throws IOException {
method_2();
} public static void sop(Object object) {
System.out.println(object);
}
}

毕向东_Java基础视频教程第20天_IO流(1~4)的更多相关文章

  1. 毕向东_Java基础视频教程第20天_IO流(7~10)

    第20天-07-IO流(递归) package bxd; import java.io.File; public class FileDemo3 { // 非递归打印 public static vo ...

  2. 毕向东_Java基础视频教程第20天_IO流(15~17)

    第20天-15-IO流(打印输出流) 打印输出流:PrintWriter与PrintStream 两者的区别:Since JDK 1.4 it's possible to specify the ch ...

  3. 毕向东_Java基础视频教程第20天_IO流(11~14)

    第20天-11-IO流(Properties简述) .properties是一种主要在Java相关技术中用来存储应用程序的可配置参数的文件的文件扩展名.它们也可以存储用于国际化和本地化的字符串,这种文 ...

  4. 毕向东_Java基础视频教程第20天_IO流(5~6)

    第20天-05-IO流(文件列表一) static File[] listRoots() List the available filesystem roots. String[] list() Re ...

  5. 毕向东_Java基础视频教程第19天_IO流(20~22)

    第19天-20-IO流(改变标准输入输出设备) static void setIn(InputStream in) Reassigns the "standard" input s ...

  6. 毕向东_Java基础视频教程第19天_IO流(01~05)

    第19天-01-IO流(BufferedWriter) 字符流的缓冲区 缓冲区的出现提高了对数据的读写效率. 对应类缓冲区要结合流才可以使用. BufferedWriter BufferedReade ...

  7. 毕向东_Java基础视频教程第19天_IO流(06~10)

    第19天-06-IO流(装饰设计模式) 装饰设计模式: 当想要对已有的对象进行功能增强时, 可以定义类,将已有对象传入,基于已有的功能,并提供加强功能.那么这个自定义的类称为装饰类. 装饰类通常会通过 ...

  8. 毕向东_Java基础视频教程第19天_IO流(11~14)

    第19天-11-IO流(字节流File读写操作) import java.io.FileInputStream; import java.io.FileOutputStream; import jav ...

  9. 毕向东_Java基础视频教程第21天_IO流(1)

    第21天-01-IO流(对象的序列化) ObjectInputStream与ObjectOutputStream 被操作的对象需要实现Serializable接口(标记接口) 非必须, 但强烈建议所有 ...

随机推荐

  1. css变量的应用

    微软在2017年3月份宣布 Edge 浏览器将支持 CSS 变量,到现在已经过去一年多了,哈哈,是不是有点后知后觉? 这个知识点是在阮一峰的日志上浏览到的,在此借用一下了..跟大家分享一下..... ...

  2. PHP 判断字符串 是否 包含另一个字符串

    1.stristr 忽略大小写 $string = 'Hello World!'; if(stristr($string, 'earth') === FALSE) { echo '"eart ...

  3. 阅读The Java® Language Specification需要知道的术语

    Null Pointer Exception,简称NPE 在java中,static final修饰的是常量.根据编译器的不同行为,常量又可分为编译时常量和运行时常量. 举例说明吧 public st ...

  4. orcale 之游标的属性

    无论是显式游标还是隐式游标都会有四个属性分别是:%ISOPEN.%FOUND.%NOTFOUND.%ROWCOUNT 下面我们分别对着几种属性进行分析. %FOUND 该属性表示当前游标是否指向有效的 ...

  5. 关于对Enum的理解

    之前一直对枚举类型的理解存在误解,现重新学习 Enum 类型的介绍 枚举类型(Enumerated Type) 很早就出现在编程语言中,它被用来将一组类似的值包含到一种类型当中.而这种枚举类型的名称则 ...

  6. ASP.NET MVC Core Starter Kit

    上一篇博文<创建.NET Core程序的Nuget Package>提到准备创建一个Nuget包,用于自动生成一个简单的ASP.NET MVC Core的示例项目.本来是打算用Nuget实 ...

  7. JavaScript中有对字符串编码的三个函数:escape,encodeURI,encodeURIComponent

    JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decod ...

  8. pygame(一)

    昨天,是有在树莓派上面操作pygame的,但是树莓派又上不了网了,很奇怪,我的306wifi显示的是树莓派连接成功,但是就是无法用网络,所以就下载不了图片,坐等HDMI线吧. 现在,看小甲鱼的视频已经 ...

  9. CSS浮动的3个特性(高手绕行)

    1.  浮动元素会脱离正常的文档流,按照其外边距指定的位置相对于它的上一个块级元素(或父元素)显示: 代码示例: <!DOCTYPE HTML > <html> <hea ...

  10. Winform学习之随笔一:Log4net

    前提题要:因为我最近负责的Winform项目,好多都用到了这个log4net的日志功能,开发程序对数据一般都要求做到雁过留痕,所以日志对于我们程序员是不可或缺.因此我把对log4net的使用做一个记录 ...