資源簡介
java聊天室,帶有可執(zhí)行文件,源代碼和效果圖

代碼片段和文件信息
package?base.io;
import?java.io.BufferedReader;
import?java.io.BufferedWriter;
import?java.io.File;
import?java.io.FileNotFoundException;
import?java.io.FileWriter;
import?java.io.FileReader;
import?java.io.IOException;
import?java.util.List;
public?class?FileManager?{?
public?static?final?String?LAST_CHAR=“\n“;
//寫文件?參數(shù)(絕對路徑內(nèi)容是否創(chuàng)建是否追加)
public?static?void?write(String?path?String?contentboolean?isCreateboolean?isAppend)?{
try?{
//判斷路徑是否存在
File?file=new?File(getShortPath(path));
if(!file.exists()){
if(isCreate)
file.mkdir();
else
throw?new?java.io.FileNotFoundException(path);
}
//如果是否存在文件是否向后追加
File?file1=new?File(path);
if(!file1.exists()&&!isCreate){
throw?new?java.io.FileNotFoundException(path);
}
if(file1.exists()&&isAppend){
content=read(path)+content;
}
//開始寫文件
FileWriter?fw?=?new?FileWriter(path);
BufferedWriter?bw?=?new?BufferedWriter(fw);
String?array[]?=?content.split(LAST_CHAR);
for?(int?i?=?0;?i? bw.write(array[i]);
bw.newLine();
}
bw.flush();
bw.close();
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
// 寫文件?參數(shù)(絕對路徑內(nèi)容(集合)是否創(chuàng)建是否追加)
public?static?void?write(String?path?List?listboolean?isCreateboolean?isAppend)?{
StringBuffer?buffer=new?StringBuffer();
for(object?obj:list){
buffer.append(obj+“\n“);
}
write(pathbuffer.toString()isCreateisAppend);
}
// 讀日志,根據(jù)傳進(jìn)來的時(shí)間
public?static?String?read(String?path)?throws?FileNotFoundException?{
File?file=new?File(path);
//判斷要讀取的文件是否存在
if(!file.exists()){
throw?new?java.io.FileNotFoundException(path);
}
StringBuffer?result?=?new?StringBuffer();
try?{
FileReader?fr?=?new?FileReader(path);
BufferedReader?br?=?new?BufferedReader(fr);
String?temp?=?null;
do?{
temp?=?br.readLine();
if?(temp?!=?null){
result.append(temp+LAST_CHAR);
}
}?while?(temp?!=?null);
fr.close();
br.close();
}?catch?(IOException?ex)?{
ex.printStackTrace();
}
return?result.toString().replaceAll(LAST_CHAR+LAST_CHARLAST_CHAR);
}
//獲取不帶路徑的文件名(兩種方法)
public?static?String?getShortName(String?pathboolean?isMySelf){
return?isMySelf?path.substring(path.lastIndexOf(“\\“)+1path.length()):?new?File(path).getName();
}
public?static?String?getExtendName(String?path)
????{
???????return?(path.substring(path.lastIndexOf(“.“)+1path.length())).toLowerCase();
????}
//獲取不帶文件名的路徑
public?static?String?getShortPath(String?path){
return?path.substring(0path.lastIndexOf(“\\“)+1);
}
public?static?void?main(String?args[]){
try?{
System.out.println(“文件夾路徑:“+getShortPath(“C:\\temp\\a.txt“));
System.out.println(“文件名1:“+getShortName(“C:\\temp\\a.txt“false));
System.out.println(“文件名2:“+getShortName(“C:\\temp\\a.txt“true));
System.out.println(“擴(kuò)展名:“+getExtendName(“C:\\temp\\a.txt“));
write(“C:\\temp\\a.txt“?“大家好我正在學(xué)習(xí)
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件??????68489??2007-03-27?09:48??Java聊天室\可執(zhí)行程序(Jar)\ChatSystemClient_fat.jar
?????文件??????42539??2007-03-27?09:32??Java聊天室\可執(zhí)行程序(Jar)\ChatSystemServer_fat.jar
?????文件????????226??2007-01-10?15:23??Java聊天室\源代碼(Eclipse工程)\ba
?????文件????????326??2007-03-26?09:12??Java聊天室\源代碼(Eclipse工程)\ba
?????文件????????387??2007-01-10?15:23??Java聊天室\源代碼(Eclipse工程)\ba
?????文件???????4384??2010-03-23?16:51??Java聊天室\源代碼(Eclipse工程)\ba
?????文件???????3311??2007-03-23?10:00??Java聊天室\源代碼(Eclipse工程)\ba
?????文件???????3205??2010-03-23?16:51??Java聊天室\源代碼(Eclipse工程)\ba
?????文件???????2324??2007-03-19?17:34??Java聊天室\源代碼(Eclipse工程)\ba
?????文件???????1299??2010-03-23?16:51??Java聊天室\源代碼(Eclipse工程)\ba
?????文件???????9009??2010-03-23?16:51??Java聊天室\源代碼(Eclipse工程)\ba
?????文件???????3395??2010-03-23?16:51??Java聊天室\源代碼(Eclipse工程)\ba
?????文件??????10536??2007-03-23?09:57??Java聊天室\源代碼(Eclipse工程)\ba
?????文件???????1182??2010-03-23?16:51??Java聊天室\源代碼(Eclipse工程)\ba
?????文件????????809??2007-03-23?09:58??Java聊天室\源代碼(Eclipse工程)\ba
?????文件???????4423??2010-03-23?16:51??Java聊天室\源代碼(Eclipse工程)\ba
?????文件???????4778??2007-03-23?09:58??Java聊天室\源代碼(Eclipse工程)\ba
?????文件???????3297??2010-03-23?16:51??Java聊天室\源代碼(Eclipse工程)\ba
?????文件???????2321??2007-01-12?10:02??Java聊天室\源代碼(Eclipse工程)\ba
?????文件??????27210??2007-03-26?09:12??Java聊天室\源代碼(Eclipse工程)\ba
?????文件????????303??2010-03-23?17:23??Java聊天室\源代碼(Eclipse工程)\ChatSystemClient\.classpath
?????文件????????927??2007-03-26?16:47??Java聊天室\源代碼(Eclipse工程)\ChatSystemClient\.fatjar
?????文件????????392??2007-01-05?13:35??Java聊天室\源代碼(Eclipse工程)\ChatSystemClient\.project
?????文件????????629??2010-03-23?16:26??Java聊天室\源代碼(Eclipse工程)\ChatSystemClient\.settings\org.eclipse.jdt.core.prefs
?????文件????????675??2010-03-23?17:23??Java聊天室\源代碼(Eclipse工程)\ChatSystemClient\bin\com\chat\client\About$1.class
?????文件???????2086??2010-03-23?17:23??Java聊天室\源代碼(Eclipse工程)\ChatSystemClient\bin\com\chat\client\About.class
?????文件????????847??2010-03-23?17:23??Java聊天室\源代碼(Eclipse工程)\ChatSystemClient\bin\com\chat\client\Clientfr
?????文件????????780??2010-03-23?17:23??Java聊天室\源代碼(Eclipse工程)\ChatSystemClient\bin\com\chat\client\Clientfr
?????文件????????780??2010-03-23?17:23??Java聊天室\源代碼(Eclipse工程)\ChatSystemClient\bin\com\chat\client\Clientfr
?????文件???????1288??2010-03-23?17:23??Java聊天室\源代碼(Eclipse工程)\ChatSystemClient\bin\com\chat\client\Clientfr
............此處省略163個(gè)文件信息
評論
共有 條評論