File-- byte stream - character stream

File type

java.io.FileClass is an abstract file and directory path names indicate, mainly for files and directories are created, find and delete operations.

1. Constructor

public File(String pathname) :通过将给定的路径字符串创建新的 File实例。
public File(String parent, String child) :通过父路径字符串和子文件名创建新的 File实例。
public File(File parent, String child) :从File对象和子文件创建新的 File实例。

Examples demo

// 文件路径名
String pathname = "D:\\aaa.txt";
File file1 = new File(pathname);

// 文件路径名
String pathname2 = "D:\\aaa\\bbb.txt";
File file2 = new File(pathname2);

// 通过父路径和子路径字符串
String parent = "d:\\aaa";
String child = "bbb.txt";
File file3 = new File(parent, child);

// 通过父级File对象和子路径字符串
File parentDir = new File("d:\\aaa");
String child = "bbb.txt";
File file4 = new File(parentDir, child);

Note:

  1. A File object represents the actual existence of a hard disk file or directory.
  2. Whether or not there is a file or directory in the path, do not affect the creation of a File object.

2. The common method

2.1 acquisition function method
public String getAbsolutePath() :返回此File的绝对路径。
public String getPath() :将此File转换为路径名字符串。
public String getName() :返回由此File表示的文件或目录的名称。
public long length() :返回由此File表示的文件的长度。

Examples demo

File f = new File("d:/aaa/bbb.java");
System.out.println("文件绝对路径:"+f.getAbsolutePath());
System.out.println("文件构造路径:"+f.getPath());
System.out.println("文件名称:"+f.getName());
System.out.println("文件长度:"+f.length()+"字节");
File f2 = new File("aaa");
System.out.println("目录绝对路径:"+f2.getAbsolutePath());
System.out.println("目录构造路径:"+f2.getPath());
System.out.println("目录名称:"+f2.getName());
System.out.println("目录长度:"+f2.length());

文件绝对路径:d:\aaa\bbb.java
文件构造路径:d:\aaa\bbb.java
文件名称:bbb.java
文件长度:0字节
目录绝对路径:F:\javaStudyFinally\javaStudy20191028\studyjava\aaa
目录构造路径:aaa
目录名称:aaa
目录长度:0
The method of determining function of 2.2
public boolean exists() :此File表示的文件或目录是否实际存在。
public boolean isDirectory() :此File表示的是否为目录。
public boolean isFile() :此File表示的是否为文件。

Examples Demo (d next disc has a folder name aaa)

File f = new File("d:\\aaa\\bbb.java");
File f2 = new File("d:\\aaa");
// 判断是否存在
System.out.println("d:\\aaa\\bbb.java 是否存在:"+f.exists());
System.out.println("d:\\aaa 是否存在:"+f2.exists());
// 判断是文件还是目录
System.out.println("d:\\aaa 文件?:"+f2.isFile());
System.out.println("d:\\aaa 目录?:"+f2.isDirectory());

d:\aaa\bbb.java 是否存在:false
d:\aaa 是否存在:true
d:\aaa 文件?:false
d:\aaa 目录?:true
2.3 Creating delete function method
public boolean createNewFile() :当且仅当具有该名称的文件尚不存在时,创建一个新的空文件。
public boolean delete() :删除由此File表示的文件或目录。
public boolean mkdir() :创建由此File表示的目录。
public boolean mkdirs() :创建由此File表示的目录,包括任何必需但不存在的父目录。

Examples demo

// 文件的创建
File f = new File("aaa.txt");
System.out.println("是否存在:"+f.exists()); // false
System.out.println("是否创建:"+f.createNewFile()); // true
System.out.println("是否存在:"+f.exists()); // true
// 目录的创建
File f2= new File("newDir");
System.out.println("是否存在:"+f2.exists());// false
System.out.println("是否创建:"+f2.mkdir()); // true
System.out.println("是否存在:"+f2.exists());// true
// 创建多级目录
File f3= new File("newDira\\newDirb");
System.out.println(f3.mkdir());// false
File f4= new File("newDira\\newDirb");
System.out.println(f4.mkdirs());// true
// 文件的删除
System.out.println(f.delete());// true
// 目录的删除
System.out.println(f2.delete());// true
System.out.println(f4.delete());// false
Directory traversal
public String[] list() :返回一个String数组,表示该File目录中的所有子文件或目录。
public File[] listFiles() :返回一个File数组,表示该File目录中的所有的子文件或目录。

