1.文件的读取
package test.byteio;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class Demo01 {
public static void main(String[] args) {
File src = new File("e:/xp/test/a.txt");
InputStream is = null;
try {
is = new FileInputStream(src);
byte[] car = new byte[1024];
int len = 0;
StringBuilder sb = new StringBuilder();
while (-1 != (len = is.read(car))) {
String info = new String(car, 0, len);
sb.append(info);
}
System.out.println(sb.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("文件不存在");
} catch (IOException e) {
e.printStackTrace();
System.out.println("读取文件失败");
} finally {
try {
if (null != is) {
is.close();
}
} catch (Exception e2) {
System.out.println("关闭文件输入流失败");
}
}
}
}
2.写出文件
package test.byteio;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Demo02 {
public static void main(String[] args) {
File dest = new File("e:/xp/test/test.txt");
OutputStream os = null;
try {
os = new FileOutputStream(dest, true);
String str = "bjsxt is very good \r\n";
byte[] data = str.getBytes();
os.write(data, 0, data.length);
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("文件未找到");
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件写出失败");
} finally {
try {
if (null != os) {
os.close();
}
} catch (Exception e2) {
System.out.println("关闭输出流失败");
}
}
}
}
3.文件复制
package test.byteio;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class CopyFileDemo {
public static void main(String[] args) {
String src = "E:/xp/test";
String dest = "e:/xp/test/4.jpg";
try {
copyFile(src, dest);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("文件不存在");
} catch (IOException e) {
e.printStackTrace();
System.out.println("拷贝文件失败|关闭流失败");
}
}
public static void copyFile(String srcPath, String destPath) throws FileNotFoundException, IOException {
File src = new File(srcPath);
File dest = new File(destPath);
if (!src.isFile()) {
System.out.println("只能拷贝文件");
throw new IOException("只能拷贝文件");
}
InputStream is = new FileInputStream(src);
OutputStream os = new FileOutputStream(dest);
byte[] flush = new byte[1024];
int len = 0;
while (-1 != (len = is.read(flush))) {
os.write(flush, 0, len);
}
os.flush();
os.close();
is.close();
}
}
4.文件夹复制
package test.byteio;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
public class CopyDir {
public static void main(String[] args) {
String srcPath="E:/test/a";
String destPath="E:/test/a/b";
try {
FileUtil.copyDir(srcPath,destPath);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void copyDir(String srcPath,String destPath){
File src=new File(srcPath);
File dest =new File(destPath);
copyDir(src,dest);
}
public static void copyDir(File src,File dest){
if(src.isDirectory()){
dest =new File(dest,src.getName());
}
copyDirDetail(src,dest);
}
public static void copyDirDetail(File src,File dest){
if(src.isFile()){
try {
FileUtil.copyFile(src, dest);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else if(src.isDirectory()){
dest.mkdirs();
for(File sub:src.listFiles()){
copyDirDetail(sub,new File(dest,sub.getName()));
}
}
}
}
5.文件工具类
package test.byteio;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FileUtil {
public static void copyDir(String srcPath,String destPath) throws FileNotFoundException, IOException{
if(srcPath.equals(destPath)){
return ;
}
File src=new File(srcPath);
File dest =new File(destPath);
copyDir(src,dest);
}
public static void copyDir(File src,File dest) throws FileNotFoundException, IOException{
if(src.isDirectory()){
dest =new File(dest,src.getName());
if(dest.getAbsolutePath().contains(src.getAbsolutePath())){
System.out.println("父目录不能拷贝到子目录中");
return;
}
}
copyDirDetail(src,dest);
}
public static void copyDirDetail(File src,File dest) throws FileNotFoundException,IOException{
if(src.isFile()){
try {
FileUtil.copyFile(src, dest);
} catch (FileNotFoundException e) {
throw e;
} catch (IOException e) {
throw e;
}
}else if(src.isDirectory()){
dest.mkdirs();
for(File sub:src.listFiles()){
copyDirDetail(sub,new File(dest,sub.getName()));
}
}
}
public static void copyFile(String srcPath,String destPath) throws FileNotFoundException,IOException {
copyFile(new File(srcPath),new File(destPath));
}
public static void copyFile(File src,File dest) throws FileNotFoundException,IOException {
if(! src.isFile()){
System.out.println("只能拷贝文件");
throw new IOException("只能拷贝文件");
}
if(dest.isDirectory()){
System.out.println(dest.getAbsolutePath()+"不能建立于文件夹同名的文件");
throw new IOException(dest.getAbsolutePath()+"不能建立于文件夹同名的文件");
}
InputStream is =new BufferedInputStream(new FileInputStream(src));
OutputStream os =new BufferedOutputStream(new FileOutputStream(dest));
byte[] flush =new byte[1024];
int len =0;
while(-1!=(len=is.read(flush))){
os.write(flush, 0, len);
}
os.flush();
os.close();
is.close();
}
}