【Java寒假打卡】Java基础-字节输入流和字节输出流)
概述 字节输出流快速入门 字节输出流的注意事项 输出字节流-一次性写多个数据 字节输出流的两个问题 try-catch-finally捕获异常 字节输入流写数据 字节流读数据 一次性读取多个数据 案例、文件复制 使用字节数组进行拷贝文件
概述
字节输出流快速入门
package com. hfut. edu. test9 ;
import java. io. File ;
import java. io. FileNotFoundException ;
import java. io. FileOutputStream ;
import java. io. IOException ;
public class test10 {
public static void main ( String [ ] args) throws IOException {
FileOutputStream fos = new FileOutputStream ( "D:\\heima\\1.txt" ) ;
FileOutputStream fos1 = new FileOutputStream ( new File ( "D:\\heima\\1.txt" ) ) ;
fos. write ( 97 ) ;
fos. close ( ) ;
}
}
字节输出流的注意事项
如果文件不存在 创建该文件 如果文件存在 写入数据的时候 直接清空 写出的整数 实际是ASCII值 对应码表的一个字符 每次使用完输出流,必须释放该资源
输出字节流-一次性写多个数据
package com. hfut. edu. test9 ;
import java. io. FileOutputStream ;
import java. io. IOException ;
public class test11 {
public static void main ( String [ ] args) throws IOException {
FileOutputStream fos = new FileOutputStream ( "D:\\heima\\1.txt" ) ;
byte [ ] bys = { 97 , 98 , 99 , 101 , 102 , 103 } ;
fos. write ( bys) ;
fos. write ( bys, 0 , 2 ) ;
fos. close ( ) ;
}
}
字节输出流的两个问题
package com. hfut. edu. test9 ;
import java. io. FileOutputStream ;
import java. io. IOException ;
import java. nio. charset. StandardCharsets ;
public class test11 {
public static void main ( String [ ] args) throws IOException {
FileOutputStream fos = new FileOutputStream ( "D:\\heima\\1.txt" ) ;
fos. write ( 97 ) ;
fos. write ( "\r\n" . getBytes ( ) ) ;
fos. write ( 98 ) ;
fos. write ( "\r\n" . getBytes ( ) ) ;
fos. write ( 99 ) ;
fos. write ( "\r\n" . getBytes ( ) ) ;
fos. close ( ) ;
}
}
package com. hfut. edu. test9 ;
import java. io. FileOutputStream ;
import java. io. IOException ;
import java. nio. charset. StandardCharsets ;
public class test11 {
public static void main ( String [ ] args) throws IOException {
FileOutputStream fos = new FileOutputStream ( "D:\\heima\\1.txt" , true ) ;
fos. write ( 97 ) ;
fos. write ( "\r\n" . getBytes ( ) ) ;
fos. write ( 98 ) ;
fos. write ( "\r\n" . getBytes ( ) ) ;
fos. write ( 99 ) ;
fos. write ( "\r\n" . getBytes ( ) ) ;
fos. close ( ) ;
}
}
try-catch-finally捕获异常
package com. hfut. edu. test9 ;
import java. io. File ;
import java. io. FileOutputStream ;
import java. io. IOException ;
public class test12 {
public static void main ( String [ ] args) {
FileOutputStream fos = null ;
try {
fos = new FileOutputStream ( new File ( "D:\\heima\\1.txt" ) ) ;
fos. write ( 97 ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
if ( fos != null ) {
try {
fos. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
}
}
}
字节输入流写数据
package com. hfut. edu. test9 ;
import java. io. File ;
import java. io. FileInputStream ;
import java. io. FileNotFoundException ;
import java. io. IOException ;
public class test13 {
public static void main ( String [ ] args) throws IOException {
FileInputStream fis = new FileInputStream ( new File ( "D:\\heima\\1.txt" ) ) ;
int read = fis. read ( ) ;
System . out. println ( ( char ) read) ;
fis. close ( ) ;
}
}
字节流读数据 一次性读取多个数据
package com. hfut. edu. test9 ;
import java. io. File ;
import java. io. FileInputStream ;
import java. io. FileNotFoundException ;
import java. io. IOException ;
public class test14 {
public static void main ( String [ ] args) throws IOException {
FileInputStream fis = new FileInputStream ( new File ( "D:\\heima\\1.txt" ) ) ;
int b;
while ( ( b = fis. read ( ) ) != - 1 )
{
System . out. println ( b) ;
}
fis. close ( ) ;
}
}
案例、文件复制
package com. hfut. edu. test9 ;
import java. io. File ;
import java. io. FileInputStream ;
import java. io. FileOutputStream ;
import java. io. IOException ;
public class test15 {
public static void main ( String [ ] args) throws IOException {
FileInputStream fis = new FileInputStream ( new File ( "D:\\heima\\1.txt" ) ) ;
FileOutputStream fos = new FileOutputStream ( "D:\\heima\\2.txt" ) ;
int b;
while ( ( b = fis. read ( ) ) != - 1 ) {
fos. write ( b) ;
}
fis. close ( ) ;
fos. close ( ) ;
}
}
使用字节数组进行拷贝文件
package com. hfut. edu. test9 ;
import java. io. File ;
import java. io. FileInputStream ;
import java. io. FileOutputStream ;
import java. io. IOException ;
public class test15 {
public static void main ( String [ ] args) throws IOException {
FileInputStream fis = new FileInputStream ( new File ( "D:\\heima\\1.txt" ) ) ;
FileOutputStream fos = new FileOutputStream ( "D:\\heima\\2.txt" ) ;
byte [ ] bytes = new byte [ 1024 ] ;
int len;
while ( ( len = fis. read ( ) ) != - 1 ) {
fos. write ( bytes, 0 , len) ;
}
fis. close ( ) ;
fos. close ( ) ;
}
}