IO flow

The flow of data is divided into: the input and output streams. (In memory as a reference)
input streams: the data read from the other memory devices to the stream.
Output stream: Write data stream from memory to other devices.
Depending on the type of data is divided into: byte stream and character stream.
Byte stream: bytes, reading and writing of stream data.
Character stream: in characters, reading and writing of stream data.

Input stream Output stream
Byte stream InputStream input stream of bytes Byte output stream OutputStream
Character stream Character-input stream Reader Character-output stream Writer

Close and refresh

Because of the built-in buffer, if you do not close the output stream, can not write characters to a file. But the stream object is closed, we can not continue to write data. If we both want to write data, and want to continue to use the stream, we need to flush the method.

flush: flush the buffer stream object can continue to use.
close: first flush the buffer, then the notification system to release resources. Stream objects can no longer be used.

1. byte stream

1.1 output stream of bytes (the OutputStream)

java.io.OutputStream abstract class is a superclass of all output stream of bytes, the byte information is written to the specified destination. It defines the basic functionality common method output stream of bytes.

public void close() :关闭此输出流并释放与此流相关联的任何系统资源。
public void flush() :刷新此输出流并强制任何缓冲的输出字节被写出。
public void write(byte[] b) :将 b.length字节从指定的字节数组写入此输出流。
public void write(byte[] b, int off, int len) :off开始位置,len写入字节长度
public abstract void write(int b) :将指定的字节(ASCII码)输出流。
1.2 FileOutputStream类

OutputStream There are many sub-categories, we start with the simplest of a subclass.
java.io.FileOutputStream class file output stream for writing data to a file.

Construction method

public FileOutputStream(File file) :创建文件输出流以写入由指定的 File对象表示的文件。
public FileOutputStream(String name) : 创建文件输出流以指定的名称写入文件。
public FileOutputStream(File file, boolean append) : 创建文件输出流以写入由指定的File对象表示的文件。
public FileOutputStream(String name, boolean append) : 创建文件输出流以指定的名称写入文件。

Examples demo

// 使用File对象创建流对象
File file = new File("a.txt");
FileOutputStream fos = new FileOutputStream(file);
// 使用文件名称创建流对象
FileOutputStream fos = new FileOutputStream("b.txt");

The first two constructors, when you create a file output stream without this file creates files with the file will clear the file, the judge added two constructors "true" if additional data, "false" represents the empty original data.

Common method

写出字节: write(int b) 方法,每次可以写出一个字节数据,代码使用演示:

写出字节数组: write(byte[] b) ,每次可以写出数组中的数据,
写出指定长度字节数组: write(byte[] b, int off, int len) ,每次写出从off索引开始,len个字节
写出换行:Windows系统里,换行符号是\r\n。

Examples demo

// 使用文件名称创建流对象
FileOutputStream fos = new FileOutputStream("fos.txt");
// 写出数据
fos.write(97); // 写出第1个字节
fos.write(98); // 写出第2个字节
fos.write(99); // 写出第3个字节
// 关闭资源
fos.close();

输出结果:
abc

// 使用文件名称创建流对象
FileOutputStream fos = new FileOutputStream("fos.txt");
// 字符串转换为字节数组
byte[] b = "莫逸风".getBytes();
// 写出字节数组数据
fos.write(b);
// 关闭资源
fos.close();

输出结果:
莫逸风

// 使用文件名称创建流对象
FileOutputStream fos = new FileOutputStream("fos.txt");
// 字符串转换为字节数组
byte[] b = "abcde".getBytes();
// 写出从索引2开始,2个字节。索引2是c,两个字节,也就是cd。
fos.write(b,2,2);
// 关闭资源
fos.close();

输出结果:
cd

// 写出一个换行, 换行符号转成数组写出
fos.write("\r\n".getBytes());

Carriage return \ r and newline \ n-:
carriage return: return to the beginning of a line (return).
Newline: The next line (newline).
Wrap System:
Windows system, the end of each line is a carriage return + line, that \ r \ the n-;
Unix systems, the end of each line only wrap that \ the n-;
Mac system, at the end of each line is a carriage return, that is, \ r. Start with a unified Linux from Mac OS X.

1.3 input stream of bytes (the InputStream)

java.io.InputStream abstract class is a superclass of all the input stream of bytes, byte information can be read into memory. It defines the basic functionality common method of input stream of bytes.

public void close() :关闭此输入流并释放与此流相关联的任何系统资源。
public abstract int read() : 从输入流读取数据的下一个字节。
public int read(byte[] b) : 从输入流中读取一些字节数,并将它们存储到字节数组 b中 。
1.4 FileInputStream class

java.io.FileInputStream class file input stream of bytes read from the file.

Construction method

FileInputStream(File file) : 由指定的 File对象来创建一个 FileInputStream。
FileInputStream(String name) : 以指定的名称来创建一个 FileInputStream。

The file must exist when you create a stream object, if not the file, throws FileNotFoundException.

Examples demo

// 使用File对象创建流对象
File file = new File("a.txt");
FileInputStream fos = new FileInputStream(file);
// 使用文件名称创建流对象
FileInputStream fos = new FileInputStream("b.txt");

Common method

读取字节:int read 方法,每次可以读取一个字节的数据,提升为int类型,读取到文件末尾,返回-1 。
使用字节数组读取:int read(byte[] b) ,每次读取b的长度个字节到数组中,返回读取到的有效字节个数,读取到末尾时,返回-1 ,

Examples demo

// 使用文件名称创建流对象
FileInputStream fis = new FileInputStream("read.txt");
// 定义变量,保存数据
int b ;
// 循环读取
while ((b = fis.read())!=‐1) {
System.out.print((char)b);
}
// 关闭资源
fis.close();

输出结果:
abcde

// 使用文件名称创建流对象.
FileInputStream fis = new FileInputStream("read.txt"); // 文件中为abcde
// 定义变量,作为有效个数
int len ;
// 定义字节数组,作为装字节数据的容器
byte[] b = new byte[2];
// 循环读取
while (( len= fis.read(b))!=‐1) {
    // 每次读取后,把数组变成字符串打印
    System.out.println(new String(b));
}
// 关闭资源
fis.close();

输出结果:
ab
cd
ed(长度不够,最后一次字符数组只将第一个字符替换掉了,所以d还存在)

输出可改为
System.out.println(new String(b,0,len));// len 每次读取的有效字节个数

输出结果:
ab
cd
e

Reading using an array, each reading a plurality of bytes, reducing the number of IO operations between systems, thereby improving the efficiency of reading and writing, developers use.

2. The character stream

When using a byte stream to read a text file, there may be a small problem. Is the face of Chinese characters, may not show the full character, it is because a Chinese character may occupy multiple bytes of storage. So Java to provide some character stream classes to read and write data in units of characters, designed to handle text files.

2.1 character-input stream (Reader)

java.io.Writer abstract class is a superclass for all classes of characters to write the stream, the specified character information written to the destination. It defines the basic functionality common method output stream of bytes.

void write(int c) 写入单个字符。
void write(char[] cbuf) 写入字符数组。
abstract void write(char[] cbuf, int off, int len) 写入字符数组的某一部分,off数组的开始索引,len
写的字符个数。
void write(String str) 写入字符串。
void write(String str, int off, int len) 写入字符串的某一部分,off字符串的开始索引,len写的字符个
数。
void flush() 刷新该流的缓冲。
void close() 关闭此流,但要先刷新它。
2.2 FileReader class

