文件的读写 -- java FileInputStream
来源:互联网 发布:河南百度seo排名优化 编辑:程序博客网 时间:2024/06/16 00:36
一 FileInputStream 方式读写 可以读写 jpg doc txt等文件,因为 以字节流 方式传输
二 FileReader 方式读写 只能读写txt文件,因为以 字符流 方式传输
package test.file;
import java.io.*;
public class FileIOTest {
/**
* 读取的字符 为-1 表示文件结束 对中文采用unicode
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FileIOTest f = new FileIOTest();
try {
f.ReadWriteFileByByte();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//用字节流传输 可以将字节流转换成指定的字符流 详见 函数ReadWriteFile
public void ReadWriteFileByByte() throws IOException{
String sfile = "c:\\企业文化(1)(1).doc";
// String sfile = "c:\\ask.jpg";
String dfile ="d:\\企业文化(1)(1).doc";
File srcFile = new File(sfile);
File destFile = new File(dfile);
try {
if(!destFile.exists()){
destFile.createNewFile();
}
FileInputStream fin = new FileInputStream(srcFile);
FileOutputStream fout = new FileOutputStream(destFile);
// 在末尾追加字符
// FileOutputStream fout = new FileOutputStream(destFile,true);
//这样是不对的 因为无法检测是否为-1 表示文件结束
// byte[] bytes = new byte[1024];
// fin.read(bytes);
// fout.write(bytes);
// 一次读取1024字节
byte[] bytes = new byte[1024];
while(fin.read(bytes)!=-1){
fout.write(bytes);
fout.flush();
}
fin.close();
fout.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//用字符流输入输出 readline 一行一行地读取
public void ReadWriteFile() throws IOException{
//只能打开txt 文件 复制 c:\\企业文化.doc就出现乱码
String sfile = "c:\\ProjectEstablishFinderImpl.java";
String dfile ="d:\\ProjectEstablishFinderImpl.java";
//转换编码 这里没有用到
File srcFile = new File(sfile);
FileInputStream fin1 = new FileInputStream(srcFile);
InputStreamReader ins1 = new InputStreamReader(fin1,"utf-8");
// 转换编码结束
// File destFile = new File(dfile);
// FileOutputStream fout = new FileOutputStream(destFile);
// OutputStreamWriter outs = new OutputStreamWriter(fout);
FileReader ins = new FileReader(sfile);
FileWriter outs = new FileWriter(dfile);
BufferedReader readBuf = new BufferedReader(ins);
BufferedWriter writeBuf = new BufferedWriter(outs);
String s = null;
while((s=readBuf.readLine())!=null){
// System.out.println(s);
writeBuf.write(s);
writeBuf.newLine();//启用新的一行
writeBuf.flush();
}
readBuf.close();
writeBuf.close();
}
//编码转换~~
// public String encode(byte[] array){
// String~~
// return EncodingUtils.getString(array,TEXT_ENCODING);
// }
}
- 文件的读写 -- java FileInputStream
- java 使用FileInputStream,FileReader,BufferedStream读写记事本文件的例子
- 黑马程序员 java Properties FileInputStream 属性设置 文件读写
- Java文件读写操作(FileInputStream、FileReader、BufferedReader)
- 使用FileInputStream和FileOutputStream读写sdcard卡下的文件
- java的FileInputStream类读取文件
- Java基础———重温关于的文件读写操作,File类,FileInputStream类等的使用
- 用FileInputStream和FileOutPutStream读写文件
- 【IO流】FileInputStream,FileOutputStream读写文件
- [Java]FileInputStream/FileOutputStream 复制文件
- 【JAVA】文件输入流 FileInputStream
- java 读取文件内容 FileInputStream
- java复制单个文件 FileInputStream
- Java文件之FileInputStream、FileOutputStream
- java复制单个文件 FileInputStream
- java--文件复制FileInputStream FileOutputStream
- Java处理文件的方法(FileInputStream、FileReader、File*)
- java最基础的文件操作(FileInputStream和FileoutputStream)
- oracle 表约束的添加、修改以及约束的禁用启用(转)
- 制造业:一年进口芯片花费超万亿 中国制造在为美国打工?
- JSONObject 的简单使用
- 使用Camera2 替代过时的Camera API
- spring 事务管理
- 文件的读写 -- java FileInputStream
- PreparedStatement
- $Proxy5 cannot be cast to -- SSH 事务采用java5注解
- 迁移学习概述
- java 常见的Exception runtimeException
- 关于函数Vector3.Lerp差值函数的理解记录
- hibernate执行sql语句
- java 使用jdbc 链接数据库
- xml在html中格式化输出