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);
}
}