Java中对文件的操作
java中提供了io类库,可以轻松的用java实现对文件的各种操作。
1。新建目录
String filePath="c:/aaa/";
filePath=filePath.toString();//中文转换
java.io.File myFilePath=new java.io.File(filePath);
if(!myFilePath.exists())
myFilePath.mkdir();
2。新建文件
String filePath="c:/哈哈.txt";
filePath=filePath.toString();
File myFilePath=new File(filePath);
if(!myFilePath.exists())
myFilePath.createNewFile();
FileWriter resultFile=new FileWriter(myFilePath);
PrintWriter myFile=new PrintWriter(resultFile);
String strContent = "中文测试".toString();
myFile.println(strContent);
resultFile.close();
3。删除文件
String filePath="c:/支出证明单.xls";
filePath=filePath.toString();
java.io.File myDelFile=new java.io.File(filePath);
myDelFile.delete();
4。文件拷贝
int bytesum=0;
int byteread=0;
file://读到流中
InputStream inStream=new FileInputStream("c:/aaa.doc");
FileOutputStream fs=new FileOutputStream( "d:/aaa.doc");byte[] buffer =new byte[1444];
int length;
while ((byteread=inStream.read(buffer))!=-1)
{
out.println("<DT><B>"+byteread+"</B></DT>");
bytesum+=byteread;
System.out.println(bytesum);
fs.write(buffer,0,byteread);
}
inStream.close();
5。整个文件夹拷贝
String url1="C:/aaa";
String url2="d:/java/";
(new File(url2)).mkdirs();
File[] file=(new File(url1)).listFiles();
for(int i=0;i<file.length;i++){
if(file[i].isFile()){
file[i].toString();
FileInputStream input=new FileInputStream(file[i]);
FileOutputStream output=new FileOutputStream(url2+"/"+(file[i].getName()).toString());
byte[] b=new byte[1024*5];
int len;
while((len=input.read(b))!=-1){
output.write(b,0,len);
}
output.flush();
output.close();
input.close();
}
}
6。文件下载
String fileName = "zsc104.swf".toString();
//读到流中
InputStream inStream=new FileInputStream("c:/zsc104.swf");
//设置输出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition","attachment; filename=\"" + fileName + "\"");
//循环取出流中的数据
byte[] b = new byte[100];
int len;
while((len=inStream.read(b)) >0)
response.getOutputStream().write(b,0,len);
inStream.close();
7。数据库字段中的文件下载
int bytesum=0;
int byteread=0;
//打开数据库
ResultSet result=null;
String Sql=null;
PreparedStatement prestmt=null;
DBstep.iDBManager2000 DbaObj=new DBstep.iDBManager2000();
DbaObj.OpenConnection();
//取得数据库中的数据
Sql="select * from t_local_zhongzhuan ";
result=DbaObj.ExecuteQuery(Sql);
result.next();
file://将数据库中的数据读到流中
InputStream inStream=result.getBinaryStream("content");
FileOutputStream fs=new FileOutputStream( "c:/dffdsafd.doc");
byte[] buffer =new byte[1444];
int length;
while ((byteread=inStream.read(buffer))!=-1)
{
out.println("<DT><B>"+byteread+"</B></DT>");
bytesum+=byteread;
System.out.println(bytesum);
fs.write(buffer,0,byteread);
}
8。把网页保存成文件
URL stdURL = null;
BufferedReader stdIn = null;
PrintWriter stdOut = null;
try {
stdURL = new URL("http://www.163.com");
}
catch (MalformedURLException e) {
throw e;
}
try {
stdIn = new BufferedReader(new InputStreamReader(stdURL.openStream()));
stdOut = new PrintWriter(new BufferedWriter(new FileWriter("c:/163.html")));
}
catch (IOException e) {
}
/***把URL指定的页面以流的形式读出,写成指定的文件***/
try {
String strHtml = "";
while((strHtml = stdIn.readLine())!=null) {
stdOut.println(strHtml);
}
}
catch (IOException e) {
throw e;
}
finally {
try {
if(stdIn != null)
stdIn.close();
if(stdOut != null)
stdOut.close();
}
catch (Exception e) {
System.out.println(e);
}
}
9。直接下载网上的文件
int bytesum=0;
int byteread=0;
URL url = new URL("http://pimg.163.com/sms/micheal/logo.gif");
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();
FileOutputStream fs=new FileOutputStream( "c:/abc.gif");
byte[] buffer =new byte[1444];
int length;
while ((byteread=inStream.read(buffer))!=-1)
{
out.println("<DT><B>"+byteread+"</B></DT>");
bytesum+=byteread;
System.out.println(bytesum);
fs.write(buffer,0,byteread);
}
10。按行读文件
FileReader myFileReader=new FileReader("c:/哈哈.txt");
BufferedReader myBufferedReader=new BufferedReader(myFileReader);
String myString=null;
String resultString=new String();
while((myString=myBufferedReader.readLine())!=null) { resultString=resultString+myString+"<br>"; }
out.println(resultString); myFileReader.close();
Java按一行一行进行文件的读取或写入
/**
* 一行一行读取文件,适合字符读取,若读取中文字符时会出现乱码
*
* 流的关闭顺序:先打开的后关,后打开的先关,
* 否则有可能出现java.io.IOException: Stream closed异常
*
* @throws IOException
*/
@Test
public void readFile01() throws IOException {
FileReader fr=new FileReader("E:/phsftp/evdokey/evdokey_201103221556.txt");
BufferedReader br=new BufferedReader(fr);
String line="";
String[] arrs=null;
while ((line=br.readLine())!=null) {
arrs=line.split(",");
System.out.println(arrs[0] + " : " + arrs[1] + " : " + arrs[2]);
}
br.close();
fr.close();
}
/**
* 一行一行读取文件,解决读取中文字符时出现乱码
*
* 流的关闭顺序:先打开的后关,后打开的先关,
* 否则有可能出现java.io.IOException: Stream closed异常
*
* @throws IOException
*/
@Test
public void readFile02() throws IOException {
FileInputStream fis=new FileInputStream("E:/phsftp/evdokey/evdokey_201103221556.txt");
InputStreamReader isr=new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr);
//简写如下
//BufferedReader br = new BufferedReader(new InputStreamReader(
// new FileInputStream("E:/phsftp/evdokey/evdokey_201103221556.txt"), "UTF-8"));
String line="";
String[] arrs=null;
while ((line=br.readLine())!=null) {
arrs=line.split(",");
System.out.println(arrs[0] + " : " + arrs[1] + " : " + arrs[2]);
}
br.close();
isr.close();
fis.close();
}
/**
* 一行一行写入文件,适合字符写入,若写入中文字符时会出现乱码
*
* 流的关闭顺序:先打开的后关,后打开的先关,
* 否则有可能出现java.io.IOException: Stream closed异常
*
* @throws IOException
*/
@Test
public void writeFile01() throws IOException {
String[] arrs={
"zhangsan,23,FuJian",
"lisi,30,ShangHai",
"wangwu,43,BeiJing",
"laolin,21,ChongQing",
"ximenqing,67,GuiZhou"
};
FileWriter fw=new FileWriter(new File("E:/phsftp/evdokey/evdokey_201103221556.txt"));
//写入中文字符时会出现乱码
BufferedWriter bw=new BufferedWriter(fw);
//BufferedWriter bw=new BufferedWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("E:/phsftp/evdokey/evdokey_201103221556.txt")), "UTF-8")));
for(String arr:arrs){
bw.write(arr+"\t\n");
}
bw.close();
fw.close();
}
/**
* 一行一行写入文件,解决写入中文字符时出现乱码
*
* 流的关闭顺序:先打开的后关,后打开的先关,
* 否则有可能出现java.io.IOException: Stream closed异常
*
* @throws IOException
*/
@Test
public void writeFile02() throws IOException {
String[] arrs={
"zhangsan,23,福建",
"lisi,30,上海",
"wangwu,43,北京",
"laolin,21,重庆",
"ximenqing,67,贵州"
};
//写入中文字符时解决中文乱码问题
FileOutputStream fos=new FileOutputStream(new File("E:/phsftp/evdokey/evdokey_201103221556.txt"));
OutputStreamWriter osw=new OutputStreamWriter(fos, "UTF-8");
BufferedWriter bw=new BufferedWriter(osw);
//简写如下:
//BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
// new FileOutputStream(new File("E:/phsftp/evdokey/evdokey_201103221556.txt")), "UTF-8"));
for(String arr:arrs){
bw.write(arr+"\t\n");
}
//注意关闭的先后顺序,先打开的后关闭,后打开的先关闭
bw.close();
osw.close();
fos.close();
}