十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
public static void getFileSize(String path){
创新互联建站是一家专业从事成都网站设计、网站建设的网络公司。作为专业网站设计公司,创新互联建站依托的技术实力、以及多年的网站运营经验,为您提供专业的成都网站建设、成都全网营销及网站设计开发服务!
//传入文件路径
File file = new File(path);
//测试此文件是否存在
if(file.exists()){
//如果是文件夹
//这里只检测了文件夹中第一层 如果有需要 可以继续递归检测
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;// 输出字节流
OutputStreamWriter outputWrite = null;// 输出字符流
PrintWriter print = null;// 输出缓冲区
try {
output = new FileOutputStream(fileName);
outputWrite = new OutputStreamWriter(output);
print = new PrintWriter(outputWrite);
print.print(str);
print.flush();// 一定不要忘记此句,否则数据有可能不能被写入文件
output.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
再一个问题就是只要你编码正确就可以正常打开。
txt文件格式如下,采用的是一个服务端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)'
转换代码如下
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");
}
}
转换结果如下
?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)
{
}
}
}
你确定有这个文件吗?为什么我试了下都是可以的啊,是不是你的编码有问题不支持中文的啊
你换个英文的文件名称试试可以不可以。看是不是中文编码的问题。如果是的 你是是修改下属性
filename = new String(filename.getBytes("GBK"), "iso8859-1");//文件名要转一下编码。
再试试 希望能帮到你!