Home> Java> javaTutorial> body text

How to use FileInputStream and FileOutputStream functions in Java for file streaming operations

PHPz
Release: 2023-06-26 16:42:10
Original
1382 people have browsed it

The FileInputStream and FileOutputStream functions in Java are two important classes used for file stream operations. They can read and write files, and support operations such as copying, renaming, and deleting files. This article will introduce in detail how to use these two classes for file stream operations.

  1. FileInputStream class

The FileInputStream class is used to read file contents. You can create a file input stream object through its constructor, and then use its read() method to read the contents of the file byte by byte.

The following is a sample code that uses FileInputStream to read the file content:

import java.io.FileInputStream; import java.io.IOException; public class FileInputDemo { public static void main(String[] args) { try { // 创建文件输入流对象 FileInputStream fis = new FileInputStream("test.txt"); // 读取文件内容 int data; while ((data = fis.read()) != -1) { System.out.print((char) data); } // 关闭输入流 fis.close(); } catch (IOException e) { e.printStackTrace(); } } }
Copy after login

In the above sample code, a FileInputStream object is first created to read the file named test.txt, and then Use a while loop to read the file contents byte by byte and print it on the console. Finally, close the input stream to release resources.

  1. FileOutputStream class

The FileOutputStream class is used to write file contents. You can create a file output stream object through its constructor, and then use its write() method to write the file content byte by byte.

The following is a sample code for using FileOutputStream to write the contents of a file:

import java.io.FileOutputStream; import java.io.IOException; public class FileOutputDemo { public static void main(String[] args) { try { // 创建文件输出流对象 FileOutputStream fos = new FileOutputStream("test.txt"); // 写入文件内容 String content = "Hello, world!"; byte[] data = content.getBytes(); fos.write(data); // 关闭输出流 fos.close(); } catch (IOException e) { e.printStackTrace(); } } }
Copy after login

In the above sample code, a FileOutputStream object is first created to write to a file named test.txt, and then Convert the string "Hello, world!" to a byte array and write it to a file using the write() method. Finally, close the output stream to release resources.

  1. Copy files

Using Java's file stream operation can easily copy files. You only need to create two file stream objects, one to read the original file content and one to write the target file content.

The following is a sample code for copying a file:

import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileCopyDemo { public static void main(String[] args) { try { // 创建文件输入流对象 FileInputStream fis = new FileInputStream("original.txt"); // 创建文件输出流对象 FileOutputStream fos = new FileOutputStream("copy.txt"); // 逐个字节读取并写入文件内容 int data; while ((data = fis.read()) != -1) { fos.write(data); } // 关闭输入输出流 fis.close(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } }
Copy after login

In the above sample code, a FileInputStream object is first created to read the file named original.txt, and then a FileOutputStream object is created to write a file named copy.txt, and then use a while loop to read the original file content byte by byte and write it to the target file. Finally, close the input and output streams to release resources.

  1. Rename files

Renaming files is one of the commonly used functions in file operations. File renaming can be easily achieved using Java's file stream operations.

The following is a sample code for renaming a file:

import java.io.File; public class FileRenameDemo { public static void main(String[] args) { // 创建旧文件对象 File oldFile = new File("old.txt"); // 创建新文件对象 File newFile = new File("new.txt"); // 重命名文件 if (oldFile.renameTo(newFile)) { System.out.println("文件重命名成功"); } else { System.out.println("文件重命名失败"); } } }
Copy after login

In the above sample code, an old file object and a new file object are first created, and then the old file object is created using the renameTo() method. The file is renamed to a new file. If the rename is successful, "File rename successful" is output, otherwise "File rename failed" is output.

  1. Delete files

Deleting files is also a common file operation function. File deletion can be easily achieved using Java's file stream operations.

The following is a sample code to delete a file:

import java.io.File; public class FileDeleteDemo { public static void main(String[] args) { // 创建文件对象 File file = new File("test.txt"); // 删除文件 if (file.delete()) { System.out.println("文件删除成功"); } else { System.out.println("文件删除失败"); } } }
Copy after login

In the above sample code, a file object is first created, and then the file is deleted using the delete() method. If the deletion is successful, "File Deletion Successful" is output, otherwise "File Deletion Failed" is output.

Summary

Java’s file stream operations are very powerful and flexible. The FileInputStream and FileOutputStream classes can be used to easily perform file reading and writing operations, and the File class can be used to copy, rename, and delete files. These operations are essential for Java developers. I hope this article can be helpful to beginners.

The above is the detailed content of How to use FileInputStream and FileOutputStream functions in Java for file streaming operations. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!