【JAVA学习笔记】文件基础知识


一、文件

什么是文件

文件是保存数据的地方,比如大家经常使用的word文档,txt文件,excel文件…。它既可以保存一张图片,也可以保存视频,声音.…

文件流

文件在程序中是以流的形式来操作的

流:数据在数据源(文件)和程序(内存)之间经历的路径
输入流:数据从数据源(文件)到程序(内存)的路径
输出流:数据从程序(内存)到数据源(文件)的路径

常用的文件操作

创建文件

//方式1 new File(String pathname)
	public static void create01() {
   
		String filePath = "D:\\news1.txt";
		File file = new File(filePath);
		
		try {
   
			file.createNewFile();
			System.out.println("news1.txt创建成功");
		} catch (IOException e) {
   
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}
	
	//方式2 new File(File parent , String child)
	public static void create02() {
   
		File parentFile = new File("D:\\");
		String filename = "news2.txt";
		File file = new File(parentFile, filename);
		
		try {
   
			file.createNewFile();
			System.out.println("news2.txt创建成功");
		} catch (IOException e) {
   
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
 	}
	
	//方式3 new File(String parent , String child)
	public static void create03() {
   
		String parentPath = "D:\\";
		String filePath = "news3.txt";
		File file = new File(parentPath, filePath);
		
		try {
   
			file.createNewFile();
			System.out.println("news3.txt创建成功");
		} catch (IOException e) {
   
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
 	}

获得文件相关信息

public static void info() {
   
		//创建文件对象
		File file = new File("D:\\news1.txt");
		
		System.out.println("文件名:"+file.getName());
				
		System.out.println("文件绝对路径:"+file.getAbsolutePath());
		
		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());
	}

目录的操作和文件删除

//删除文件或者目录,在java编程中,目录也被当做一种文件
		//删除目录,必须是个空目录才能删除成功
		String filePath = "D:\\news1.txt";
		File file = new File(filePath);
		file.delete();
	
		
		//创建多级目录,mkdir()只能创建一级目录
		String directoryPath = "D:\\a\\b\\c";
		File file2 = new File(directoryPath);
		file2.mkdirs();

二、IO流原理及流的分类

IO流原理

  1. I/O是Input/Output的缩写,V/O技术是非常实用的技术,用于处理数据传输.
    如读/写文件,网络通讯等。
  2. Java程序中,对于数据的输入/输出操作以”流(stream)”的方式进行。
  3. java.io包下提供了各种“流”类和接口,用以获取不同种类的数据,并通过方法输入或输出数据
  4. 输入input:读取外部数据(磁盘、光盘等存储设备的数据)到程序(内存)中。
  5. 输出output:将程序(内存)数据输出到磁盘、光盘等存储设备中

流的分类

  1. 按操作数据单位不同分为:字节流(8 bit) 二进制文件,字符流(按字符,根据编码不同而不同) 文本文件
  2. 按数据流的流向不同分为:输入流,输出流
  3. 按流的角色的不同分为:节点流,处理流/包装流
  4. 字节输入流:InputStream;字节输出流:OutputStream
  5. 字符输入流:Reader;字符输出流:Writer
  6. Java的IO流共涉及40多个类,实际上非常规则,都是从如上4个抽象基类派生的.由这四个类派生出来的子类名称都是以其父类名作为子类名后缀。

InputStream

代码如下(示例):

//用FileInputStream演示
//一个字节一个字节读
public static void readFile01() {
   
		String filePath = "D:\\news2.txt";
		FileInputStream fileInputStream=null;
		int read=0;
		
		try {
   
			fileInputStream = new FileInputStream(filePa
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值