字节流

来源:互联网 发布:网络变压器信号定义 编辑:程序博客网 时间:2024/05/29 19:19
例一:
import java.io.File ;
import java.io.OutputStream ;
import java.io.FileOutputStream ;
import java.io.IOException ;
import java.io.FileNotFoundException ;
public class Test{
public static void main(String[] args){
String path = "d:" + File.separator + "Test.txt" ;
File f = new File(path) ;
String str = "hello,world" ;
byte[] b = str.getBytes() ;
try{
OutputStream out = new FileOutputStream(f) ;
try{
for(int i = 0 ; i < b.length ; i++){
out.write(b[i]) ;
}
}catch(IOException e){
e.printStackTrace() ;
}
try{
out.close() ;
}catch(IOException e){
e.printStackTrace() ;
}
}catch(FileNotFoundException e){
e.printStackTrace() ;
}
}
}
例二
import java.io.File ;
import java.io.OutputStream ;
import java.io.FileOutputStream ;
import java.io.IOException ;
import java.io.FileNotFoundException ;
public class Test{
public static void main(String[] args){
String path = "d:" + File.separator + "Test.txt" ;
File f = new File(path) ;
String str = "hello,world" ;
byte[] b = str.getBytes() ;
try{
OutputStream out = new FileOutputStream(f) ;
try{
out.write(b) ;
}catch(IOException e){
e.printStackTrace() ;
}
try{
out.close() ;
}catch(IOException e){
e.printStackTrace() ;
}
}catch(FileNotFoundException e){
e.printStackTrace() ;
}
}
}
0 0
原创粉丝点击