一、文件
1. 文件就是保存数据的地方。
2. 文件流: 文件在程序中是以流的形式来操作的。
3. 常用的文件操作:
4. 创建文件:3种方式
package com.File;/*
**@verson 1.0
*/
import org.junit.Test;
import java.io.File;
import java.io.IOException;
public class File_ {
public static void main(String[] args) {
}
//方式1. new File(String pathname)
@Test
public void create01(){
String filePath="D:\\news1.txt";
File file=new File(filePath);
try {
file.createNewFile();
System.out.println("文件创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
//2.new File(File pareant, String child)//根据父目录文件+子路径构建
public void create02(){
File parentFile = new File("D:\\");
String fileName="news2.txt";
//这里只是一个java对象
File file=new File(parentFile,fileName);
try {
//真正的在磁盘创建
file.createNewFile();
System.out.println("创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
//3.new File(File pareant, String child)//根据父目录文件+子路径构建
public void create03(){
String parentPath="D:\\";
String fileName="News3.txt";
File file=new File(parentPath,fileName);
try {
file.createNewFile();
System.out.println("创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
}
5. 获取文件信息:
6. 目录的操作和文件删除:
@Test
public void m1(){
String filePath="D:\\news1.txt";
File file = new File(filePath);
if (file.exists()){
if (file.delete()){
System.out.println(filePath+"删除成功");
}else {
System.out.println(filePath+"删除失败");
}
}else {
System.out.println("改文件不存在");
}
}
D:\news1.txt删除成功
@Test
public void m3(){
String filePath="D:\\demo\\a\\b\\c";
File file = new File(filePath);
if (file.exists()){
System.out.println(filePath+"存在");
}else {
if (file.mkdirs()){
System.out.println(filePath+"创建成功");
}else {
System.out.println(filePath+"创建失败");
}
}
二、
1. IO流原理及流的分类:
2. IO流体系图-常用的类
3. FileInputStream:
4. FileOutputStream:
5. 文件拷贝:
String srcFilePath="C:\\Users\\wxj\\Pictures\\Camera Roll\\1.jpg";
String destFilePath="D:\\1.jpg";
FileInputStream fileInputStream=null;
FileOutputStream fileOutputStream=null;
try {
fileInputStream=new FileInputStream(srcFilePath);
fileOutputStream=new FileOutputStream(destFilePath);
//定义字节数组
byte [] buf=new byte[1024];
int readLen=0;
while ((readLen=fileInputStream.read(buf))!= -1){
fileOutputStream.write(buf,0,readLen);
}
System.out.println("拷贝ok");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream !=null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream !=null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
"拷贝ok
三、
1. FileReader 和 FileWriter
2. FileReader :
3. FileWriter:
4.节点流和处理流:
5.处理流设计模式:
6. BufferedReader;BufferedWriter;Buffered拷贝。