目录
文件与文件流理解
文件:保存数据的地方
文件流:文件在程序中是以流的形式来操作
创建文件常用的三种方式
方式1 new File(String pathname)//根据路径创建
方式2 new File(File parent,String child) //根据父目录文件+子路径构建
方式3:new File(String parent,String child) //根据父目录+子路径构建
package iofuxi;
import java.io.File;
import java.io.IOException;
public class FileCreate {
public static void main(String[] args) {
//方式1 new File(String pathname)
String filePath = "d:\\news1.txt";
//这里的File 在java程序中只是一个对象
File file = new File(filePath);
try {
file.createNewFile();//将文件真正的创建到硬盘上
System.out.println("文件创建成功");
} catch (IOException e) {
e.printStackTrace();
}
//方式2 new File(File parent,String child) //根据父目录文件+子路径构建
File parentFile = new File("d:\\");
String fileName = "news2.txt";
File file1 = new File(parentFile,fileName);
try {
file1.createNewFile();
System.out.println("文件创建成功");
} catch (IOException e) {
e.printStackTrace();
}
//方式3:new File(String parent,String child) //根据父目录+子路径构建
// String parentPath = "d:\\";
String parentPath = "d:/";//
String fileName1 = "news3.txt";
File file2 = new File(parentPath,fileName1);
try {
file2.createNewFile();
System.out.println("创建文件成功");
} catch (IOException e) {
e.printStackTrace();
}
}
}
File构造方法:
获取文件信息:
File 相关方法
package iofuxi;
import java.io.File;
public class FileInformation {
public static void main(String[] args) {
//获取文件的信息
//getName、getAnsolutePath 绝对路径、getParent、length、exists、isFile、isDirectory
File file = new File("d:\\news1.txt");
System.out.println("文件名字= "+ file.getName());
System.out.println("文件父级目录= "+file.getParent());
System.out.println("文件大小(字节)= "+file.length());
System.out.println("文件是否存在 :"+file.exists());
System.out.println("是不是一个文件:"+file.isFile());
System.out.println("是不是一个目录:"+file.isDirectory());
}
}
目录的操作和文件删除:
1.删除文件
package iofuxi;
import java.io.File;
public class Directory {
public static void main(String[] args) {
//判断 d:\\news1.txt 是否存在,如果存在就删除
String filePath = "d:\\news1.txt";
File file = new File(filePath);
if (file.exists()){
if (file.delete()){//返回值为boolean
System.out.println(filePath+"删除成功");
}else {
System.out.println(filePath+"删除失败");
}
}else {
System.out.println("该文件不存在");
}
}
}
2.删除目录
public static void main(String[] args) {
//判断 D:\\demo02是否存在,如果存在就删除
//在java编程中,目录也被当做文件
String filePath = "D:\\demo02";
File file = new File(filePath);
if (file.exists()){ //目录里为空才能删除,否则删除失败
if (file.delete()){//返回值为boolean
System.out.println(filePath+"删除成功");
}else {
System.out.println(filePath+"删除失败");
}
}else {
System.out.println("该文件不存在");
}
}
3.创建多级目录
public static void main(String[] args) {
//判断 D:\\demo\\a\\b\\c是否存在,如果存在就删除
//在java编程中,目录也被当做文件
String directoryPath = "D:\\demo\\a\\b\\c";
File file = new File(directoryPath);
if (file.exists()){
System.out.println(directoryPath+"存在");
}else {//不存在,创建多级目录
if (file.mkdirs()){//mkdirs() 创建多级目录 mkdir()是创建单级目录
System.out.println(directoryPath+"创建成功");
}else {
System.out.println(directoryPath+"创建失败");
}
}
}
IO流原理及流的分类
原理:
1.I/O Input/Output 的缩写,用于处理数据传输,如读写文件,网络通讯等。
2.java程序中,对于数据的输入/输出操作以“流(stream)”的方式进行。
3.java.io包下提供的各种的“流”类和接口,用以获取不同种类的数据,并通过方法输入或输出数据。
分类:
- 按操作数据单位不同分为: 字节流(8 bit)二进制文件,字符流(按字符) 文本文件;
- 按数据流的流向不同分为: 输入流,输出流;
- 按流的角色的不同分为: 节点流,处理流/包装流。
InputStream、 OutputStream 、Reader、Writer都是抽象类;
IO流体系图- 常用的类
InputStream:字节输入流
InputStream抽象类是所有类字节输入流的超类
InputStream 常用的子类:
- 1. FilelnputStream: 文件输入流
- 2.BufferedInputStream: 缓冲字节输入流--直接父类是FilterInputStream
- 3.ObjectInputStream: 对象字节输入流
FilelnputStream字节输入流
1、单个字节读取文件,效率低
package iofuxi;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* 读取文件
* 单个字节的读取,效率比较低
*/
public class FileInputStream_{
public static void main(String[] args) {
String filePath = "d:\\hello.txt";
int readData = 0;
FileInputStream fileInputStream = null;
try {
//创建 FileInputStream 对象,用于读取文件
fileInputStream = new FileInputStream(filePath);
//从该输入流读取一个字节的数据,如果没有输入可用,此方法将阻止
//返回-1表示读取完毕
while (true){
try {
if (!((readData = fileInputStream.read())!=-1)) break;
} catch (IOException e) {
e.printStackTrace();
}
System.out.print((char)readData);//如果文件中存有汉字,一个汉字三个字节,将会乱码
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2.读取字节数组,效率较高
package iofuxi;
import java.io.FileInputStream;
import java.io.IOException;
/**
* 读取文件
* 字节数组的读取,效率比较高
*/
public class FileInputStream_{
public static void main(String[] args) {
String filePath = "d:\\hello.txt";
byte[] buf = new byte[1024];//一次读取1024个字节
int readLen = 0;
FileInputStream fileInputStream = null;
try {
//创建 FileInputStream 对象,用于读取文件
fileInputStream = new FileInputStream(filePath);
//从该输入流最多读取b.length字节的数组,如果没有输入可用,此方法将阻止
//返回-1表示读取完毕
//如果读取正常,返回实际读取的字节数
while ((readLen = fileInputStream.read(buf))!=-1){
System.out.print(new String(buf,0,readLen));//如果文件中存有汉字,一个汉字三个字节,将会乱码
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
//关闭文件流释放资源
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
OutputStream
FileOutputStream
FileOutputStream具体方法:
package iofuxi;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 使用FileOutputStream 将数据写入文件
* 如果该文件不存在,则创建该文件
*/
public class FileOutputStream_ {
public static void main(String[] args) {
//创建FileOutputStream 对象
String filePath = "d:\\a.txt";
FileOutputStream fileOutputStream = null;
try {
//得到了一个对象
fileOutputStream = new FileOutputStream(filePath);
//写入一个字节 方法一
//fileOutputStream.write('H');
//写入字符串 方法二
String str = "hello word!111";
//str.getBytes() 可以把 字符串 -> 字节数组
// fileOutputStream.write(str.getBytes());
/* 方法三
write(byte[] b, int off, int len)
将 len字节从位于偏移量 off的指定 byte阵列写入此输出流。
*/
fileOutputStream.write(str.getBytes(),0,str.length());
//str.length() 可以灵活改动,写< str.length() 个字节
/*
问题, 每次执行会覆盖原来数据内容 fileOutputStream = new FileOutputStream(filePath);
fileOutputStream = new FileOutputStream(filePath,true); 创建方式,当写入内容是追加到文件内容后
*/
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
问题:每次执行会覆盖原来数据内容 fileOutputStream = new FileOutputStream(filePath); fileOutputStream = new FileOutputStream(filePath,true); 创建方式,当写入内容是追加到文件内容后。
注意构造方法:
未完,待续......