Java IO
Java IO
IO: Input And Output
java 中有IO,即输入与输出,输入input 输出output
java 有典型的IO与NIO,常说的IO指典型IO, 阻塞IO, NIO表示非阻塞IO
阻塞: 等待IO任务完成才能继续下一步的操作
非阻塞: 暂不能清晰描述,可以查看网址文章链接:http://ifeve.com/overview
这里简单介绍Java中常见的文件读写
字节流读写文件:
读文件
public static void readFile(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists() || !file.isFile()) {
System.out.println("file is not exist");
} else {
FileInputStream fileInputStream = new FileInputStream(file); //创建文件输入流
byte[] read = new byte[1024];
while (fileInputStream.read(read) != -1) {
System.out.println("read content:" + read);
}
fileInputStream.close(); // 关闭流 释放资源
}
}
写文件
public static void writeFile(byte[] content) throws IOException {
File file = new File("D:\\writeFile.xml"); //获取写文件的位置
if (!file.exists())
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(file); //创建文件输出流
fileOutputStream.write(content); // content 表示写入文件的内容
fileOutputStream.close(); //关闭流 释放资源
}
拷贝文件
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class TestIO {
public static void main(String[] args) throws IOException {
String originFilePath = "D:\\Program.md";
String aimFilePath = "D:\\aimFile.md";
copyFile(originFilePath, aimFilePath);
// testRandonAccessFile();
}
/*
* 读文件
*/
public static void readFile(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists() || !file.isFile()) {
System.out.println("file is not exist");
} else {
FileInputStream fileInputStream = new FileInputStream(file);
byte[] read = new byte[1024];
while (fileInputStream.read(read) != -1) {
System.out.println("read content:" + read);
}
fileInputStream.close();
}
}
/**
* 写文件
* @throws IOException
*/
public static void writeFile(byte[] content) throws IOException {
File file = new File("D:\\writeFile.xml");
if (!file.exists())
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(content);
fileOutputStream.close();
}
/*
* 文件复制 -> 读取一个文件复制到另一个地方
*/
public static void copyFile(String originFilePath, String aimPath) {
File originFile = new File(originFilePath);
if (!originFile.exists()) {
System.err.println("file is not exists!");
return; //文件不存在
}
File aimFile = new File(aimPath);
FileInputStream fi = null;
FileOutputStream fo = null;
try {
byte[] content = new byte[1024]; //每次读文件内容的字节数组
if (!aimFile.exists())
aimFile.createNewFile();
// 通过路径转化为 输入流 和 输出流
fi = new FileInputStream(originFile); //输入流
fo = new FileOutputStream(aimFile); //输出流
while (fi.read(content) != -1) { //输入流将数据读取到字节数组,如果返回值为 -1 表示文件已经读完
fo.write(content); //输出流将数组内容写入文件
}
} catch (IOException e) {
System.err.println("read file exception!");
try {
// close resources;
if (fo != null)
fo.close();
if (fi != null)
fi.close();
} catch (IOException es) {
// TODO Auto-generated catch block
es.printStackTrace();
}
} finally {
try {
if (fo != null)
fo.close();
if (fi != null)
fi.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*
* java 涉及NIO的 RandomAccessFile 类
*/
private static void testRandonAccessFile() throws IOException {
RandomAccessFile aFile = new RandomAccessFile("D:\\settings.xml", "rw");
FileChannel channel = aFile.getChannel();
File file = new File("D:\\creatFile.xml");
if (!file.getParentFile().exists())
file.getParentFile().mkdirs();
if (!file.isFile()) {
System.err.println("file is not exist!");
System.exit(0);
}
if (!file.exists())
file.createNewFile();
FileOutputStream newFile = new FileOutputStream(file);
ByteBuffer buffer = ByteBuffer.allocate(1024);
int i = 0;
while (channel.read(buffer) != -1) {
i++;
buffer.flip();
while (buffer.hasRemaining()) {
newFile.write(buffer.get());
}
buffer.clear();
}
aFile.close();
newFile.close();
System.out.println("文件读取次数:" + i);
}
}