当前位置:网站首页>IO stream, byte stream read / write copy
IO stream, byte stream read / write copy
2022-06-30 04:30:00 【A fat man】
Byte stream read
package io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
/**
* @author : htf
* Use byte stream to read file
*/
public class TestInputStream {
// Reading data from a file : Use inputStream
public static void read1() {
File file=new File("E:\\temp\\a.txt");
InputStream is=null;
try {
is =new FileInputStream(file);
int a1=is.read();
System.out.println((char)a1);
int a2=is.read();
System.out.println((char)a2);
int a3=is.read();
System.out.println((char)a3);
int a4=is.read();
System.out.println((char)a4);
int a5=is.read();
System.out.println((char)a5);
System.out.println();
} catch (Exception e) {
e.printStackTrace();
}
}
// Trying to read data from a file : Use inputStream
public static void read2() {
File file=new File("E:\\temp\\a.txt");
InputStream is=null;
try {
is =new FileInputStream(file);// Node flow
int r=0;
while((r=is.read())!=-1) {
System.out.println((char)r);
}
System.out.println();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void read3() {
File file=new File("E:\\temp\\a.txt");
InputStream is=null;
try {
is =new FileInputStream(file);
byte[] b=new byte[5];// new byte[1024] // Open a room to store the read data , This is the data transfer station , Also called a buffer
int t=0;
while((t=is.read(b))!=-1) {
System.out.println(new String(b, 0, t));
}
System.out.println();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//read1();
//read2();
read3();
}
}
Byte stream write
package io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* @author : htf
* Use byte stream write and file copy
*
*/
public class TestOutputStream {
//1. Output the data in the program to the hard disk file :outPutstream FileOutputStream
public static void test1() {
File parent=new File("E:\\temp");
// Determine if the directory exists , If not , Then create a new one
if(!parent.exists()) {
parent.mkdir();
}
File file=new File(parent, "aa.txt");
FileOutputStream fos=null;
try {
fos =new FileOutputStream(file);
fos.write(65);
fos.write(66);
fos.write(67);
fos.write(68);
fos.write(69);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
fos.close();// close resource
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// Open buffer write
public static void test2() {
File parent=new File("E:\\temp");
// Determine if the directory exists , If not , Then create a new one
if(!parent.exists()) {
parent.mkdir();
}
File file=new File(parent, "aaa.txt");
FileOutputStream fos=null;
try {
fos =new FileOutputStream(file);
int a=0;
byte b[]=new byte[] {65,66,67,68,69};
fos.write(b);// Write it all at once
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
fos.close();// close resource
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// Copy files : Reading while writing
public static void test3() {
File src=new File("E:\\temp\\a.txt");
File dest=new File("E:\\temp\\a1.txt");
FileInputStream fis=null;
FileOutputStream fos=null;
try {
fis=new FileInputStream(src);
fos=new FileOutputStream(dest);
// Read a , Write a
int a=fis.read();
fos.write(a);
a=fis.read();
fos.write(a);
a=fis.read();
fos.write(a);
a=fis.read();
fos.write(a);
a=fis.read();
fos.write(a);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// Copy files , Write in cycles
public static void test4() {
File src=new File("E:\\temp\\a.txt");
File dest=new File("E:\\temp\\a2.txt");
FileInputStream fis=null;
FileOutputStream fos=null;
try {
fis=new FileInputStream(src);
fos=new FileOutputStream(dest);
int a=0;
while((a=fis.read())!=-1) {
fos.write(a);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// Open a buffer to read files
public static void test5() {
File src=new File("E:\\temp\\a.txt");
File dest=new File("E:\\temp\\a3.txt");
FileInputStream fis=null;
FileOutputStream fos=null;
try {
fis=new FileInputStream(src);
fos=new FileOutputStream(dest);
byte b[]=new byte[1024];
int a=0;
while((a=fis.read(b))!=-1) {
fos.write(b,0,a);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
test5();
}
}
边栏推荐
- Basic knowledge of redis
- Refers to the difference between IP and *ip at output
- Qt Creator 8 Beta2发布
- El upload Upload file (Manual upload, Automatic upload, upload progress)
- 基于海康EhomeDemo工具排查公网部署出现的视频播放异常问题
- Five methods to clear floating and their advantages and disadvantages
- Es2017 key summary
- Fair lock and unfair lock
- JS static method
- I spent three years in a big factory outsourcing, which subverted my understanding!
猜你喜欢

Redis cache avalanche, breakdown and penetration

File and IO

Explain the underlying principles of JVM garbage collection in simple terms
![Salary management system based on servlet+jsp+mysql [source code + database]](/img/4a/6015cf17f4297691e97b48a5592fc5.png)
Salary management system based on servlet+jsp+mysql [source code + database]

Myrpc version 5

Technology sharing | broadcast function design in integrated dispatching

Machine learning notes

Day 12 advanced programming techniques

An error occurs when sqlyog imports the database. Please help solve it!

输入输出及中断技术——微机第六章学习笔记
随机推荐
AI落地的新范式,就“藏”在下一场软件基础设施的重大升级里
Intern method of string
Detailed explanation of data link layer
Default value of JS parameter
[从零开始学习FPGA编程-52]:高阶篇 - 基于IP核的FPGA开发 - IP核使用的基本框架(以锁相环PLL为例)
Es2017 key summary
QT creator 8 beta2 release
Iterator of JS
Pig-Latin (UVA492)
Blocking queue example
FortiGate firewall filters the specified session and cleans it up
破局存量客群营销,试一下客户分群管理(含聚类模型等实操效果评估)
Network layer protocol hardware
Detailed explanation of network layer
《机器人SLAM导航核心技术与实战》第1季:第0章_SLAM发展综述
2021-07-14
How to solve the problem of link hyperlinks when trying to link the database?
errno和perror
Daily summary of code knowledge
How the FortiGate firewall rejects a port by using the local in policy policy