IO操作
File

构造方法
public File(String pathname);//主要在Java EE的开发之中public File(File parent, String child);//主要在Android开发之中
获取文件的基本信息
除了以上的常用的方法之外,在File类之中还可以通过以下的方法取得一些文件的基本信息:
- public String getName();取得文件的名称:
- public boolean isDirectory();给定的路径是否是文件夹:
- public boolean isFile();给定的路径是否是文件:
- public boolean isHidden();是否是隐藏文件:
- public long lastModified();文件的最后一次修改日期:
- public long length();取得文件大小:
- public boolean mkdir();创建一级目录
- public boolean mkdirs();创建多级目录
字节流与字符流
(1)字节操作流:OutputStream、InputStream;
(2)字符操作流:Writer、Reader。
字节流
不管是写入还是读取,都是对byte()进行操作
OutputStream和InputStream是字节流的两个顶层父类。让他们提供了输出流类和输入流类通用API,字节流一般用于读写二进制数据
OutputStream output = new FileOutputStream(file, true);对原有数据不覆盖
OutputStream output = new FileOutputStream(file);会对原有数据覆盖
- 写入单个字节数据:public abstract void write(int b) throws IOException;
- 写入一组字节数据:public void write(byte[] b) throws IOException;
- 写入部分字节数据:public void write(byte[] b, int off, int len) throws IOException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class TestDemo {
public static void main(String[] args) throws Exception {
//第一步:定义文件路径
File file = new File("D:"+File.separator + "demo"+ File.separator + "test.txt");
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
//第二步:实例化输出流
OutputStream output = new FileOutputStream(file);
String data = "hello world !\r\nhello world !\r\nhello world !\r\nhello world !";
// 第三步:输出数据,要将数据变为字节数组输出
output.write(data.getBytes());
//第四步:关闭资源
output.close();
}
}
InputStream input = new FileInputStream(file);
(1)读取单个字节:
public abstract int read() throws IOException;
注意:每次执行read()方法都会读取一个数据源的指定数据,如果现在发现已经读取到了结尾返回-1;
(2)将读取的数据保存到字节数组中:
public int read(byte[] b) throws IOException;
注意:如果现在要读取的数据小于byte的数据,这个时候read()方法的返回值int返回的是数据个数,如果数据已经读完了,则这个时候的int返回的是-1;
(3)将读取的数据保存在部分字节数组中:
public int read(byte[] b, int off, int len) throws IOException;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
public class TestDemo {
public static void main(String[] args) throws Exception {
//第一步:定义文件路径
File file = new File("D:" + File.separator + "demo" + File.separator + "test.txt"); // 定义文件路径
if (file.exists()) { // 文件存在则可以读取
//第二步:实例化输入流
InputStream input = new FileInputStream(file);
//第三步:读取数据到字节数组
byte data[] = new byte[1024]; // 假设要读的长度是1024
int len = input.read(data); // 读取数据,返回读取个数
//第四步:关闭资源
input.close();
System.out.println("读取的数据是:【" + new String(data, 0, len) + "】");
}
}
}
字符流
字符输出流:Writer
写入字符串不需要将字符串转换为字节形式
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
public class TestDemo {
public static void main(String[] args) throws Exception {
File file = new File("D:" + File.separator + "demo" + File.separator + "test.txt"); // 定义文件路径
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();// 创建父目录
}
Writer out = new FileWriter(file);
String data = "Hello World .";
out.write(data); // 直接写入字符串
out.close(); // 关闭
}
}
字符输入流:Reader
读取方法:public int read (char[] cbuf) throws IOException;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
public class TestDemo {
public static void main(String[] args) throws Exception {
File file = new File("D:" + File.separator + "demo" + File.separator + "test.txt"); // 定义文件路径
if (file.exists()) {
Reader in = new FileReader(file); // 字符输入流
char data[] = new char[1024]; // 开辟数组
int len = in.read(data); // 读取数据
in.close();
System.out.println("读取数据内容:【" + new String(data, 0, len) + "】");
}
}
}
完成一次文件复制
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class TestDemo {
public static void main(String[] args) throws Exception {
File inFile = new File("D:" + File.separator + "demo"
+ File.separator + "test.zip"); // 定义文件路径
File outFile = new File("D:" + File.separator + "demo"
+ File.separator + "test2.zip"); // 定义文件路径
long start = System.currentTimeMillis();
if (!inFile.exists()) { // 源文件不存在
System.out.println("源文件不存在!");
System.exit(1); // 程序退出
}
if(!outFile.getParentFile().exists()){
outFile.getParentFile().mkdirs();
}
InputStream input = new FileInputStream(inFile);
OutputStream output = new FileOutputStream(outFile);
int temp = 0;//保存每次读取的个数
byte data[] = new byte[4096]; // 每次读取4096字节
while ((temp = input.read(data)) != -1) { // 将每次读取进来的数据保存在字节数组里,并返回读取的个数
output.write(data, 0, temp); // 输出数组
}
long end = System.currentTimeMillis();
System.out.println("拷贝完成,所花费的时间:" + (end - start) + "毫秒");
input.close();
output.close();
}
}
.

浙公网安备 33010602011771号