public?static?void?getFileSize(String?path){
創(chuàng)新互聯(lián)建站是一家專業(yè)從事成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司。作為專業(yè)網(wǎng)站設(shè)計(jì)公司,創(chuàng)新互聯(lián)建站依托的技術(shù)實(shí)力、以及多年的網(wǎng)站運(yùn)營經(jīng)驗(yàn),為您提供專業(yè)的成都網(wǎng)站建設(shè)、成都全網(wǎng)營銷及網(wǎng)站設(shè)計(jì)開發(fā)服務(wù)!
//傳入文件路徑
File?file?=?new?File(path);
//測試此文件是否存在
if(file.exists()){
//如果是文件夾
//這里只檢測了文件夾中第一層?如果有需要?可以繼續(xù)遞歸檢測
if(file.isDirectory()){
int?size?=?0;
for(File?zf?:?file.listFiles()){
if(zf.isDirectory())?continue;
size?+=?zf.length();
}
System.out.println("文件夾?"+file.getName()+"?Size:?"+(size/1024f)+"kb");
}else{
System.out.println(file.getName()+"?Size:?"+(file.length()/1024f)+"kb");
}
//如果文件不存在
}else{
System.out.println("此文件不存在");
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class CreateFile {
public static void main(String[] args) {
String str = "需要寫入的字";
String fileName = "D:\\a\\a.xml";
OutputStream output = null;// 輸出字節(jié)流
OutputStreamWriter outputWrite = null;// 輸出字符流
PrintWriter print = null;// 輸出緩沖區(qū)
try {
output = new FileOutputStream(fileName);
outputWrite = new OutputStreamWriter(output);
print = new PrintWriter(outputWrite);
print.print(str);
print.flush();// 一定不要忘記此句,否則數(shù)據(jù)有可能不能被寫入文件
output.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
再一個(gè)問題就是只要你編碼正確就可以正常打開。
txt文件格式如下,采用的是一個(gè)服務(wù)端tomcat的log日志
[06/Mar/2013:18:43:55]?110.110.1.1?GET?/rest/keyword/getallids/aid/3/os/android2.3.5/ver/1.0/?HTTP/1.1?200?210?1453?1.453?'Dalvik/1.4.0?(Linux;?U;?Android?2.3.5;?T550?Build/MocorDroid2.3.5)'
[06/Mar/2013:18:43:55]?110.110.1.1?GET?/rest/keyword/getallids/aid/3/os/android2.3.4/ver/1.0/?HTTP/1.1?200?210?1453?1.453?'Dalvik/1.4.0?(Linux;?U;?Android?2.3.4;?LG-E730?Build/GRJ22)'
[06/Mar/2013:18:43:55]?110.110.1.1?GET?/rest/keyword/getallids/aid/3/os/android2.3.5/ver/1.0/?HTTP/1.1?200?210?1453?1.453?'Dalvik/1.4.0?(Linux;?U;?Android?2.3.5;?MI-ONE?Plus?Build/GINGERBREAD)'
[06/Mar/2013:18:43:55]?110.110.1.1?GET?/rest/keyword/getallids/aid/3/os/android2.3.6/ver/1.0/?HTTP/1.1?200?210?1453?1.453?'Dalvik/1.4.0?(Linux;?U;?Android?2.3.6;?GT-I9220?Build/GINGERBREAD)'
[06/Mar/2013:18:43:55]?110.110.1.1?POST?/rest/keyword/getallids/aid/3/os/android4.0.3/ver/1.0/?HTTP/1.1?200?210?1469?1.469?'Dalvik/1.6.0?(Linux;?U;?Android?4.0.3;?HUAWEI?C8812?Build/HuaweiC8812)'
[06/Mar/2013:18:43:55]?110.110.1.1?GET?/rest/keyword/getallids/aid/3/os/android2.2.2/ver/1.0/?HTTP/1.1?200?210?1453?1.453?'Dalvik/1.2.0?(Linux;?U;?Android?2.2.2;?HUAWEI?T8300?Build/FRF91)'
[06/Mar/2013:18:43:55]?110.110.1.1?GET?/rest/keyword/getallids/aid/3/os/android2.3.6/ver/1.0/?HTTP/1.0?200?210?1484?1.484?'Dalvik/1.4.0?(Linux;?U;?Android?2.3.6;?Mytel?Build/GRK39F)'
[06/Mar/2013:18:43:55]?110.110.1.1?POST?/rest/keyword/getallids/aid/3/os/android4.1.1/ver/1.0/?HTTP/1.1?200?210?1406?1.406?'Dalvik/1.6.0?(Linux;?U;?Android?4.1.1;?GT-N7100?Build/JRO03C)'
轉(zhuǎn)換代碼如下
package?test;
import?java.io.BufferedReader;
import?java.io.File;
import?java.io.FileReader;
import?java.io.FileWriter;
import?java.io.IOException;
import?java.io.Writer;
import?org.dom4j.Document;
import?org.dom4j.DocumentHelper;
import?org.dom4j.Element;
import?org.dom4j.io.XMLWriter;
public?class?promble04?{
@SuppressWarnings("resource")
public?static?void?txtToXML(String?txtPath)?throws?IOException
{
File?file?=?new?File(txtPath);
Document?document?=?DocumentHelper.createDocument();
Element?list?=?document.addElement("list");
BufferedReader?br?=?new?BufferedReader(new?FileReader(file));
String?line?=?br.readLine();
while(line!=null)
{
String?temp[]??=?line.split("?");
Element?log?=?list.addElement("log");
Element?time?=?log.addElement("time");
Element?ip?=?log.addElement("ip");
Element?httpType?=?log.addElement("httpType");
Element?url?=?log.addElement("url");
Element?httplevel?=?log.addElement("httplevel");
Element?linuxVersion?=?log.addElement("linuxVersion");
Element?os?=?log.addElement("os");
Element?AndroidVersion?=?log.addElement("AndroidVersion");
Element?phoneType?=?log.addElement("phoneType");
for(int?i?=?0;itemp.length;i++)
{
time.setText(temp[0]);
ip.setText(temp[1]);
httpType.setText(temp[2]);
url.setText(temp[3]);
httplevel.setText(temp[4]);
linuxVersion.setText(temp[9]);
os.setText(temp[10]);
AndroidVersion.setText(temp[12]);
phoneType.setText(temp[13]);
}
line?=?br.readLine();
}
Writer?filewriter?=?new?FileWriter("D:\\log\\test.xml");
XMLWriter?xmlWriter?=?new?XMLWriter(filewriter);
xmlWriter.write(document);
xmlWriter.close();
}
public?static?void?main(String?args[])?throws?IOException
{
promble04.txtToXML("D:\\log\\test.txt");
}
}
轉(zhuǎn)換結(jié)果如下
?xml?version="1.0"?encoding="UTF-8"?
list
log
time[06/Mar/2013:18:43:55]/time
ip110.110.1.1/ip
httpTypeGET/httpType
url/rest/keyword/getallids/aid/3/os/android2.3.5/ver/1.0//url
httplevelHTTP/1.1/httplevel
linuxVersion'Dalvik/1.4.0/linuxVersion
os(Linux;/os
AndroidVersionAndroid/AndroidVersion
phoneType2.3.5;/phoneType
/log
log
time[06/Mar/2013:18:43:55]/time
ip110.110.1.1/ip
httpTypeGET/httpType
url/rest/keyword/getallids/aid/3/os/android2.3.4/ver/1.0//url
httplevelHTTP/1.1/httplevel
linuxVersion'Dalvik/1.4.0/linuxVersion
os(Linux;/os
AndroidVersionAndroid/AndroidVersion
phoneType2.3.4;/phoneType
/log
log
time[06/Mar/2013:18:43:55]/time
ip110.110.1.1/ip
httpTypeGET/httpType
url/rest/keyword/getallids/aid/3/os/android2.3.5/ver/1.0//url
httplevelHTTP/1.1/httplevel
linuxVersion'Dalvik/1.4.0/linuxVersion
os(Linux;/os
AndroidVersionAndroid/AndroidVersion
phoneType2.3.5;/phoneType
/log
log
time[06/Mar/2013:18:43:55]/time
ip110.110.1.1/ip
httpTypeGET/httpType
url/rest/keyword/getallids/aid/3/os/android2.3.6/ver/1.0//url
httplevelHTTP/1.1/httplevel
linuxVersion'Dalvik/1.4.0/linuxVersion
os(Linux;/os
AndroidVersionAndroid/AndroidVersion
phoneType2.3.6;/phoneType
/log
log
time[06/Mar/2013:18:43:55]/time
ip110.110.1.1/ip
httpTypePOST/httpType
url/rest/keyword/getallids/aid/3/os/android4.0.3/ver/1.0//url
httplevelHTTP/1.1/httplevel
linuxVersion'Dalvik/1.6.0/linuxVersion
os(Linux;/os
AndroidVersionAndroid/AndroidVersion
phoneType4.0.3;/phoneType
/log
log
time[06/Mar/2013:18:43:55]/time
ip110.110.1.1/ip
httpTypeGET/httpType
url/rest/keyword/getallids/aid/3/os/android2.2.2/ver/1.0//url
httplevelHTTP/1.1/httplevel
linuxVersion'Dalvik/1.2.0/linuxVersion
os(Linux;/os
AndroidVersionAndroid/AndroidVersion
phoneType2.2.2;/phoneType
/log
log
time[06/Mar/2013:18:43:55]/time
ip110.110.1.1/ip
httpTypeGET/httpType
url/rest/keyword/getallids/aid/3/os/android2.3.6/ver/1.0//url
httplevelHTTP/1.0/httplevel
linuxVersion'Dalvik/1.4.0/linuxVersion
os(Linux;/os
AndroidVersionAndroid/AndroidVersion
phoneType2.3.6;/phoneType
/log
log
time[06/Mar/2013:18:43:55]/time
ip110.110.1.1/ip
httpTypePOST/httpType
url/rest/keyword/getallids/aid/3/os/android4.1.1/ver/1.0//url
httplevelHTTP/1.1/httplevel
linuxVersion'Dalvik/1.6.0/linuxVersion
os(Linux;/os
AndroidVersionAndroid/AndroidVersion
phoneType4.1.1;/phoneType
/log
/list
import java.io.*;
public class FileExample{
public static void main(String args[])
{
try
{
char str[]=new char[20];
FileReader f1=new FileReader("a.txt");
FileWriter f2=new FileWriter("b.txt");
f1.read(str);
f2.write(str);
f2.close();
f1.close();
}
catch(Exception e)
{
}
}
}
你確定有這個(gè)文件嗎?為什么我試了下都是可以的啊,是不是你的編碼有問題不支持中文的啊
你換個(gè)英文的文件名稱試試可以不可以??词遣皇侵形木幋a的問題。如果是的 你是是修改下屬性
filename = new String(filename.getBytes("GBK"), "iso8859-1");//文件名要轉(zhuǎn)一下編碼。
再試試 希望能幫到你!