Java basics: I/O streams and File class file operation methods
♒I/O流原理及流的分类
I/O原理
I/O是Input和Output的缩写,I/O技术是非常实用的技术,用于处理数据传输(如:读/写文件,网络通信)
Java程序中,对于数据的输入/输出操作是以流(stream)的方式进行的
java.io包下提供了各种流(stream)类和接口,用以获取不同种类的数据,并通过方法输入和输出数据。
文件流:文件在程序中是以流的形式操作的
输入流:数据从数据源(文件)到程序(内存)的路径
输出流:数据从程序(内存)到数据源(文件)的路径
I/O流的分类
按操作数据单位分为:字节流(二进制文件)、字符流(文本文件)
按数据的流向分为:输入流、输出流
按流的角色分为:节点流、处理流
抽象基类 | 字节流 | 字符流 |
---|---|---|
输入流 | InputStream | Reader |
输出流 | OutputStream | Writer |
♨️I/O的体系结构
文件(File)
概念
什么是文件?
文件,对于我们并陌生,文件就是保存数据的地方,比如word文档、txt文本、excel文件、图片、视频…等都是文件,操作系统中以文件为单位管理磁盘中的数据。从数据存储角度来说,所有文件本质上都是一样的,都是由一个个字节组成的归根到底都是0-1比特串。
文件夹(目录)
多个文件如果不分类放在一起,用户使用起来就非常不方便,因此,又引入了树形目录(也叫文件夹)的机制,可以把文件放在不同的文件夹中,文件夹中还可以嵌套文件夹,这就便于用户对文件进行管理和使用。
✍️常用操作(File类)
创建文件对象相关构造器和方法
new File(String pathname);//根据路径构建一个File对象
new File(File parent,String child);//根据父目录文件+子路径构建
new File(String parent,String child);//根据父目录路径+子路径构建
createNewFile();//创建新文件
在E盘下,用以上方式创建文件test01.txt\test02.txt\test03.txt
import java.io.File; import java.io.IOException; public class FileCreate { public static void main(String[] args) throws IOException { //方式1 String pathname = "e:\\test01.txt"; File file1 = new File(pathname); file1.createNewFile(); //方式2 File parentfile = new File("e:\\"); String child2 = "test02.txt"; File file2 = new File(parentfile, child2); file2.createNewFile(); //方式3 String parent = "e:\\"; String child3 = "test03.txt"; File file3 = new File(parent, child3); file3.createNewFile(); } }
获取文件的相关信息
get.getName();//获取文件名字
canRead();//文件是否可读
canWrite();//文件是否可写
getAbsoultePath();//获取文件的绝对路径
getPath();//相对路径
getParent();//获取文件父级目录
lenth();//文件大小(字节)
exists();//判断文件是否存在
isFile();//判断是不是一个文件
isDirectory();//判断是不是一个目录
import java.io.File; public class FileInfomation { public static void main(String[] args) { //创建文件对象 File file = new File("e:\\test01.txt"); System.out.println("文件名字:" + file.getName()); System.out.println("文件是否可读:" + file.canRead()); System.out.println("文件是否可写:" + file.canWrite()); System.out.println("文件绝对路径:" + file.getAbsolutePath()); System.out.println("文件大小(字节):" + file.length()); System.out.println("文件是否存在:" + file.exists()); System.out.println("是不是一个文件:" + file.isFile()); System.out.println("是不是一个目录:" + file.isDirectory()); } }
文件比较
File f1=new File("D:\\test1.txt"); File f2=new File("D:\\test2.txt"); f1==f2;//比较的是两个对象的地址 f1.equals(f2);//比较两个对象对应的文件的路径
目录操作和文件删除
mkdir();//创建单层目录
mkdirs();//创建多层目录
delete();//删除目录(这层目录必须为空,没有内容)
查看文件目录
list();//返回一个字符串数组,命名由此抽象路径名表示的目录中的文件和目录。
listFiles();//返回一个抽象路径名数组,表示由该抽象路径名表示的目录中的文件。
案列:遍历一个目录下的所有文件打印输出
public class PrintFile { public static void main(String[] args) { //创建文件对象 File file = new File("e:\\Test"); String[] list = file.list();//文件夹下目录/文件对应的名字的数组 for (String s : list) { System.out.println(s); } File[] files = file.listFiles(); for (File f : files) { System.out.println(f.getName() + "," + f.getAbsolutePath()); } } }
遍历目录
1.给定一个文件对象file
2.listFiles()获取该文件下的所有文件对象数组
3.遍历File对象数组,如果是目录,递归调用该方法获取该目录下的所有文件对象;如果是文件,打印输出路径+姓名
import java.io.File; public class PrintFile { public static void main(String[] args) { File file = new File("E:\\Code"); getAllFile(file); } public static void getAllFile(File file) { //获取给定目录下的所有File对象数组 File[] files = file.listFiles(); //开始遍历 if (files != null) { for (File f : files) { if (f.isDirectory()) {//判断是否为目录,如果是,调用递归 getAllFile(f); } else {//不是,就打印路径+文件名 System.out.println(f.getAbsoluteFile() + "下的:" + f.getName()); } } } } }
The above is the detailed content of Java basics: I/O streams and File class file operation methods. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is
