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();
}
}
}
}
}
文件中的信息是:黄昏
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();
}
}
}
}
}
文件中的信息是:黄昏
相关文章推荐
- java调用c#wcf心得,主要解决传递图片问题
- eclipse不打印Logcat信息
- java中常用的集合极其特点
- Java 多线程 并发编程
- java基础算法之递推
- Java 锁的学习
- Java 工程师成神之路
- Java的动态代理
- java中常见异常的结构和分类
- spring框架在开发中为我们做了哪些事?
- Java中停止线程执行的方法
- 从 Java 应用程序动态生成 PDF 文件
- socket理解
- java生成PDF文件
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- 你不可不知的Eclipse快捷键
- 你不可不知的Eclipse快捷键
- java怎么循环遍历ArrayList
- [翻译]现代java开发指南 第一部分
- [改善Java代码]性能考虑,数组是首选