java.io
包几乎包含了在java中执行输入和输出(i/o)所需的所有类。 所有这些流代表输入源和输出目的地。 java.io
包中的流支持许多数据,如:原始,对象,本地化字符等。
流(streams
)可以定义为数据序列,它有两种 -
inputstream
- 它用于从源读取数据。outputstream
- 它用于将数据写入目标。java为与文件和网络相关的i/o提供强大而灵活的支持,但本教程只涵盖了流和i/o相关的非常基本的功能。下面将看到一些最常用的例子 -
1.1. 字节流
java字节流用于执行8
位字节的输入和输出。尽管有许多与字节流相关的类,但最常用的类是fileinputstream
和fileoutputstream
。以下示例使用这两个类将输入文件的内容复制到输出文件中 -
import java.io.*;
public class copyfile {
public static void main(string args[]) throws ioexception {
fileinputstream in = null;
fileoutputstream out = null;
try {
in = new fileinputstream("d:\\input.txt");
out = new fileoutputstream("d:\\output.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
}finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
假设在d盘下有一个文件:input.txt
,它的内容如下 -
this is test for copy file.
power by h3.com
下一步,编译上面的程序并执行它,它将创建一个:d:/output.txt
文件,内容与d:/input.txt
中的相同。
1.2. 字符流
java字节流用于执行8
位字节的输入和输出,而java字符流用于执行16
位unicode的输入和输出。 尽管有许多与字符流相关的类,但最常用的类是filereader
和filewriter
。 虽然filereader
内部使用fileinputstream
类,而filewriter
内部使用fileoutputstream
类,但主要区别在于filereader
一次读取两个字节,而filewriter
一次写入两个字节。
可以重新编写上面的例子,它使用这两个类将输入文件(具有unicode字符)复制到输出文件中 -
import java.io.*;
public class copyfile {
public static void main(string args[]) throws ioexception {
filereader in = null;
filewriter out = null;
try {
in = new filereader("d:/input.txt");
out = new filewriter("d:/output.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
}finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
假设在d盘下有一个文件:input.txt
,它的内容如下 -
this is test for copy file.
power by h3.com
下一步,编译上面的程序并执行它,它将创建一个:d:/output.txt
文件,内容与d:/input.txt
中的相同。
所有编程语言都支持标准i/o,用户的程序可以从键盘输入,然后在计算机屏幕上产生输出。 如果您了解c或c++编程语言,那么应该了解三个标准流:stdin
,stdout
和stderr
。 同样,java提供以下三个标准流 -
system.in
。system.out
。system.err
。以下是一个简单的程序,它使用inputstreamreader
来读取标准输入流,直到用户键入:q
-
import java.io.*;
public class readconsole {
public static void main(string args[]) throws ioexception {
inputstreamreader cin = null;
try {
cin = new inputstreamreader(system.in);
system.out.println("enter characters, 'q' to quit>");
char c;
do {
c = (char) cin.read();
system.out.print(c);
} while(c != 'q');
}finally {
if (cin != null) {
cin.close();
}
}
}
}
将上面的代码保存在readconsole.java
文件中,并尝试编译并执行它,如下面的程序所示。 程序继续读取并输出用户输入的字符,直到按q
退出 -
$javac readconsole.java
$java readconsole
enter characters, 'q' to quit>
1
1
a
a
c
c
如前所述,流可以定义为数据序列。 inputstream
用于从源读取数据,outputstream
用于将数据写入目标。
以下是处理输入和输出流的类层次结构。
两个重要的流是:fileinputstream
和fileoutputstream
,将在本教程中讨论。
3.1. fileinputstream
此流用于从文件中读取数据。 可以使用关键字new
创建对象,并且有几种类型的构造函数可用。
以下构造函数将文件名作为字符串来创建输入流对象以读取文件 -
inputstream f = new fileinputstream("d:/java/hello.txt");
以下构造函数采用文件对象来创建输入流对象以读取文件。 首先,使用file()
方法创建一个文件对象,如下所示 -
file f = new file("d:/java/hello.txt");
inputstream f = new fileinputstream(f);
当创建了inputstream
对象,就可以使用一些辅助方法来读取流或在流上执行其他操作。
编号 | 方法 | 描述 |
---|---|---|
1 | public void close() throws ioexception{} |
此方法关闭文件输出流。 释放与该文件关联的所有系统资源,抛出ioexception 。 |
2 | protected void finalize()throws ioexception {} |
此方法清除与文件的连接。 确保在没有对此流的引用时调用此文件输出流的close() 方法,抛出ioexception 。 |
3 | public int read(int r)throws ioexception{} |
此方法从inputstream 读取指定的数据字节,并返回一个int 值。 返回数据的下一个字节,如果它是文件的末尾,则返回-1 。 |
4 | public int read(byte[] r) throws ioexception{} |
此方法将输入流中的r.length 个字节读入数组。返回读取的总字节数。 如果它到达文件的结尾,则返回-1 。 |
5 | public int available() throws ioexception{} |
给出可以从此文件输入流中读取的字节数。 返回一个int 值。 |
还有其他重要的输入流可用,有关更多详细信息,请参阅以下链接 -
3.2. fileoutputstream
fileoutputstream
用于创建文件并将数据写入文件。 如果文件尚不存在,则会在打开文件以进行输出之前创建该文件。
这里有两个构造函数,可用于创建fileoutputstream
对象。
以下构造函数将文件名作为字符串来创建输入流对象以写入文件 -
outputstream f = new fileoutputstream("d:/java/hello.txt")
下面的构造函数接受一个文件对象来创建一个输出流对象来写入该文件。 首先,使用file()
方法创建一个文件对象,如下所示 -
file f = new file("d:/java/hello.txt");
outputstream f = new fileoutputstream(f);
当创建了outputstream
对象,就使用它的一些辅助方法来写入流或在流上执行其他操作。
编号 | 方法 | 描述 |
---|---|---|
1 | public void close() throws ioexception{} |
此方法关闭文件输出流,释放与该文件关联的所有系统资源。抛出ioexception 。 |
2 | protected void finalize()throws ioexception {} |
此方法清除与文件的连接,确保在没有对此流的引用时调用此文件输出流的close() 方法。抛出ioexception 。 |
3 | public void write(int w)throws ioexception{} |
此方法将指定的字节写入输出流。 |
4 | public void write(byte[] w) |
将长度为w.length 的字节从字节数组写入outputstream 。 |
还有其他重要的输出流,有关更多详细信息,请参阅以下链接 -
示例
以下是演示如何使用inputstream
和outputstream
类对象的示例 -
import java.io.*;
public class filestreamtest {
public static void main(string args[]) {
try {
byte bwrite [] = {11,21,3,40,5};
outputstream os = new fileoutputstream("d:/test.txt");
for(int x = 0; x < bwrite.length ; x++) {
os.write( bwrite[x] ); // writes the bytes
}
os.close();
inputstream is = new fileinputstream("d:/test.txt");
int size = is.available();
for(int i = 0; i < size; i++) {
system.out.print((char)is.read() + " ");
}
is.close();
} catch (ioexception e) {
system.out.print("exception");
}
}
}
上面的代码将创建文件test.txt
并将以二进制格式写入给定的数字数据,同样也会在屏幕上输出。
可通过其他几个类来了解文件导航和i/o的基础知识。如下 -
目录是一个文件,它可以包含其他文件和目录的列表。 使用file
对象创建目录,列出目录中可用的文件。 有关完整的详细信息,请查看在file对象上调用的所有方法的列表以及与目录相关的内容。
4.1.1. 创建目录
有两种file
类的方法,可用于创建目录 -
mkdir()
方法创建一个目录,创建成功时返回true
,失败时返回false
。 失败表示file
对象中指定的路径已存在,或者由于整个路径尚不存在或权限问题而无法创建目录。mkdirs()
方法创建目录和目录的所有上级目录。以下示例创建一个目录:d:/tmp/user/java/bin -
示例
import java.io.file;
public class createdir {
public static void main(string args[]) {
string dirname = "d:/tmp/user/java/bin";
file d = new file(dirname);
// 创建目录及父级目录
d.mkdirs();
}
}
编译并执行上面的代码来创建目录:d:/tmp/user/java/bin。
注 - java会根据约定自动处理unix和windows上的路径分隔符。如果在windows版本的java上使用正斜杠(/),则路径仍将正确解析。
4.1.2. 列出目录
可以使用file
对象的list()
方法列出目录中可用的所有文件和目录,如下所示 -
import java.io.file;
public class readdir {
public static void main(string[] args) {
file file = null;
string[] paths;
try {
// 创建一个file对象
file = new file("d:/software");
// 文件和目录的数组
paths = file.list();
// 对于路径数组中的名称
for(string path:paths) {
// 打印文件名和目录名
system.out.println(path);
}
} catch (exception e) {
// if any error occurs
e.printstacktrace();
}
}
}
执行上面代码,它将根据d:/software
目录中目录和文件产生以下结果 -
apache-maven-3.5.4
apache-tomcat-9.0.14
aptana_studio
eclipse
editplusportable
javajars
kafka_2.11-2.0.0
mysql-5.7.23-winx64
navicat premium 11
php-cs-fixer.phar
spring-2.0.5.release
... ...