Table of Contents
概念
io流对应的方法
一、File方法(创建文件)
二、FileInputStream(获取字节方法)输入流
三、FileOutputStream(写入字节方法)输出流
Home Java javaTutorial What are the read and write operations of Java IO stream creation?

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

May 14, 2023 am 08:22 AM
java io

概念

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");
Copy after login

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

声明方式:

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

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));
Copy after login

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

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

fis.close();
Copy after login

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

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

声明方式:

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

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

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

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

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

.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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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 ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

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

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

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

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

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

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

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

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

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

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

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

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

See all articles