Java中的IO操作函数使用框架
Java中的IO操作函数使用框架
Java提供了IO操作函数使用框架,使得我们可以方便地进行读写文件、网络通信等操作。IO操作通常涉及到文件输入输出(File I/O)和字节流输入输出(Stream I/O)等。本文主要介绍Java中常用的IO操作函数及其使用方法。
一、File类与FileWriter、FileReader类
File类是Java中常用的文件类,用于表示一个文件或目录。FileWriter和FileReader类用于读写字符流。例如下面的代码可以创建一个名为test.txt的文件,并写入Hello World!:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
File file = new File("test.txt");
try {
FileWriter writer = new FileWriter(file);
writer.write("Hello World!");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileReader类用于读取文件,例如下面的代码将读取test.txt文件中的内容并打印出来:
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
File file = new File("test.txt");
try {
FileReader reader = new FileReader(file);
int data = reader.read();
while(data != -1) {
System.out.print((char)data);
data = reader.read();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
二、BufferedReader、BufferedWriter类
BufferedReader和BufferedWriter类用于读写字符流。它们可以一次读取或写入多个字符,从而提高了IO操作的效率。例如下面的代码将使用BufferedWriter类创建一个名为test.txt的文件,并向其写入Hello World!:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
File file = new File("test.txt");
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write("Hello World!");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
BufferedReader类用于读取文件,例如下面的代码将使用BufferedReader类读取test.txt文件中的内容:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
File file = new File("test.txt");
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = null;
while((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
三、InputStream、OutputStream类
InputStream和OutputStream类用于读写字节流。例如下面的代码将使用OutputStream类创建一个名为test.txt的文件,并向其写入Hello World!:
import java.io.FileOutputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("test.txt");
byte[] bytes = "Hello World!".getBytes();
fos.write(bytes);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
InputStream类用于读取文件,例如下面的代码将使用InputStream类读取test.txt文件中的内容:
import java.io.FileInputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("test.txt");
byte[] bytes = new byte[1024];
int len = fis.read(bytes);
fis.close();
System.out.println(new String(bytes, 0, len));
} catch (IOException e) {
e.printStackTrace();
}
}
}
四、ByteArrayOutputStream、ByteArrayInputStream类
ByteArrayOutputStream和ByteArrayInputStream类用于读写字节数组流。例如下面的代码使用ByteArrayOutputStream类将字符串转换为字节数组:
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
String s = "Hello World!";
byte[] bytes = s.getBytes();
bos.write(bytes);
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
ByteArrayInputStream类用于读取字节数组流,例如下面的代码将使用ByteArrayInputStream类读取上面的字节数组:
import java.io.ByteArrayInputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
try {
byte[] bytes = "Hello World!".getBytes();
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
byte[] buffer = new byte[1024];
int len = bis.read(buffer);
bis.close();
System.out.println(new String(buffer, 0, len));
} catch (IOException e) {
e.printStackTrace();
}
}
}
五、Socket类
Socket类用于实现网络通信。例如下面的代码将使用Socket类与百度服务器建立连接,并向其发送请求:
import java.io.*;
import java.net.*;
public class Test {
public static void main(String[] args) {
try {
Socket socket = new Socket("www.baidu.com", 80);
OutputStream os = socket.getOutputStream();
PrintWriter pw = new PrintWriter(os);
pw.println("GET / HTTP/1.1");
pw.println("Host: www.baidu.com");
pw.println("");
pw.flush();
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
while((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
is.close();
pw.close();
os.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
六、总结
Java中的IO操作函数使用框架提供了多种读写文件、网络通信等操作的方法,可以帮助我们快速完成IO操作。在使用IO操作时,我们需要根据具体需求选择合适的IO操作函数及其参数,并注意输入输出流的关闭。
