首页 > 编程知识 正文

急求java简单文件管理类程序,java实现文件管理器

时间:2023-12-27 15:09:11 阅读:323164 作者:BIVM

本文目录一览:

用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("nn添加同学地址,请按1"); 

   printf("n查找同学地址,请按2"); 

printf("n修改同学地址,请按3"); 

   printf("n删除原来地址,请按4"); 

   printf("n输出所有地址,请按5"); 

   printf("n退出本通讯录,请按0n"); 

   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("nn操作完毕,请再次选择!"); 

  } 

    else 

    printf("nn选择错误,请再次选择!"); 

 } 

void output() 

 { 

  struct person one; 

  if((fp=fopen(filename,"r"))==NULL)/*用输入打开一个文本文*/ 

   { 

    printf("n不能打开通讯簿!"); 

    exit(0); 

   } 

    printf("nn%12sn","通 讯 簿"); 

    while(!feof(fp))/*检验fp所指文件是否结束,此为一个循环语句*/ 

     { 

fscanf(fp,"%s%s%s%sn",one.name,one.number,one.tel,one.addr);/*从fp所指文件中读出数据*/      printf("n%-10s%-15s%-15s%-30sn",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; //五入

}

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。