当前位置:网站首页>Byte stream write input
Byte stream write input
2022-04-21 22:10:00 【Can't learn WOW】
Three ways of byte stream writing input
import java.io.*;
public class file_demo01 {
public static void main(String[] args) throws IOException {
// Create byte output stream object
//FileOutputStream(String name) Create a file output stream to write to the file with the specified name
FileOutputStream fos = new FileOutputStream("demo1/javase/j3.txt");
// If there is no such document , In fact, one will also be created j3.txt
/* 1. Call the system function to create a file 2. Create byte output stream object 3. Let the byte output stream object point to the created file * */
//1.void write (int b); Writes the specified bytes to the output stream of this file
// fos.write(99);// Here is ascll code
//2.void write(byte[] b);
// byte[] by = {97,98,99,100};
// fos.write(by);
//byte[] getBytes() Returns the byte array corresponding to the string
byte[] bytes="His faliure in the exam is attributable to his laziness".getBytes();
//3.void write(byte[] b, int off ,int len) take len Bytes from the specified off Position start , write in file The file of
fos.write(bytes,5,10);//offset In fact, it can be understood as subscript
fos.close();
}
}
import java.io.*;
public class file_demo01 {
public static void main(String[] args) throws IOException {
/* * 1. How to correctly realize line feed when writing data in byte stream ? * fos.write("\n".getBytes()); * 2. How to append byte stream write data ? *public FileOutputStream(String name, boolean append) * If the second parameter is true, Write the beginning of the byte to the end of the file instead of , In this way, we can add * */
FileOutputStream fos = new FileOutputStream("demo1/javase/j1.txt",true);
for (int i=0;i<=10;i++){
byte[] bytes = "yes,i do".getBytes();
fos.write(bytes);
fos.write("\n".getBytes());
}
fos.close();
}
}
import java.io.*;
public class file_demo01 {
public static void main(String[] args) {
/* Byte stream write data and exception handling * finally : Provide... In exception handling finally Block to perform all cleanup operations , for instance IO Free resources in the stream * Because when an exception is caught , Just jumped out of the program , As a result, subsequent cleanup operations do not release resources , Therefore use finally To enforce * characteristic : stay finally The control statement must be executed , Unless JVM sign out * */
FileOutputStream fos = null;
try {
fos = new FileOutputStream("demo1/javase/j1.txt");
fos.write("yes".getBytes());
}catch (IOException e){
e.printStackTrace();
}finally{
if (fos !=null){
// Preventing root creation fails , here fos=null
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
版权声明
本文为[Can't learn WOW]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212206183345.html
边栏推荐
- EventBridge 集成云服务实践
- ROS - use OpenCV to send and receive cameras
- Finding a way to combine new technologies with itself is the key to the development of industrial Internet
- Hospital-Oriented RFID Service
- Thread safety for the first time. This article is enough
- [best practice] patrol inspection item: local disk type inspection of cloud server (CVM) instance
- AUTOCAD——三种箭头的画法
- Online CSV to yaml tool
- CPT 102_ LEC 11
- 如何将 ODBC 数据库与 PHP 连接?
猜你喜欢

Oracle级联删除表(不受外键约束)

es6如何求两个数组的交集

Gd32f303 learning notes (1) -- setting up environment, compiling and writing

Helm installation sonarqube (bitnami) 9.3

搭建本地Canal中间件进行数据迁移 —— 从缓存击穿得到的启发

Seven schemes of implementing distributed lock based on redis

2022 Chongqing's latest architectural eight members (Civil Engineering) simulation question bank and answers

Online yaml to properties tool

Building local canal middleware for data migration -- Inspiration from cache breakdown
![[MySQL] solve the problem of MAC accessing MySQL server on windows](/img/45/53ef2fe3abed114148340787226260.png)
[MySQL] solve the problem of MAC accessing MySQL server on windows
随机推荐
【ES6】Promise
外包學生管理系統架構設計文檔
Analysts believe that Samsung Galaxy Z fold 4 and Z flip 4 may be cheaper than their previous products
什么?你们公司还没有将JVM初始和最大堆内存大小设置为相同值?
AUTOCAD——三种箭头的画法
Thread safety for the first time. This article is enough
【ES6】Iterator和forof循环
[ES6] iterator and forof loop
Attack and defense world MFW
ServiceWorker 缓存与 HTTP 缓存
Leetcode0785. Judgement bipartite graph (DFS)
Idea operates redis on Linux through jedis; Failed to connect to any host resolved for DNS name
[ES6] deconstruction and assignment of variables
Gd32f303 learning notes (1) -- setting up environment, compiling and writing
INT 102_ TTL 09
[ES6] array expansion
【ES6】let和const命令
【ES6】Generator
【ES6】对象的扩展
刷题有什么好的建议吗,怎么样可以提升效率?