当前位置:网站首页>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();
}
}
边栏推荐
- [从零开始学习FPGA编程-52]:高阶篇 - 基于IP核的FPGA开发 - IP核使用的基本框架(以锁相环PLL为例)
- Mongodb learning
- [learn FPGA programming from scratch -52]: high level chapter - FPGA development based on IP core - basic framework for IP core use (taking PLL as an example)
- Introduction to system programming
- FortiGate firewall and Aruze cloud tunnel interruption
- Qt6 QML Book/Qt Quick 3D/Qt Quick 3D
- Five methods to clear floating and their advantages and disadvantages
- Interview topic of MySQL
- Create thread in callable mode
- JS static method
猜你喜欢

Threejs realizes the simulation of river, surface flow, pipe flow and sea surface

MySQL DDL change

Blocking queue example

Es2017 key summary

FortiGate firewall quick initialization administrator password

Error encountered in SQL statement, solve

Interprocess communication

Mongodb learning

Day 12 advanced programming techniques

破局存量客群营销,试一下客户分群管理(含聚类模型等实操效果评估)
随机推荐
Implementation steps of dynamic proxy
Interview topic of MySQL
MySQL DDL change
FortiGate firewall modifies the default timeout of a session
Knowledge - how to build rapport in sales with 3 simple skills
Blue Bridge Cup: magic cube rotation [Vocational group]
After the win10 system uses the browser to download, the content is moved or deleted without reason
oslo_ config. cfg. ConfigFileParseError: Failed to parse /etc/glance/glance-api. Conf: a solution to errors
Slam mapping, automatic navigation and obstacle avoidance based on ROS (bingda robot)
Idea grey screen problem
Qt 6.3.1Conan软件包发布
QT 6.3.1conan software package release
Five methods to clear floating and their advantages and disadvantages
Redis cache avalanche, breakdown and penetration
Pig-Latin (UVA492)
Websocket implementation principle
Learning about signals
Day 11 script and game AI
Imile uses Zadig's multi cloud environment to deploy thousands of times a week to continuously deliver global business across clouds and regions
Indefinite parameters of JS function