您的位置:首页 > 编程语言 > Java开发

java文件输入输出流

2016-04-21 20:41 555 查看
package io;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

public class FileTest {

public static void main(String[] args){
File file=new File("D:","test.txt");
FileInputStream in=null;
FileOutputStream out=null;

try{
out=new FileOutputStream(file);
out.write("黄昏".getBytes());

}
catch(FileNotFoundException e){
e.printStackTrace();
}

catch(IOException e){
e.printStackTrace();

}finally{
if(out!=null){
try{
out.close();
}catch(IOException e){
e.printStackTrace();
}
}
}

try{
in=new FileInputStream(file);
byte[]content=new byte[1024];
int length=in.read(content);
System.out.println("文件中的信息是:"+new String(content,0,length));

}catch(FileNotFoundException e)

{
e.printStackTrace();

}

catch(IOException e){
e.printStackTrace();

}finally{
if(in!=null){
try{
in.close();
}
catch(IOException e){
e.printStackTrace();

}
}

}

}
}

文件中的信息是:黄昏
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: