读文件
/**
* 读文件
* @param file 目标文件
* @throws IOException
*/
public static void readFile(File file) throws IOException {
//实例化文件输入流
if(!file.exists()){
throw new IllegalAccessError("文件" + file + "不存在");
}
FileInputStream in = new FileInputStream(file);
//按每个字节进行读取
int temp;//读取的数据
int count = 0;//读取的数据的长度
while((temp = in.read()) != -1){
//按照16进制进行输出
//小于两个字节进行补零操作
if(temp < 0Xf){
System.out.print("0");
}
System.out.print(Integer.toHexString(temp) + " ");
//换行
if(++count % 10 == 0){
System.out.println();
}
}
in.close();
}
public static void readFile(String file) throws IOException {
readFile(new File(file));
}
写入文件
/**
* 写文件
* @param file 目标文件
* @param data 向文件中写的内容
* @param isAdd 是否向文件内追写内容,false则删除原文件,新建一个文件进行写操作
* @throws IOException
*/
public static void putFile(File file,byte[] data,FileOutputStream out) throws IOException{
//创建文件输出流
for(byte b: data){
out.write(b);
}
out.close();
}
public static void putFile(String file,byte[] data,boolean isAdd) throws IOException{
FileOutputStream out = new FileOutputStream(file,true);
putFile(new File(file),data,out);
}
public static void putFile(String file,byte[] data) throws IOException{
FileOutputStream out = new FileOutputStream(file);
putFile(new File(file),data,out);
}
拷贝文件
/**
* 按字节进行文件复制
* @param fromFile 数据来源文件
* @param ToFile 复制到的文件
* @return
* @throws IOException
*/
public static boolean copyFileByBite(File fromFile,File toFile) throws IOException {
if(!fromFile.exists()){
throw new IllegalArgumentException("文件" + fromFile + "不存在");
}
if(!fromFile.isFile()){
throw new IllegalArgumentException("文件" + fromFile + "不是文件");
}
//创建读取文件的输入流
FileInputStream in = new FileInputStream(fromFile);
//创建写入文件的输出流
FileOutputStream out = new FileOutputStream(toFile,true);
int inFlag = 0;//输入流读取的文件的长度
while((inFlag = in.read()) != -1){
out.write(inFlag);
out.flush();
}
in.close();
out.close();
return true;
}
/**
* 按通过一定的缓冲数组进行文件复制
* @param fromFile 数据来源文件
* @param ToFile 复制到的文件
* @return
* @throws IOException
*/
public static boolean copyFileByBuffer(File fromFile,File toFile) throws IOException {
if(!fromFile.exists()){
throw new IllegalArgumentException("文件" + fromFile + "不存在");
}
if(!fromFile.isFile()){
throw new IllegalArgumentException("文件" + fromFile + "不是文件");
}
//创建读取文件的输入流
FileInputStream in = new FileInputStream(fromFile);
//创建写入文件的输出流
FileOutputStream out = new FileOutputStream(toFile,true);
//创建读取文件的缓冲区
byte[] buffer = new byte[10 * 1024];
int inFlag = 0;//输入流读取的文件的长度
while((inFlag = in.read(buffer)) != -1){
//将缓冲区中读入的数据写入文件
for(int i = 0;i < inFlag;i++){
out.write(buffer[i]);
}
out.flush();
}
in.close();
out.close();
return true;
}
/**
* 通过带缓冲的字节流来复制
* @param fromFile 数据来源文件
* @param ToFile 复制到的文件
* @return
* @throws IOException
*/
public static boolean copyFileByBufferStream(File fromFile,File toFile) throws IOException {
if(!fromFile.exists()){
throw new IllegalArgumentException("文件" + fromFile + "不存在");
}
if(!fromFile.isFile()){
throw new IllegalArgumentException("文件" + fromFile + "不是文件");
}
//创建读取文件的输入缓冲流
BufferedInputStream bin = new BufferedInputStream(
new FileInputStream(fromFile));
//创建写入文件的输出缓冲流
BufferedOutputStream bout = new BufferedOutputStream(
new FileOutputStream(toFile));
int flag;
while((flag = bin.read()) != -1){
bout.write(flag);
bout.flush();
}
bin.close();
bout.close();
return true;
}