这篇文章主要介绍“Java的IO流知识点有哪些”,在日常操作中,相信很多人在Java的IO流知识点有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Java的IO流知识点有哪些”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
分类一:按操作方式(类结构)
字节流和字符流:
输出流和输入流:
输出流:从内存读出到文件。只能进行写操作。
输入流:从文件读入到内存。只能进行读操作。
注意:这里的出和入,都是相对于系统内存而言的。
为什么要有处理流?直接使用节点流,读写不方便,为了更快的读写文件,才有了处理流。
按操作方式分类结构图:
根据以上分类,以及jdk的说明,我们可以画出更详细的类结构图,如下:

分类说明
字节流的输入和输出对照图:

字符流的输入和输出对照图:

其是字符流和字节流之间的桥梁;
可对读取到的字节数据经过指定编码转换成字符;
可对读取到的字符数据经过指定编码转换成字节;
当字节和字符之间有转换动作时;
流操作的数据需要编码或解码时。
InputStreamReader:输入流转到读流;
String fileName= "d:"+File.separator+"hello.txt";
File file=new File(fileName);
Writer out=new OutputStreamWriter(new FileOutputStream(file));
out.write("hello");
out.close();
OutputStreamWriter:输出流转到写流;
String fileName= "d:"+File.separator+"hello.txt";
File file=new File(fileName);
Reader read=new InputStreamReader(new FileInputStream(file));
char[] b=new char[100];
int len=read.read(b);
System.out.println(new String(b,0,len));
read.close();
这两个流对象是字符体系中的成员,它们有转换作用,本身又是字符流,所以在构造的时候需要传入字节流对象进来。
分类二:按操作对象
按操作对象分类结构图:

分类说明:
对文件进行操作(节点流):
FileInputStream(字节输入流),
FileOutputStream(字节输出流),
FileReader(字符输入流),
FileWriter(字符输出流)
对管道进行操作(节点流):
字节/字符数组流(节点流):
除了上述三种是节点流,其他都是处理流,需要跟节点流配合使用。
典型使用案例
/**
* 复制文件:一边读,一边写
*/
class hello {
public static void main(String[] args) throws IOException {
if (args.length != 2) {
System.out.println("命令行参数输入有误,请检查");
System.exit(1);
}
File file1 = new File(args[0]);
File file2 = new File(args[1]);
if (!file1.exists()) {
System.out.println("被复制的文件不存在");
System.exit(1);
}
InputStream input = new FileInputStream(file1);
OutputStream output = new FileOutputStream(file2);
if ((input != null) && (output != null)) {
int temp = 0;
while ((temp = input.read()) != (-1)) {
output.write(temp);
}
}
input.close();
output.close();
}
}
说明:
流在使用结束后,一定要执行关闭操作,即调用close( )方法。
FileInputStream.read():
这个方法是对这个流一个一个字节的读,返回的结果就是这个字节的int表示方式;
当已经没有内容时,返回的结果为-1;
FileOutputStream.write():
将内容写到文件。
2. 不使用FIle,将流中的字符转换大写小:
public static void main(String[] args) throws IOException {
String str = "ROLLENHOLT";
ByteArrayInputStream input = new ByteArrayInputStream(str.getBytes());
ByteArrayOutputStream output = new ByteArrayOutputStream();
int temp = 0;
while ((temp = input.read()) != -1) {
char ch = (char) temp;
output.write(Character.toLowerCase(ch));
}
String outStr = output.toString();
input.close();
output.close();
System.out.println(outStr);
}
/**
* 消息发送类
* */
class Send implements Runnable {
private PipedOutputStream out = null;
public Send() {
out = new PipedOutputStream();
}
public PipedOutputStream getOut() {
return this.out;
}
public void run() {
String message = "hello , Rollen";
try {
out.write(message.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
try {
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 接受消息类
*/
class Recive implements Runnable {
private PipedInputStream input = null;
public Recive() {
this.input = new PipedInputStream();
}
public PipedInputStream getInput() {
return this.input;
}
public void run() {
byte[] b = new byte[1000];
int len = 0;
try {
len = this.input.read(b);
} catch (Exception e) {
e.printStackTrace();
}
try {
input.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("接受的内容为 " + (new String(b, 0, len)));
}
}
/**
* 测试类
*/
class hello {
public static void main(String[] args) throws IOException {
Send send = new Send();
Recive recive = new Recive();
try {
//管道连接
send.getOut().connect(recive.getInput());
} catch (Exception e) {
e.printStackTrace();
}
new Thread(send).start();
new Thread(recive).start();
}
}
public static void main(String[] args) throws IOException {
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in));
String str = null;
System.out.println("请输入内容");
try{
str = buf.readLine();
}catch(IOException e){
e.printStackTrace();
}
System.out.println("你输入的内容是:" + str);
}
public static void main(String[] args) throws IOException {
File file = new File("/Users/liuluming/Documents/hello.txt");
// 此刻直接输出到屏幕
System.out.println("hello");
try {
System.setOut(new PrintStream(new FileOutputStream(file)));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println("这些内容在文件中才能看到哦!");
}
其他类:File
File类是对文件系统中文件以及文件夹进行封装的对象,可以通过对象的思想来操作文件和文件夹。 File类保存文件或目录的各种元数据信息,包括文件名、文件长度、最后修改时间、是否可读、获取当前文件的路径名,判断指定文件是否存在、获得当前目录中的文件列表,创建、删除文件和目录等方法。
其他类:RandomAccessFile
该对象并不是流体系中的一员,其封装了字节流,同时还封装了一个缓冲区(字符数组),通过内部的指针来操作字符数组中的数据。 该对象特点:
该对象只能操作文件,所以构造函数接收两种类型的参数:a.字符串文件路径;b.File对象。
该对象既可以对文件进行读操作,也能进行写操作,在进行对象实例化时可指定操作模式(r,rw)。
注意:
该对象在实例化时,如果要操作的文件不存在,会自动创建;如果文件存在,写数据未指定位置,会从头开始写,即覆盖原有的内容。 可以用于多线程下载或多个线程同时写数据到文件
到此,关于“Java的IO流知识点有哪些”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注天达云网站,小编会继续努力为大家带来更多实用的文章!