search
HomeJavajavaTutorialWhat are the read and write operations of Java IO stream creation?

概念

IO流可以初步的理解为数据间的传输,我们将一组数据入:1234567,将他们从hello文件中转入haha文件中,使用程序的方法进行转入的话则需要一个一个的传入,即为一个字节一个字节的传输,我们每次只能传入或读取一个字节,这就是io流的大致流程,io流对任何类型的文件都可以进行读取。如:文本文件,图片,歌曲mp3,视频等等的。

因为io流是一个字节一个字节的传入读取的所以我们需要用到byte单字节变量来获取长度。如果获取过多的内容则需要使用对应的数组。

io流对应的方法

所有io流方法中都需要写入相应的文件操作路径,且所有io流的方法都有一个共同的父类接口(Exception),所以我们在使用时都需要链接相对应的接口如:

public static void main(String[] args) throws Exception

一、File方法(创建文件)

声明方式:

File file1 = new File("D:\\java制作\\高级特性\\hehe.txt");

File方法主要用于创建文件,且该方法在使用的时候必须填写需要被创建的文件的具体路径,我们需要将对应的文件类型后缀也写出来,如果没有路径的话默认是文件夹的格式,创建文件的方法如下:

file.createNewFile();//创造相对应的文件
file.mkdirs();//创建文件夹

.createNewFile():当且仅当具有该名称的文件不存在时,将会在对应的路径中创建一个对应的文件

.mkdirs():当且仅当具有该名称的文件夹不存在时,将会在对应的路径中创建一个对应的文件夹

File方法既然有创建文件的方式那么自然也少不了删除于判断文件是否存在的方法。

Boolean decide = file.exists();//判断该文件是否存在
file.delete();//删除该文件

.exists():测试此路径表示的文件是否存在,如果存在则返回true否则返回false

.delete():删除由此抽象路径下的文件或文件夹

此外也有一些相对应的查看文件的方法如名称,路径,大小

System.out.println("文件名称:"+file.getName());
System.out.println("相对路径:"+file.getPath());
System.out.println("绝对路径:"+file.getAbsolutePath());
System.out.println("文件大小:"+file.length()+"字节");

.getName():根据英语词义就可得知该方法是获取相应文件的文件名。

.getPath():将相应文件的路径转换为字符串

.getAbsolutePath():将相应文件的绝对路径转换为字符串格式,与上述方法相对比更加精确。

.length():返回该文件的长度,即内部字节的长度。

二、FileInputStream(获取字节方法)输入流

声明方式:

FileInputStream fis = new FileInputStream("D:\\java制作\\高级特性\\hello.txt");

FileInputStream用于读取文件内字节内容的方法,且该方法在使用的时候必须填写需要被创建的文件的具体路径,我们平常对内部内容进行读写的方式如下:

byte[] data = new byte[fis.available()];//获取文件内容并以字节的方式存储如byte[]数组中
System.out.println((char)fis.read());//读出相对应的字节并以char的方式输出
//使用循环遍历出全部的字节
byte[] data = new byte[fis.available()];
for (int i = 0; i < data.length; i++) {
    System.out.print((char) fis.read());
}
System.out.println((char)fis.read(data,0,data.length));

.available():读取剩余的字节数,且必须用byte[]数组来存储对应的长度,因为byte是用来对字节的专门处理,该方法读取的是字节数,虽然输出时不会出错,但方法循环中时会出错

.read():读取该文件中的第一个字节,因为是字节的格式所以我们需要用char(单字符变量)进行转换,才能将其输出,注意每次只能读取一个,且不会读出同一个位置的字节,每读完一个就会少一个,如果读完后继续进行读取就会答应出一个黑色边框的空格。也可以通过需求去调用相应下标下的字节就如上述的最后一行代码。

fis.close();

.close():关闭此文件的输入流并释放与流相关联的任何系统资源,在我们引用FileInputStream流是就已经默认打开了输入流,当我们不使用它是应将其关闭,就好比我们进入了放假需要开门,当我们进入拿走需要的文件后离开房间要关门一样,否则它会一直开着占用电脑性能

三、FileOutputStream(写入字节方法)输出流

声明方式:

FileOutputStream fos = new FileOutputStream("D:\\java制作\\高级特性\\hehe.txt");

FileInputStream用于读取文件内字节内容的方法,且该方法在使用的时候必须填写需要被创建的文件的具体路径,我们平常对内部内容进行写入的方式如下:

String str = "天天向上";//将需要进行存储的内容存入变量
byte[] words = str.getBytes();//将字符串变为字节方式进行存储
fos.write(words);//最后通过字节的方式进行存储

.write():将指定的内容存入文件输出流再由输出流存入文件中,存入时我们需要将文件格式转换为电脑可读懂的方式,8进制字节,所以我们需要将存储的内容用byte强转为8进制字节

fos.close();//关闭输出流

.close():关闭此文件的输出流并释放与流相关联的任何系统资源,在我们引用

FileInputStream流是就已经默认打开了输入流,当我们不使用它是应将其关闭,就好比我们进入了放假需要开门,当我们进入拿走需要的文件后离开房间要关门一样,否则它会一直开着占用电脑性能

The above is the detailed content of What are the read and write operations of Java IO stream creation?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor