java io 删除一个文件_关于java Io流的操作,复制(多个文件)、删除、剪切、下载网络资源 | 学步园...

public class FileUtil {

//复制文件

public void copyFile(String source, String dest) {

File sourceFile = new File(source);

File destFile = new File(dest + "/" + sourceFile.getName());

try {

FileInputStream fis = new FileInputStream(sourceFile);

FileOutputStream fos = new FileOutputStream(destFile);

BufferedInputStream bis = new BufferedInputStream(fis);

BufferedOutputStream bos = new BufferedOutputStream(fos);

int len = 0;

byte[] b = new byte[1024];

while((len = bis.read(b)) != -1) {

bos.write(b, 0, len);

}

bos.close();

bis.close();

fos.close();

fis.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

//复制文件及文件夹

public void copyField(String source, String dest) {

File sourceFile = new File(source);

//文件夹复制

if(sourceFile.isDirectory()) {

String[] names = sourceFile.list();

File file = new File(dest + "/" + sourceFile.getName());

if(!file.exists()) {

file.mkdir();

}

for(String name : names) {

copyField(source + "/" + name, file.getPath());

}

}

//文件复制

else {

this.copyFile(source, dest);

}

}

//删除文件及文件夹

public void deleteField(String source) {

File sourceFile = new File(source);

//文件夹

if(sourceFile.isDirectory()) {

String[] names = sourceFile.list();

//删除当前文件夹下面的子文件和子文件夹

for (String name : names) {

deleteField(source + "/" + name);

}

//删除当前文件夹

sourceFile.delete();

}

//文件

else {

sourceFile.delete();

}

}

//剪切文件及文件夹

public void cutField(String source, String dest) {

//复制文件夹

//删除文件夹

}

//从网络上下载资源

public void download(URL url) {

try {

InputStream is = url.openStream();

FileOutputStream fos = new FileOutputStream("e:/123.gif");

BufferedInputStream bis = new BufferedInputStream(is);

BufferedOutputStream bos = new BufferedOutputStream(fos);

byte[] b = new byte[1024];

int len = 0;

while((len = bis.read(b)) != -1) {

bos.write(b, 0, len);

}

bos.close();

bis.close();

fos.close();

is.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.InputStreamReader;

import java.io.PrintWriter;

public class IOTest {

public staticvoid main(String[] args) throws Exception {

Stringstr = "中国人";

/*FileOutputStreamfos  = newFileOutputStream("1.txt");

fos.write(str.getBytes("UTF-8"));

fos.close();*/

/*FileWriterfw = new FileWriter("1.txt");

fw.write(str);

fw.close();*/

PrintWriterpw = new PrintWriter("1.txt","utf-8");

pw.write(str);

pw.close();

/*FileReaderfr = new FileReader("1.txt");

char[]buf = new char[1024];

int len =fr.read(buf);

StringmyStr = new String(buf,0,len);

System.out.println(myStr);*/

/*FileInputStreamfr = new FileInputStream("1.txt");

byte[]buf = new byte[1024];

int len =fr.read(buf);

StringmyStr = new String(buf,0,len,"UTF-8");

System.out.println(myStr);*/

BufferedReaderbr = new BufferedReader(

newInputStreamReader(

newFileInputStream("1.txt"),"UTF-8"

)

);

StringmyStr = br.readLine();

br.close();

System.out.println(myStr);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值