JAVA输入输出流,
分享于 点击 17133 次 点评:135
JAVA输入输出流,
输入流
从硬盘上读取文件到内存
1,准备文件和输入流
2,准备字节数组开始读取
3,边读取边转换
4,关闭流
public static void main(String[] args) {
File file=new File("D:\\File.txt");
FileInputStream fis=null;
try {
fis=new FileInputStream(file);
byte []b=new byte[1024];
int len=fis.read(b);
while(len!=-1){
String str=new String(b,0,len);
System.out.println(str);
len=fis.read(b);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
输出流
从内存上写入文件到硬盘
1,准备文件和输出流
2,边写入边转换
3,关闭流
File file=new File("D:\\File.txt");
FileOutputStream fis=null;
try {
fis=new FileOutputStream(file);
String str="中国梦";
byte b[]=str.getBytes();
fis.write(b);
System.out.println(b);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
关于文件的复制
硬盘→内存→硬盘
代码参照上面代码修改
相关文章
- 暂无相关文章
用户点评