Java常见应用(五):文件输入输出

文件的基本操作

File的构造方法

在这里插入图片描述

Modifier and TypeMethodDescription
booleancreateNewFile()Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.
booleandelete()Deletes the file or directory denoted by this abstract pathname.
StringgetName()Returns the name of the file or directory denoted by this abstract pathname.
FilegetAbsoluteFile()Returns the absolute form of this abstract pathname.
StringgetPath()Converts this abstract pathname into a pathname string.
booleanexists()Tests whether the file or directory denoted by this abstract pathname exists.
booleanmkdir()Creates the directory named by this abstract pathname.
//new File(String pathname)
		File file = new File("E:\\JavaLearning\\g");
		boolean f = file.exists();	//文件是否存在
		if(!f) {
			System.out.println("不存在该文件,创建!");
			file.createNewFile();	//不存在则创建
			
		}
		//file.delete();	//删除文件 直接删除 而不是放在回收站
		
		System.out.println(  );
		if(file.isFile()) {
			//文件属性
			System.out.println("文件--------------------------");
			System.out.println( file.getName() );	//文件名	abc.txt
			System.out.println( file.getAbsolutePath() );	//文件绝对路径 E:\JavaLearning\abc.txt
			System.out.println(	file.getPath()	);	//文件相对路径	E:\JavaLearning\abc.txt
			System.out.println(	file.length() );	//文件大小  130  单位为Byte
		}else if(file.isDirectory()) {
			System.out.println("文件夹-------------------------------");
			System.out.println( file.getName() );	//文件夹名	d
			System.out.println( file.getAbsolutePath() );	//文件夹绝对路径 E:\JavaLearning\d
			System.out.println(	file.getPath()	);	//文件夹相对路径	E:\JavaLearning\d
			System.out.println(	file.length() );	//文件夹大小  0
		}else {
			System.out.println("非文件夹也非文件");
		}

一开始E:\JavaLearning目录下已经放了实现创建好的abc.txt文件,执行结果为:
在这里插入图片描述
将目录下的abc.txt文件删除,再次执行结果为:
在这里插入图片描述
JavaLearning目录下创建d文件夹,然后执行:
在这里插入图片描述
File file = new File(String pathname);pathname里可以是不存在的文件或文件夹,如果没有使用创建语句创建文件或文件的话,那么isFile()和isDirectory()的结果都是false。

流是FIFO的数据结构。流按方向分可以分为输入流输出流,是以内存为参照物来区分流向的。
在这里插入图片描述

文件输入

输入是:文件–> 缓冲区

在工程目录src下创建hello.txt 文件并输入内容,然后执行以下代码:

package samples;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamDemo01 {
	public static void main(String[] args) {
		InputStream in = null;
		try {
//			InputStream in = new FileInputStream("src\\hello.txt"); 可直接文件名
			in = new FileInputStream(new File("src\\hello.txt"));
			//缓冲区
			byte buf[] = new byte[in.available()];// in.available() 为输入流大小
			//将文件hello.txt内容通过管道读入到内存的buff缓冲区
			in.read(buf);
			System.out.println(new String(buf));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {//最后要关闭输入流
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}

in.read(byte [])
将文件内容读到内存中的缓冲区

执行结果:
在这里插入图片描述

文件输出

输出是:内存----> 缓冲区

package samples;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class OutputStreamDemo01 {

	public static void main(String[] args) {
		OutputStream out = null;
		try {
			out = new FileOutputStream(new File("src\\xyz.txt"));
			//out.write(byte [])
			//缓冲区---> 文件
			out.write("Hello world !! I am Java……".getBytes());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

}

out.write(byte [])
将缓冲区的内容输出到指定文件。

文件复制

package samples;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileCopyDemo01 {
	
	public static void main(String[] args) {
		
		InputStream in = null;
		OutputStream out = null;
		try {
			in = new FileInputStream(new File("src\\hello.txt"));
			out = new FileOutputStream(new File("src\\hello2.txt"));
			
			//缓冲区 不宜过大 
			byte [] buff = new byte[10];
			
			int len = -1;
			//当缓冲区内容不为空(空时in.read(buff)返回-1),就将缓冲区内容输出到 目标文件hello2.txt
			while( (len = in.read(buff) ) != -1 ) {
				//要指定长度输出到目标文件
				out.write(buff,0, len);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {//先关输出流 再关输入流
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		

	}

}

文件复制即实现:文件a----> 缓冲区—>文件b
这里不能像文件输入案例一样,把缓冲区大小开为buff.available()。这是因为,如果要复制的文件过大,在内存开这么大一个缓冲区会把内存占掉的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值