In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces you to use input and output IO stream and buffer stream to achieve text file replication of the five methods, the content is very detailed, interested friends can refer to, hope to be helpful to you.
IO stream and buffer stream can help us read and write text files.
Next, let's look directly at the five ways to copy a text file and look directly at the code:
Package com.qibao.iostream
Import java.io.BufferedReader
Import java.io.BufferedWriter
Import java.io.FileReader
Import java.io.FileWriter
Import java.io.IOException
Public class FileCopyDemo {
Public static void main (String [] args) throws IOException {
String srcFileName = "src\ com\\ qibao\\ iostream\\ FileCopyDemo.java"
String destFileName = "src\ com\\ qibao\\ iostream\\ Copy.txt"
Method1 (srcFileName, destFileName)
Method2 (srcFileName, destFileName)
Method3 (srcFileName, destFileName)
Method4 (srcFileName, destFileName)
Method5 (srcFileName, destFileName)
}
/ *
* basic IO stream reads and writes one character at a time
, /
Private static void method1 (String srcFileName, String destFileName) throws IOException {
/ / create an input stream object
FileReader fr = new FileReader (srcFileName)
/ / create an output stream object
FileWriter fw = new FileWriter (destFileName)
Int ch
/ / read and write one character at a time
While ((ch = fr.read ())! =-1) {
Fw.write (ch)
Fw.flush ()
}
/ / release resources
Fw.close ()
Fr.close ()
}
/ *
* basic IO stream reads one array at a time
, /
Private static void method2 (String srcFileName, String destFileName) throws IOException {
/ / create an input stream object
FileReader fr = new FileReader (srcFileName)
/ / create an output stream object
FileWriter fw = new FileWriter (destFileName)
Char [] chs = new char [1024]
Int len
/ / read and write one character array at a time
While ((len = fr.read (chs))! =-1) {
Fw.write (chs, 0, len)
Fw.flush ()
}
/ / release resources
Fw.close ()
Fr.close ()
}
/ *
* buffered streams read and write one character at a time
, /
Private static void method3 (String srcFileName, String destFileName) throws IOException {
/ / create an input buffered stream object
BufferedReader br = new BufferedReader (new FileReader (srcFileName))
/ / create an output buffered stream object
BufferedWriter bw = new BufferedWriter (new FileWriter (destFileName))
Int ch
/ / buffered streams read and write one character at a time
While ((ch = br.read ())! =-1) {
Bw.write (ch)
Bw.flush ()
}
/ / release resources
Bw.close ()
Br.close ()
}
/ *
* buffered streams read one array at a time
, /
Private static void method4 (String srcFileName, String destFileName) throws IOException {
/ / create an input buffered stream object
BufferedReader br = new BufferedReader (new FileReader (srcFileName))
/ / create an output buffered stream object
BufferedWriter bw = new BufferedWriter (new FileWriter (destFileName))
Char [] chs = new char [1024]
Int len
/ / read and write one character array at a time
While ((len = br.read (chs))! =-1) {
Bw.write (chs, 0, len)
Bw.flush ()
}
/ / release resources
Bw.close ()
Br.close ()
}
/ *
* buffered stream reads strings at once
, /
Private static void method5 (String srcFileName, String destFileName) throws IOException {
/ / create an input buffered stream object
BufferedReader br = new BufferedReader (new FileReader (srcFileName))
/ / create an output buffered stream object
BufferedWriter bw = new BufferedWriter (new FileWriter (destFileName))
/ / buffered streams read one row of data at a time
String str
While ((str = br.readLine ())! = null) {
Bw.write (str)
Bw.newLine ()
Bw.flush ()
}
/ / release resources
Bw.close ()
Br.close ()
}
}
On the use of input and output IO stream and buffer stream to achieve text file replication of the five methods are shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.