一 文件创建的三种方式
/**
* 文件创建的三种方法
*/
//方式1 new File(String pathname)
// 根据路径构建一个File对象
@Test
public void creat01() {
String filepath1 = "e:\\news1.txt";
File file1 = new File(filepath1); //创建文件对象
try {
file1.createNewFile(); //真正地创建文件
System.out.println("文件1创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
//方式2 new File(File parent, String child)
//根据父目录文件+子路径构建
@Test
public void creat02() {
String filepath2 = "e:\\";
File parentFile = new File(filepath2);
String child2 = "news2.txt";
File file2 = new File(parentFile, child2);
try {
file2.createNewFile();
System.out.println("文件2创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
//方式3 new File(String parent, String child)
//根据父目录+子路径构建
@Test
public void creat03() {
String filepath3 = "e:\\"; //建议这样写
// String filepath3 = "e:/"; //也可这样写
String child3 = "news3.txt";
File file3 = new File(filepath3, child3);
try {
file3.createNewFile();
System.out.println("文件3创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
二 文件输入流
/**
* 使用FileInputStream将文件数据读取到程序中的两种方式
* 注意:字节流(8bit)
*/
@Test
public void readFile01() { //方法一 使用read()读取
int readData = 0;
//读取文件路径
String filePath = "e:\\FileInputStreamTest.txt";
//1 创建 fileInputStream 对象,用于读取文件
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(filePath);
//2 fileInputStream.read() 读取文件
// read()是一个字节一个字节地读,如果返回-1,表示读取完毕
while((readData = fileInputStream.read()) != -1) {
System.out.print((char)readData); //转成char显示
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭文件流,释放资源
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void readFile02() { //方法二 使用read(byte[] b)读取
//读取文件路径
String filePath = "e:\\FileInputStreamTest.txt";
byte[] readData2 = new byte[8]; //设置为一次读取8个字节
int readLen = 0; //每次实际读取的字节数组长度,因为有时候可能一次读不满8个字节
//1 创建 fileInputStream 对象,用于读取文件
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(filePath);
//2 fileInputStream.read(byte[] b) 读取文件
// read(byte[] b)是读取最多到b.length长度的字节数组
// 如果返回-1,表示读取完毕
while((readLen = fileInputStream.read(readData2)) != -1) {
//因为此处读取的是char数组,所以转成String显示
//readLen为每次实际读取的char数组长度
//此处读取的hello,world!,第一次readLen为8,第二次readLen为4
//第三次readLen为-1,程序结束
System.out.print(new String(readData2, 0, readLen));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭文件流,释放资源
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
三 文件输出流
/**
* 使用FileOutputStream 将数据写到文件中
* 如果该文件不存在,则创建文件
* 注意:字节流(8bit)
*/
@Test
public void writeFile() {
//1 创建 FileOutputStream 对象
String filePath = "e:\\FileOutputStreamTest.txt";
FileOutputStream fileOutputStream = null;
try {
//2 得到 FileOutputStream 对象
//注意:new FileOutputStream(filePath)创建方式,写入的内容会覆盖掉原来的内容
//若使用new FileOutputStream(filePath, true)创建方式,则写入内容会加到文件后面
fileOutputStream = new FileOutputStream(filePath);
//3.1 写入一个字节 write(int b)
fileOutputStream.write('H'); //char -> int
//3.2 写入字符串 write(byte[] b)
String str = "hello,world!";
//str.getBytes() 可以把字符串转成->字节数组
fileOutputStream.write(str.getBytes());
//3.3 写入字符串的指定范围字符
// write(byte[] b, int off, int len)
// 从索引off处开始写进去len个数据
fileOutputStream.write(str.getBytes(), 0, 3);
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭文件流,释放资源
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
四 文件拷贝
/**
* 完成文件拷贝
* 1 创建文件的输入流,将文件读入到程序中
* 2 创建文件的输出流,将程序中的数据写入到指定的文件中
* 注意:在完成程序时,应该是读取部分数据就写入到指定文件(循环)
*/
public class FileCopy {
public static void main(String[] args) {
String srcFilePath = "e:\\jay.jpg";
String desFilePath = "f:\\jay.jpg";
fileCopy(srcFilePath, desFilePath);
}
public static void fileCopy(String srcFilePath, String desFilePath) {
byte[] buf = new byte[1024];
int readLen = 0;
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream(srcFilePath);
fileOutputStream = new FileOutputStream(desFilePath);
while((readLen = fileInputStream.read(buf)) != -1) {
fileOutputStream.write(buf, 0, readLen); //边读边写
}
System.out.println("拷贝成功!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fileInputStream != null) {
fileInputStream.close();
}
if(fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}