java.io.FileReader class is a convenience class reading character files. The system default character encoding and the default byte buffer is configured.

Construction method

FileReader(File file) : 创建一个新的 FileReader ,给定要读取的File对象。
FileReader(String fileName) : 创建一个新的 FileReader ,给定要读取的文件的名称。

Examples demo

// 使用File对象创建流对象
File file = new File("a.txt");
FileReader fr = new FileReader(file);
// 使用文件名称创建流对象
FileReader fr = new FileReader("b.txt");

The main method

读取字符: read 方法,每次可以读取一个字符的数据,提升为int类型,读取到文件末尾,返回-1。
使用字符数组读取: read(char[] cbuf) ,每次读取b的长度个字符到数组中,返回读取到的有效字符个数,
读取到末尾时,返回-1 ,

Examples demo

// 使用文件名称创建流对象
FileReader fr = new FileReader("read.txt");
// 定义变量,保存数据
int b ;
// 循环读取
while ((b = fr.read())!=‐1) {
System.out.println((char)b);
}
// 关闭资源
fr.close();

输出结果:

// 使用文件名称创建流对象
FileReader fr = new FileReader("read.txt");
// 定义变量,保存有效字符个数
int len ;
// 定义字符数组,作为装字符数据的容器
char[] cbuf = new char[2];
// 循环读取
while ((len = fr.read(cbuf))!=‐1) {
System.out.println(new String(cbuf,0,len));
}
// 关闭资源
fr.close();

输出结果:
莫逸
2.3 the output character stream (Writer)

java.io.Writer abstract class is a superclass for all classes of characters to write the stream, the specified character information written to the destination. It defines the basic functionality common method output stream of bytes.

void write(int c) 写入单个字符。
void write(char[] cbuf) 写入字符数组。
abstract void write(char[] cbuf, int off, int len) 写入字符数组的某一部分,off数组的开始索引,len
写的字符个数。
void write(String str) 写入字符串。
void write(String str, int off, int len) 写入字符串的某一部分,off字符串的开始索引,len写的字符个
数。
void flush() 刷新该流的缓冲。
void close() 关闭此流,但要先刷新它。
2.4 FileWriter class

java.io.FileWriter class is to write characters to the file of the convenience class. The system default character encoding and the default byte buffer is configured.

Construction method

FileWriter(File file) : 创建一个新的 FileWriter,给定要读取的File对象。
FileWriter(String fileName) : 创建一个新的 FileWriter,给定要读取的文件的名称。

Examples demo

// 使用File对象创建流对象
File file = new File("a.txt");
FileWriter fw = new FileWriter(file);
// 使用文件名称创建流对象
FileWriter fw = new FileWriter("b.txt");

The main method

写出字符: write(int b) 方法,每次可以写出一个字符数据
写出字符数组 : write(char[] cbuf) 和 write(char[] cbuf, int off, int len) ,每次可以写出字符数组中的数据,用法类似FileOutputStream

Examples demo

// 使用文件名称创建流对象
FileWriter fw = new FileWriter("fw.txt");
// 写出数据
fw.write(97); // 写出第1个字符
fw.write('b'); // 写出第2个字符
fw.write('C'); // 写出第3个字符
fw.write(30000); // 写出第4个字符,中文编码表中30000对应一个汉字。
fw.close;

输出结果:
abC田

// 使用文件名称创建流对象
FileWriter fw = new FileWriter("fw.txt");
// 字符串
String msg = "莫逸风";
// 写出字符数组
fw.write(msg); //莫逸风
// 写出从索引1开始,2个字节。索引1是'逸',两个字节,也就是'逸风'。
fw.write(msg, 1, 2); // 逸风
// 关闭资源
fw.close();

输出结果:
莫逸风逸风

Guess you like

Origin www.cnblogs.com/zhangguangxiang/p/12065757.html