本文目錄一覽:
- 1、用Java編寫簡單文件管理類
- 2、用JAVA製作一個文件系統管理器文件目錄瀏覽,創建目錄,移動文件,文件改名,文件刪除等等功能
- 3、求Java通訊錄管理程序,要編譯好的。要求數據存在文本文件中,功能包括聯繫人管理(新增,刪除,修改
- 4、求一個簡單的用java語言編寫的文件管理器的源代碼?
- 5、最簡單的java程序
- 6、急求Java簡單文件管理類程序
用Java編寫簡單文件管理類
java.io包里有很多與文件有關的類,可以很容易地實現文件的創建、刪除等基本操作,建議樓主下載一個JDK API 1.6.0的幫助文檔看看,裡面有詳細的介紹
用JAVA製作一個文件系統管理器文件目錄瀏覽,創建目錄,移動文件,文件改名,文件刪除等等功能
/**
* 取得當前目錄下文件對象
* @return
*/
public static Iterator getFiles(File currentFile) {
Vector vector = new Vector();
File afile[] = currentFile.listFiles();
for (int i = 0; i afile.length; i++)
if (afile[i].isFile())
vector.add(afile[i]);
return vector.iterator();
}
/**
* 取得當前目錄下的子目錄對象列表
* @return
*/
public static Iterator getAllDirectories(File currentFile) {
Vector vector = new Vector();
File afile[] = currentFile.listFiles();
//vector.add(new File(documentRoot, relativeFile + File.separator + “.”));
try {
vector.add(new File(currentFile.getCanonicalFile() + File.separator + “..”));
for (int i = 0; i afile.length; i++)
if (afile[i].isDirectory())
vector.add(afile[i]);
} catch (IOException e) {
if (log.isErrorEnabled()) log.error(e);
}
return vector.iterator();
}
/**
* 取得當前目錄下的子目錄對象列表
* @return
*/
public static Iterator getDirectories(File currentFile) {
Vector vector = new Vector();
File afile[] = currentFile.listFiles();
for (int i = 0; i afile.length; i++)
if (afile[i].isDirectory())
vector.add(afile[i]);
return vector.iterator();
}
/**
* 將內容寫入文件
* @param file
* @param content
* @throws IOException
*/
public static void writeFile(String file, String content)
throws IOException {
PrintWriter printwriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),BIND_ENCODING)));
printwriter.write(content);
printwriter.close();
if (printwriter.checkError())
if (log.isErrorEnabled())
log.error(“Error encountered while writing the file!”);
else
return;
}
代碼太長,分批發.
求Java通訊錄管理程序,要編譯好的。要求數據存在文本文件中,功能包括聯繫人管理(新增,刪除,修改
#includestdio.h /*I/O函數*/
#includeprocess.h/*包含exit函數*/
#includestring.h/*字符串函數*/
struct person/*定義一個結構體,結構體內元素為數組*/
{
char name[10];/*姓名*/
char number[15];/*學號*/
char tel[15];/*電話*/
char addr[30];/*地址*/
};
char filename[12];
FILE *fp;/*定義一個指針*/
void creat();/*創建一個通訊簿*/
void output();/*輸出通訊錄中所含資料*/
void append();/*添加函數*/
void search();/*查找函數*/
void Delete();/*刪除函數*/
void modify();/*修改函數*/
/*以下是主函數*/
main()
{
int m;/*定義一個整數*/
creat();
while(1)/*括號中為1代表無限循環*/
{
printf(“\n\n添加同學地址,請按1”);
printf(“\n查找同學地址,請按2”);
printf(“\n修改同學地址,請按3”);
printf(“\n刪除原來地址,請按4”);
printf(“\n輸出所有地址,請按5”);
printf(“\n退出本通訊錄,請按0\n”);
scanf(“%d”,m);
if(m=0m=5)
{
switch(m)/*調用主菜單函數,返回值整數作開關語句的條件*/
{
case 1: append();/*往通訊錄中添加*/
break;
case 2: search();/*在通訊錄中查找*/
break;
case 3: modify();/*修改通訊錄中資料*/
break;
case 4: Delete();/*刪除通訊錄中資料*/
break;
case 5: output();/*輸出通訊錄中所有名單*/
break;
case 0: exit(0);/*退出運行程序*/
}
printf(“\n\n操作完畢,請再次選擇!”);
}
else
printf(“\n\n選擇錯誤,請再次選擇!”);
}
}
void output()
{
struct person one;
if((fp=fopen(filename,”r”))==NULL)/*用輸入打開一個文本文*/
{
printf(“\n不能打開通訊簿!”);
exit(0);
}
printf(“\n\n%12s\n”,”通 訊 簿”);
while(!feof(fp))/*檢驗fp所指文件是否結束,此為一個循環語句*/
{
fscanf(fp,”%s%s%s%s\n”,one.name,one.number,one.tel,one.addr);/*從fp所指文件中讀出數據*/ printf(“\n%-10s%-15s%-15s%-30s\n”,one.name,one.number,one.tel,one.addr);/*輸出上面讀出數據*/
}
fclose(fp);/*關閉所指文件,釋放文件緩衝區,並返回值*/
}
/*****************添加函數*******************************/
void append()
{
struct person one;
if((fp=fopen(filename,”a”))==NULL)/*向二進制文本尾追加數據*/
{
printf(“\n不能打開通訊簿!”);
exit(0);
}
printf(“\n請輸入添加的姓名、電話號碼及住址\n”);
求一個簡單的用java語言編寫的文件管理器的源代碼?
public class complie {
int i,j;
public complie(int i,int j)//構建一個複數類
{
this.i=i;
this.j=j;
}
complie add(complie c)//複數加法
{
int l,k;
l=c.i+i;
k=c.j+j;
return (new complie(l,k));
}
complie cut(complie c)//複數減法
{
int l,k;
l=i-c.i;
k=j-c.j;
return (new complie(l,k));
}
void ToString()//將複數輸出
{
System.out.println(“複數為:”+i+”+”+j+”i”);
}
public static void main(String[] args)
{
complie a=new complie(4,5);
complie b=new complie(2,3);
System.out.println(“構造的複數類為:”);
a.ToString();
b.ToString();
System.out.println(“運算複數a+b=:”);
a.add(b).ToString();
System.out.println(“運算複數a-b=:”);
a.cut(b).ToString();
}
}
最簡單的java程序
public static void mainString args[]這個地方寫錯了,括號不是尖括號,
public static void main(String args[])
急求Java簡單文件管理類程序
這是別人寫好的
package sunnykid.file;
import java.io.*;
import sunnykid.text.SunnykidNumber;
/**
* p標題: JAVA文件操作工具類/p
* br
* p描述: 陽光軟體工作室常用工具包/p
* br
* p版權: 版權所有 (c) 2007/p
* br
* p組織: 陽光軟體工作室/p
*
* @author 鍾曉籟
* @version V1.0
*/
public class FileOperator {
/**
* 不帶參數的構造函數
*/
public FileOperator() {
super();
}
/**
* 刪除指定的文件
* @param filepath String 待刪除的文件路徑及名稱
* @throws IOException
*/
public void delete(String filepath) throws IOException {
Runtime rt = Runtime.getRuntime();
rt.exec(“cmd /c del ” + filepath);
}
/**
* 將字符串寫入文件
* @param content String 待寫入的字符串內容
* @param filepath String 待寫入的文件路徑及名稱
* @throws IOException
*/
public void write(String content, String filepath) throws IOException {
File file = new File(filepath);
FileWriter fw = new FileWriter(file);
fw.write(content);
fw.close();
}
/**
* 讀取文件中的內容
* @param filepath String 待讀取的文件路徑及名稱
* @return String 返回從文件中讀取的字符串內容
* @throws IOException
*/
public String read(String filepath) throws IOException {
int text = 0;
File file = new File(filepath);
FileReader fr = new FileReader(file);
int len = (int) file.length();
char[] buffer = new char[len];
while (fr.ready()) {
text = text + fr.read(buffer, text, len – text);
}
fr.close();
String content = new String(buffer, 0, text);
return content;
}
/**
* 判斷一個文件是否存在
* @param filepath String 待判斷的文件路徑及名稱
* @return boolean 返迴文件是否存在結果
*/
public boolean isExist(String filepath) {
File file = new File(filepath);
if (file.exists()) {
return true;
} else {
return false;
}
}
/**
* 重命名文件或目錄
* @param oldname String 重命名前的文件或目錄名稱
* @param newname String 重命名後的文件或目錄名稱
* @return boolean 返回操作是否成功結果
*/
public boolean rename(String oldname, String newname) {
File oldfile = new File(oldname);
File newfile = new File(newname);
boolean success = oldfile.renameTo(newfile);
return success;
}
/**
* 剪切指定文件至指定的目錄
* @param from String 源文件的路徑及名稱
* @param to String 目標路徑及名稱
*/
public void move(String from, String to) {
File oldfile = new File(from);
File newfile = new File(to);
oldfile.renameTo(newfile);
}
/**
* 拷貝指定文件至指定的目錄
* @param from String 源文件的路徑及名稱
* @param to String 目標路徑及名稱
* @throws IOException
*/
public void copy(String from, String to) throws IOException {
int BUFF_SIZE = 100000;
byte[] buffer = new byte[BUFF_SIZE];
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(from);
out = new FileOutputStream(to);
while (true) {
synchronized (buffer) {
int amountRead = in.read(buffer);
if (amountRead 0) {
break;
}
out.write(buffer, 0, amountRead);
}
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
/**
* 獲取文件擴展名
* @param filename String 需要獲取大小的文件之完整路徑
* @return String 返迴文件擴展名
*/
public String getExtension(String filename) {
String defExt = null;
if ((filename != null) (filename.length() 0)) {
int i = filename.lastIndexOf(‘.’);
if ((i 0) (i (filename.length() – 1))) {
defExt = filename.substring(i + 1);
}
}
return defExt;
}
/**
* 獲取文件字節數
* @param filename String 需要獲取大小的文件之完整路徑
* @return long 返迴文件大小字節數
*/
public long fileSize(String filename) {
long size = 0L;
File file = new File(filename);
if (this.isExist(filename) == true) {
size = file.length();
}
return size;
}
/**
* 獲取標準單位之文件大小
* @param bytesize long 需要轉換為標準單位的文件之字節數
* @return String 返回標準單位之文件大小
*/
public String switchSize(long bytesize) {
String size = “”;
SunnykidNumber sn=new SunnykidNumber();
float number = 0.0f;
if (bytesize = 0) {
size = “0Bytes”;
} else if (bytesize 1024) {
size = String.valueOf(size) + “Bytes”;
} else if (bytesize 1048576) {
number = (float) bytesize / 1024;
size = sn.parseCurrency(number) + “KB”;
} else if (bytesize 1073741824) {
number = (float) bytesize / 1024 / 1024;
size = sn.parseCurrency(number) + “MB”;
} else if (bytesize 1099511627776L) {
number = (float) bytesize / 1024 / 1024 / 1024;
size = sn.parseCurrency(number) + “GB”;
}
return size;
}
}
====================
package sunnykid.text;
import java.text.*;
/**
* p標題: 用於操作數字的類/p
* br
* p描述: 陽光軟體工作室常用工具包/p
* br
* p版權: 版權所有 (c) 2007/p
* br
* p組織: 陽光軟體工作室/p
*
* @author 鍾曉籟
* @version V1.0
*/
public class SunnykidNumber {
/**
* 不帶參數的構造函數
*/
public SunnykidNumber() {
super();
}
/**
* 將數字格式化成貨幣樣式
* @param unfmt_dbl double 未經格式化的數字
* @return String 返回按照貨幣樣式格式化後的字符串
*/
public String getCurrency(double unfmt_dbl) { //雙精度數轉化成貨幣類型兩位小數
NumberFormat nf = NumberFormat.getCurrencyInstance(); //按照貨幣類型格式化數字
String fmted_str = nf.format(unfmt_dbl);
return fmted_str;
}
/**
* 按照貨幣類型格式化數字
* @param unfmt_dbl double 未經格式化的數字
* @return String 返回按照貨幣類型格式化後的字符串
*/
public String parseCurrency(double unfmt_dbl) { //雙精度數轉化成貨幣類型兩位小數的字符串
DecimalFormat df = new DecimalFormat(“#.00”); //按照貨幣類型格式化數字
String fmted_str = df.format(unfmt_dbl);
return fmted_str;
}
/**
* 雙精度小數轉化成百分數
* @param unfmt_dbl double 未經格式化的數字
* @return String 返回按照百分比樣式格式化後的字符串
*/
public String parsePercect(double unfmt_dbl) { //雙精度小數轉化成百分數
NumberFormat nf = NumberFormat.getPercentInstance(); //按照百分比格式化數字
// nf.setMinimumIntegerDigits(integ);// 設置數的整數部分所允許的最大位數
// nf.setMaximumFractionDigits(fract);// 設置數的小數部分所允許的最大位數
String fmted_str = nf.format(unfmt_dbl);
return fmted_str;
}
/**
* 雙精度小數四捨五入為整數
* @param unfmt_dbl double 未經轉化的小數
* @return int 小數四舍後得到的整數
*/
public int roundNumber(double unfmt_dbl) {
String temp_str = String.valueOf(unfmt_dbl); //將小數轉化為字符串
if (temp_str.indexOf(“.”) = 0) {
}
int indexOfDot = temp_str.indexOf(“.”); //獲取小數點位置
int temp_int = Integer.parseInt(temp_str.substring(indexOfDot + 1,
indexOfDot + 2));
if (temp_int 5) { //判斷小數點後一位的數字是否大於5
return Integer.parseInt(temp_str.substring(0, indexOfDot)); //四舍
} else {
return Integer.parseInt(temp_str.substring(0, indexOfDot)) + 1; //五入
}
}
}
原創文章,作者:RPDFK,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/315722.html