IO流
1.输入流与输出流
- 流:数据在数据源(文件)和程序(内存)之间的路径
- 输入流:数据从数据源(文件)到程序(内存)的路径
- 输出流:数据从程序(内存)到数据源(文件)的路径
常用的文件操作
File类实现了Serializable与Comparable接口
File类的构造器
new File(String pathname)//根据路径构建一个File对象
new File(File parent,String pathname)//根据父目录文件+子路径构建一个File对象
new File(String parent,Sting child)//根据父路径+子路径构建一个File对象
new File (URI uri)//通过将给定的 file:
URI 转换为一个抽象路径名来创建一个新的 File
实例。
public static void main(String[] args) throws IOException, URISyntaxException {
File file1=new File("d:/news1.txt");
File file2=new File("d:\\","news2.txt");
File file3=new File("d:\\");
File file4=new File(file3,"news3.txt");
URI uri=new URI("file:///d:/news4.txt");
File file5= new File(uri);
//上面的file1、file2..等 只是在java中的一个对象
//只有执行了 createNewFile()方法,才会真正在磁盘中创建该文件 if(file1.createNewFile()&&file2.createNewFile()&&file4.createNewFile()&&file5.createNewFile()){
System.out.println("文件创建成功!");
}else {
System.out.println("文件创建失败!");
}
}
File对象常用的一些方法
- file.getName()//返回由此抽象路径名表示的文件或目录的名称。
- file.getAbsoluteFile()//返回此抽象路径名的绝对路径名形式。返回类型是File
- file.getAbsolutePath()//返回此抽象路径名的绝对路径名字符串。返回类型是String
- file.getParent()//返回此抽象路径名父目录的路径名字符串;如果此路径名没有指定父目录,则返回 null
- file.length()//返回由此抽象路径名表示的文件的长度。
- file.exists()//测试此抽象路径名表示的文件或目录是否存在。
- file.isFile()//测试此抽象路径名表示的文件是否是一个标准文件。
- file.isDirectory()//测试此抽象路径名表示的文件是否是一个目录。
- file.mkdir()// 创建此抽象路径名指定的目录(一级目录)。
- file.mkdir()//创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。(创建多级目录)
流的分类
字节流 字符流 输入流 InputStream Reader 输出流 OutputStream Writer
字节输入流(InputStream)常用的子类
- FileInputStream:文件输入流
- BufferedInputStream:缓冲字节输入流
- ObjectInputStream:对象字节输入流
2.字节流
FileInputStream
/**
* 单个字节读取效率低
*/
public void read01() {
String path="d:\\news1.txt";
FileInputStream fileInputStream=null;
try {
fileInputStream=new FileInputStream(path);
int read = 0;
while ((read=fileInputStream.read())!=-1){
System.out.print((char) read);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭文件流,释放资源
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void read2(){
String path="d:\\news1.txt";
FileInputStream fileInputStream=null;
//定义字节数组
byte[] buf=new byte[8];
try {
fileInputStream=new FileInputStream(path);
int readLen= 0;
//fileInputStream.read(buf) 如果读取正常返回实际读取的字节数 每次最多读8字节
//如果返回-1,表示读取完毕
while ((readLen=fileInputStream.read(buf))!=-1){
System.out.print(new String(buf,0,readLen));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭文件流,释放资源
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileOutPutStream
public class OutPutStream_ {
public static void main(String[] args) {
new OutPutStream_().writeFile();
}
public void writeFile(){
String filePath="d:\\news.txt";
FileOutputStream fos=null;
try {
//若没有该文件,则会自动创建
fos=new FileOutputStream(filePath);
/**
* 1、new FileOutputStream(filePath);这种创建方式,当写入内容时,会覆盖其内容
* 2、new FileOutputStream(filePath,true);这种方式创建,当写入内容时,会以追加的方式,不会覆盖原内容
*/
//写入一个字节
fos.write('a');
String str="wanna";
//将字符串转化为字符数组 str.getBytes()
fos.write(str.getBytes());
/**
* write(byte[] b, int off, int len)
* 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。
*/
fos.write(str.getBytes(),0,3);//截取前三个字节写入
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
两者结合使用 拷贝文件
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopy {
public static void main(String[] args) {
FileInputStream fis=null;
FileOutputStream fos=null;
String filePath="d:\\a.jpg";
String destFilePath="c:\\a.jpg";
byte[] bytes=new byte[1024];
int readLen=0;
try {
fis=new FileInputStream(filePath);
fos=new FileOutputStream(destFilePath);
while ((readLen=fis.read(bytes))!=-1){
fos.write(bytes,0,readLen);//一定要使用这个方法
}
System.out.println("拷贝完成!");
} catch (Exception e) {
e.printStackTrace();