文件就是保存数据的地方。文件在程序中是以流的形式来操作的。
输入流:数据从数据库(文件)到程序(内存)的路径。
输出流:数据从程序(内存)到数据库(文件)的路径。
new File(String pathname)根据路径创建一个File对象
new File(File parent,String child)根据父目录文件+子路径创建
new File(String parent,String child)根据父目录+子路径创建
创建新文件 createNewFile
import org.junit.Test;
import java.io.File;
import java.io.IOException;
public class FileCreate_01 {
public static void main(String[] args) {
}
//创建文件 方式1 以下的test为单元测试 输入@Test 然后快捷键alt+Enter 可单独运行,查看结果
@Test
public void create01(){
String filepath = "e:\news1.txt";
File file = new File(filepath);
try {
file.createNewFile();//有异常就catch
System.out.println("文件创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
//方式二
@Test
public void create02(){
File parentfile = new File("e:\");
String filename = "news2.txt";
File file = new File(parentfile, filename);
try {
file.createNewFile();
System.out.println("文件创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
//方式3
@Test
public void create03(){
String parent = "e:\";
String child = "news3.txt";
File file = new File(parent, child);
try {
file.createNewFile();
System.out.println("文件创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
}
获取到文件相关信息
import org.junit.Test;
import java.io.File;
public class FileInfo {
public static void main(String[] args) {
}
//获取文件信息
@Test
public void info(){
File file = new File("e:\news1.txt");
//调用相应方法得到对应信息
System.out.println(file.getName());//得到文件名字
System.out.println(file.getAbsolutePath());//得到绝对路径
System.out.println(file.getParent());//得到父级目录
System.out.println(file.length());//文件的大小,以字节来统计
}
}
news1.txt e:news1.txt e: 0 Process finished with exit code 0目录操作
mkdir创建一级目录、mkdirs创建多级目录、delete删除空目录或文件
mkdirs也是返回一个bool值
import org.junit.Test;
import java.io.File;
public class Directory_ {
public static void main(String[] args) {
}
@Test
public void f1(){
//判断e盘是否存在news3.txt,如果有就删除
String filepath = "e:\news3.txt";
File file = new File(filepath);
if (file.exists()){//判断文件是否存在
if (file.delete()){//delete方法返回一个bool值,true则删除成功 false则为删除失败
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
}else {
System.out.println("文件不存在");
}
}
}
删除成功 Process finished with exit code 0I/O流
在Java中,对数据的输入输出是按照流的方式进行的。
java.io包中提供了各种“流”的类、接口。
流的分类
1、按照操作数据单位不同,分为字节流(8 bit)、字符流(按字符)。
2、按照数据流的流向不同分为输入流和输出流
3、按照流的角色不同分为节点流、处理流/包装流
字节流分为字节输入流InputStream和字节输出流OutputStream
字符流分为字符输入流Reader和字符输出流Writer。
这4个都是抽象类
如果是文本文件,最好使用字符流。如果是二进制文件,最好使用字节流。
流是传输媒介,就是用来传输数据的。
1、FileInputStream文件输入流
2、BufferedInputStream缓冲字节输入流
3、ObjectInputStream对象字节输入流
使用FileInputStream读取news1.txt文件,并将文件内容显示到控制台。
import org.junit.Test;
import java.io.IOException;
public class FileInputStream {
public static void main(String[] args) {
}
@Test
public void readFile01() {
String filepath = "e:\news1.txt";
int read = 0;
java.io.FileInputStream fileInputStream = null;//由于后面finally中也要使用,为了扩大作用域
try {
fileInputStream = new java.io.FileInputStream(filepath);
//如果返回-1,则表示文件已经读完了
while ((read = fileInputStream.read()) != -1) {
System.out.print((char) read);//转化成char显示
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭文件流,释放资源
try {
fileInputStream.close();//又有异常 try catch一边就可以
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
hello world Process finished with exit code 0FileOutputStream
使用FileOutputStream在a.txt文件中写入“hello,world”。如果文件不存在,则创建文件。
import org.junit.Test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class FileOutputStream_01 {
public static void main(String[] args) {
}
@Test
public void writeFile(){
String filepath = "e:\news2.txt";
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(filepath);//使用该方法,则是覆盖的方式。如果在filepath后面加上true,则是追加到文件最后面。
fileOutputStream.write('a');//写入一个字符
String str = "hello,world";
fileOutputStream.write(str.getBytes(StandardCharsets.UTF_8));//里面还可以支持写入一个字节数组,常用的是字符串的getBytes方法,将字符串转化成数组
fileOutputStream.write(str.getBytes(StandardCharsets.UTF_8),1,5);//从索引为1的开始,长度为5
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
案例:文件的拷贝
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopy {
public static void main(String[] args) {
}
@Test
public void fileCopy(){
String filepath1 = "e:\news1.txt";
String filepath2 = "e:\news2.txt";
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
int read = 0;
try {
fileInputStream = new FileInputStream(filepath1);
fileOutputStream = new FileOutputStream(filepath2);
while ((read = fileInputStream.read()) != -1) {
System.out.print((char) read);//转化成char显示
fileOutputStream.write((char)read);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}



