- 浏览: 567970 次
- 性别:
- 来自: 南京
文章分类
- 全部博客 (350)
- Sybase (30)
- SQL SERVER2005 (14)
- 数据库 (27)
- SSH框架 (27)
- WebService (21)
- 下载-软件收藏 (15)
- 随笔-日常使用 (9)
- Flex 相关 (13)
- Linux (11)
- Web (64)
- XML相关 (9)
- Socket相关 (1)
- Elipse (3)
- 统计报表 (11)
- 线程相关 (3)
- Java相关 (37)
- JAVASCRIPT (19)
- JAVA反射 (3)
- JSP标签 (3)
- 随笔-其他 (2)
- 随笔-设计模式 (3)
- 随笔-架构师相关 (1)
- 下载-源码 (7)
- 下载-帮助文档 (1)
- 下载-插件 (6)
- 技术-.NET (2)
- 技术-Excel VBA (8)
- 应用-地图相关 (2)
- 应用-GSM短信猫 (5)
- 应用-单点登录 (3)
- Android相关 (3)
最新评论
-
sucheng2016:
发现jconn4.jar 里面有getBlob(String) ...
Sybase15驱动包的问题com.sybase.jdbc3.jdbc.SybDriver -
sucheng2016:
java.lang.UnsupportedOperationE ...
Sybase15驱动包的问题com.sybase.jdbc3.jdbc.SybDriver -
ok123zxx:
没下文了吗
通过 Tomcat Advanced I/O 获得高性能的 Ajax tocmat6+CometProcessor -
q1345111:
大家这个问题 尚未完成方法 com.sybase.jdbc3. ...
Sybase15驱动包的问题com.sybase.jdbc3.jdbc.SybDriver -
bdk82924:
heshujing217187 写道问题同1楼一样,求解!换j ...
Sybase15驱动包的问题com.sybase.jdbc3.jdbc.SybDriver
转自 http://www.iteye.com/topic/180807
一.获得控制台用户输入的信息
- /** *//**获得控制台用户输入的信息
- * @return
- * @throws IOException
- */
- public String getInputMessage() throws IOException...{
- System.out.println("请输入您的命令∶");
- byte buffer[]=new byte[1024];
- int count=System.in.read(buffer);
- char[] ch=new char[count-2];//最后两位为结束符,删去不要
- for(int i=0;i<count-2;i++)
- ch[i]=(char)buffer[i];
- String str=new String(ch);
- return str;
- }
/** *//**获得控制台用户输入的信息 * @return * @throws IOException */ public String getInputMessage() throws IOException...{ System.out.println("请输入您的命令∶"); byte buffer[]=new byte[1024]; int count=System.in.read(buffer); char[] ch=new char[count-2];//最后两位为结束符,删去不要 for(int i=0;i<count-2;i++) ch[i]=(char)buffer[i]; String str=new String(ch); return str; }
可以返回用户输入的信息,不足之处在于不支持中文输入,有待进一步改进。
二.复制文件
1.以文件流的方式复制文件
- /** *//**以文件流的方式复制文件
- * @param src 文件源目录
- * @param dest 文件目的目录
- * @throws IOException
- */
- public void copyFile(String src,String dest) throws IOException...{
- FileInputStream in=new FileInputStream(src);
- File file=new File(dest);
- if(!file.exists())
- file.createNewFile();
- FileOutputStream out=new FileOutputStream(file);
- int c;
- byte buffer[]=new byte[1024];
- while((c=in.read(buffer))!=-1)...{
- for(int i=0;i<c;i++)
- out.write(buffer[i]);
- }
- in.close();
- out.close();
- }
/** *//**以文件流的方式复制文件 * @param src 文件源目录 * @param dest 文件目的目录 * @throws IOException */ public void copyFile(String src,String dest) throws IOException...{ FileInputStream in=new FileInputStream(src); File file=new File(dest); if(!file.exists()) file.createNewFile(); FileOutputStream out=new FileOutputStream(file); int c; byte buffer[]=new byte[1024]; while((c=in.read(buffer))!=-1)...{ for(int i=0;i<c;i++) out.write(buffer[i]); } in.close(); out.close(); }
该方法经过测试,支持中文处理,并且可以复制多种类型,比如txt,xml,jpg,doc等多种格式
三.写文件
1.利用PrintStream写文件
- /** *//**
- * 文件输出示例
- */
- public void PrintStreamDemo()...{
- try ...{
- FileOutputStream out=new FileOutputStream("D:/test.txt");
- PrintStream p=new PrintStream(out);
- for(int i=0;i<10;i++)
- p.println("This is "+i+" line");
- } catch (FileNotFoundException e) ...{
- e.printStackTrace();
- }
- }
/** *//** * 文件输出示例 */ public void PrintStreamDemo()...{ try ...{ FileOutputStream out=new FileOutputStream("D:/test.txt"); PrintStream p=new PrintStream(out); for(int i=0;i<10;i++) p.println("This is "+i+" line"); } catch (FileNotFoundException e) ...{ e.printStackTrace(); } }
2.利用StringBuffer写文件
- public void StringBufferDemo() throws IOException......{
- File file=new File("/root/sms.log");
- if(!file.exists())
- file.createNewFile();
- FileOutputStream out=new FileOutputStream(file,true);
- for(int i=0;i<10000;i++)......{
- StringBuffer sb=new StringBuffer();
- sb.append("这是第"+i+"行:前面介绍的各种方法都不关用,为什么总是奇怪的问题 ");
- out.write(sb.toString().getBytes("utf-8"));
- }
- out.close();
- }
public void StringBufferDemo() throws IOException......{ File file=new File("/root/sms.log"); if(!file.exists()) file.createNewFile(); FileOutputStream out=new FileOutputStream(file,true); for(int i=0;i<10000;i++)......{ StringBuffer sb=new StringBuffer(); sb.append("这是第"+i+"行:前面介绍的各种方法都不关用,为什么总是奇怪的问题 "); out.write(sb.toString().getBytes("utf-8")); } out.close(); }
该方法可以设定使用何种编码,有效解决中文问题。
四.文件重命名
- /** *//**文件重命名
- * @param path 文件目录
- * @param oldname 原来的文件名
- * @param newname 新文件名
- */
- public void renameFile(String path,String oldname,String newname)...{
- if(!oldname.equals(newname))...{//新的文件名和以前文件名不同时,才有必要进行重命名
- File oldfile=new File(path+"/"+oldname);
- File newfile=new File(path+"/"+newname);
- if(newfile.exists())//若在该目录下已经有一个文件和新文件名相同,则不允许重命名
- System.out.println(newname+"已经存在!");
- else...{
- oldfile.renameTo(newfile);
- }
- }
- }
/** *//**文件重命名 * @param path 文件目录 * @param oldname 原来的文件名 * @param newname 新文件名 */ public void renameFile(String path,String oldname,String newname)...{ if(!oldname.equals(newname))...{//新的文件名和以前文件名不同时,才有必要进行重命名 File oldfile=new File(path+"/"+oldname); File newfile=new File(path+"/"+newname); if(newfile.exists())//若在该目录下已经有一个文件和新文件名相同,则不允许重命名 System.out.println(newname+"已经存在!"); else...{ oldfile.renameTo(newfile); } } }
五.转移文件目录
转移文件目录不等同于复制文件,复制文件是复制后两个目录都存在该文件,而转移文件目录则是转移后,只有新目录中存在该文件。
- /** *//**转移文件目录
- * @param filename 文件名
- * @param oldpath 旧目录
- * @param newpath 新目录
- * @param cover 若新目录下存在和转移文件具有相同文件名的文件时,是否覆盖新目录下文件,cover=true将会覆盖原文件,否则不操作
- */
- public void changeDirectory(String filename,String oldpath,String newpath,boolean cover)...{
- if(!oldpath.equals(newpath))...{
- File oldfile=new File(oldpath+"/"+filename);
- File newfile=new File(newpath+"/"+filename);
- if(newfile.exists())...{//若在待转移目录下,已经存在待转移文件
- if(cover)//覆盖
- oldfile.renameTo(newfile);
- else
- System.out.println("在新目录下已经存在:"+filename);
- }
- else...{
- oldfile.renameTo(newfile);
- }
- }
- }
/** *//**转移文件目录 * @param filename 文件名 * @param oldpath 旧目录 * @param newpath 新目录 * @param cover 若新目录下存在和转移文件具有相同文件名的文件时,是否覆盖新目录下文件,cover=true将会覆盖原文件,否则不操作 */ public void changeDirectory(String filename,String oldpath,String newpath,boolean cover)...{ if(!oldpath.equals(newpath))...{ File oldfile=new File(oldpath+"/"+filename); File newfile=new File(newpath+"/"+filename); if(newfile.exists())...{//若在待转移目录下,已经存在待转移文件 if(cover)//覆盖 oldfile.renameTo(newfile); else System.out.println("在新目录下已经存在:"+filename); } else...{ oldfile.renameTo(newfile); } } }
六.读文件
1.利用FileInputStream读取文件
2.利用BufferedReader读取
在IO操作,利用BufferedReader和BufferedWriter效率会更高一点
3.利用dom4j读取xml文件
- /** *//**从目录中读取xml文件
- * @param path 文件目录
- * @return
- * @throws DocumentException
- * @throws IOException
- */
- public Document readXml(String path) throws DocumentException, IOException...{
- File file=new File(path);
- BufferedReader bufferedreader = new BufferedReader(new FileReader(file));
- SAXReader saxreader = new SAXReader();
- Document document = (Document)saxreader.read(bufferedreader);
- bufferedreader.close();
- return document;
- }
/** *//**从目录中读取xml文件 * @param path 文件目录 * @return * @throws DocumentException * @throws IOException */ public Document readXml(String path) throws DocumentException, IOException...{ File file=new File(path); BufferedReader bufferedreader = new BufferedReader(new FileReader(file)); SAXReader saxreader = new SAXReader(); Document document = (Document)saxreader.read(bufferedreader); bufferedreader.close(); return document; }
七.创建文件(文件夹)
1.创建文件夹
- /** *//**创建文件夹
- * @param path 目录
- */
- public void createDir(String path)...{
- File dir=new File(path);
- if(!dir.exists())
- dir.mkdir();
- }
/** *//**创建文件夹 * @param path 目录 */ public void createDir(String path)...{ File dir=new File(path); if(!dir.exists()) dir.mkdir(); }
2.创建新文件
- /** *//**创建新文件
- * @param path 目录
- * @param filename 文件名
- * @throws IOException
- */
- public void createFile(String path,String filename) throws IOException...{
- File file=new File(path+"/"+filename);
- if(!file.exists())
- file.createNewFile();
- }
/** *//**创建新文件 * @param path 目录 * @param filename 文件名 * @throws IOException */ public void createFile(String path,String filename) throws IOException...{ File file=new File(path+"/"+filename); if(!file.exists()) file.createNewFile(); }
八.删除文件(目录)
1.删除文件
- /** *//**删除文件
- * @param path 目录
- * @param filename 文件名
- */
- public void delFile(String path,String filename)...{
- File file=new File(path+"/"+filename);
- if(file.exists()&&file.isFile())
- file.delete();
- }
/** *//**删除文件 * @param path 目录 * @param filename 文件名 */ public void delFile(String path,String filename)...{ File file=new File(path+"/"+filename); if(file.exists()&&file.isFile()) file.delete(); }
2.删除目录
要利用File类的delete()方法删除目录时,必须保证该目录下没有文件或者子目录,否则删除失败,因此在实际应用中,我们要删除目录,必须利用递归删除该目录下的所有子目录和文件,然后再删除该目录。
- /** *//**递归删除文件夹
- * @param path
- */
- public void delDir(String path)...{
- File dir=new File(path);
- if(dir.exists())...{
- File[] tmp=dir.listFiles();
- for(int i=0;i<tmp.length;i++)...{
- if(tmp[i].isDirectory())...{
- delDir(path+"/"+tmp[i].getName());
- }
- else...{
- tmp[i].delete();
- }
- }
- dir.delete();
- }
- }
/** *//**递归删除文件夹 * @param path */ public void delDir(String path)...{ File dir=new File(path); if(dir.exists())...{ File[] tmp=dir.listFiles(); for(int i=0;i<tmp.length;i++)...{ if(tmp[i].isDirectory())...{ delDir(path+"/"+tmp[i].getName()); } else...{ tmp[i].delete(); } } dir.delete(); } }
3、读文件
- /** *//**读文件
- * @param path
- * @return
- * @throws IOException
- */
- public String BufferedReaderDemo(String path) throws IOException...{
- File file=new File(path);
- if(!file.exists()||file.isDirectory())
- throw new FileNotFoundException();
- BufferedReader br=new BufferedReader(new FileReader(file));
- String temp=null;
- StringBuffer sb=new StringBuffer();
- temp=br.readLine();
- while(temp!=null)...{
- sb.append(temp+" ");
- temp=br.readLine();
- }
- return sb.toString();
- }
/** *//**读文件 * @param path * @return * @throws IOException */ public String BufferedReaderDemo(String path) throws IOException...{ File file=new File(path); if(!file.exists()||file.isDirectory()) throw new FileNotFoundException(); BufferedReader br=new BufferedReader(new FileReader(file)); String temp=null; StringBuffer sb=new StringBuffer(); temp=br.readLine(); while(temp!=null)...{ sb.append(temp+" "); temp=br.readLine(); } return sb.toString(); }
- /** *//**读文件
- * @param path
- * @return
- * @throws IOException
- */
- public String FileInputStreamDemo(String path) throws IOException...{
- File file=new File(path);
- if(!file.exists()||file.isDirectory())
- throw new FileNotFoundException();
- FileInputStream fis=new FileInputStream(file);
- byte[] buf = new byte[1024];
- StringBuffer sb=new StringBuffer();
- while((fis.read(buf))!=-1)...{
- sb.append(new String(buf));
- buf=new byte[1024];//重新生成,避免和上次读取的数据重复
- }
- return sb.toString();
- }
/** *//**读文件 * @param path * @return * @throws IOException */ public String FileInputStreamDemo(String path) throws IOException...{ File file=new File(path); if(!file.exists()||file.isDirectory()) throw new FileNotFoundException(); FileInputStream fis=new FileInputStream(file); byte[] buf = new byte[1024]; StringBuffer sb=new StringBuffer(); while((fis.read(buf))!=-1)...{ sb.append(new String(buf)); buf=new byte[1024];//重新生成,避免和上次读取的数据重复 } return sb.toString(); }
/** * * 按行读取文件文件 ,防止中文乱码 * * @param fileName * @author bdk * @date 2011-7-20 上午02:20:14 */ public static void read(String fileName) { File file = null; InputStreamReader read = null; BufferedReader br = null; try { file = new File(fileName); read = new InputStreamReader(new FileInputStream(file), "UTF-8"); br = new BufferedReader(read); String strIn = null; while ((strIn = br.readLine()) != null) { System.out.println(strIn); } } catch (Exception e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if (read != null) { try { read.close(); } catch (IOException e) { e.printStackTrace(); } } } }
写文件中文乱码
public static void main(String[] args) throws IOException { String s = "comunicação"; try { File f = new File("a.txt"); if (!f.exists()) { f.createNewFile(); } OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f), "UTF-8"); BufferedWriter writer = new BufferedWriter(write); writer.write(s); writer.close(); } catch (Exception e) { System.out.println("写文件内容操作出错"); e.printStackTrace(); } }
发表评论
-
Jav解析soap的xml
2015-05-17 21:13 808解析xml import org.dom4j.Docume ... -
Json转换利器Gson
2013-12-13 08:59 531转: http://blog.csdn.net/lk_blog ... -
asm jar包冲突的问题和解决方法(转)
2013-11-03 01:48 1169asm jar包冲突的问题和解决方法 在用Spring+Hi ... -
Spring ehCache 示例
2013-04-28 15:46 784http://blog.chinaunix.net/uid-2 ... -
将json字符串转换为bean (json-lib)
2013-04-26 15:26 1018Json-lib可以将Java对象转成json格式的字符串,也 ... -
QPID学习
2013-03-09 14:13 4453最近在看QPID 首先看下QPID是什么,他是个消息队列,用 ... -
短网址的原理和实现
2013-01-22 15:36 15390微博上经常会看到类似 http://t.cn/Afafhe 这 ... -
JAVA实现栈(stack)与堆(heap)
2013-01-11 17:22 929Java实现 栈(stack)与堆(heap) 上次写过一 ... -
Tomcat7.0下实现的websocket 代码
2013-01-07 19:37 1000测试环境: JDK1.6 Tomcat7.0.30 ... -
java IO写入文件效率——几种方法比较
2012-11-02 11:17 1090总结: 如果按字符和字节来分类,除方法1和2,其余都是按字符 ... -
Apache自带压力测试工具AB的使用方法
2012-09-18 11:26 1319使用例子: 1、打开dos界面,开始-》运行-》输入“cmd ... -
MyEclipse6.01注册码,Java源码
2012-07-02 16:12 1107不用为注册码犯愁了.. 下面是在网上搜索到的一段代码 ,分享给 ... -
ant解决OutOfMemoryError 或者Error starting modern compiler
2012-05-11 16:36 1405起因:在执行ant脚本的时候 ,报的错误是 Error st ... -
Java中使用Json 用到的jar包
2012-02-22 08:25 1392操作json开源的Jar包很多 ,那么多的jar选择哪个好呢 ... -
JsonUtils 类,将任意数据格式转换为Json格式
2012-02-21 09:12 1821package json; ... -
Java生成二维码或一维条形码(待续 未完)
2012-02-13 08:22 1772Java生成二维码或一维条形码(待续) 前段时间用了“ ... -
Java操作SVN(待续)
2012-02-02 08:31 1035一般大家都会直接使用snv客户端或者在eclipse中svn插 ... -
使用BeanUtils类简化request和resultset转换
2012-02-01 15:41 1098转:http://xdwangiflytek.iteye.co ... -
HttpClient 网络抓取
2012-01-20 10:08 1139利用 HttpClient 进行抓取 ... -
用Java实现按字节长度截取中英文数字字符串的方法总结 .
2011-12-20 16:59 1676转:http://blog.csdn.net/yanwudin ...
相关推荐
1 MFC文件操作大全 1.创建文件夹 CreateDirectory(%%1,NULL); 2.创建文件 CFile file; file.Open(%%1,CFile::modeCreate|CFile::modeWrite); 3.删除文件 DeleteFile(%%1); 4.删除文件夹 Remove...
Visual_C++_MFC文件操作大全
Java文件操作大全[汇编].pdf
根据提供的信息,我们可以总结出以下关于C#文件操作的关键知识点: ### 1. 创建文件夹 在C#中,可以通过`System.IO`命名空间中的`Directory.CreateDirectory`方法来创建一个新的文件夹。 ```csharp using System....
模拟实现采用二级目录结构的磁盘文件系统中的文件操作。 文件系统是操作系统中管理和存取信息的机构,它具有“按名存取”的功能,不仅方便用户,而且能提高系统效率且安全可靠。 在用户程序中可使用文件系统提供的...
操作系统实验四的核心目标是设计和实现一个简单的...通过这样的实验,学生能够深入理解文件系统如何管理磁盘空间,跟踪文件元数据,以及如何执行基本的文件操作,这对理解和设计更复杂的操作系统有着至关重要的作用。
一个简单的文件系统(操作系统课程设计)主要任务是对用户文件和系统文件进行管理,以方便用户使用,并保证文件的安全性。文件管理具有对文件存储空间的管理、目录管理、文件的读/写管理以及文件的共享与保护功能。...
linux版本和vc++6.0版本 含设计报告,流程图等。 设计一个简单的多用户文件系统。即 ①在系统中用一个文件来模拟一个磁盘;...基本上是进入一个界面(此界面就是该文件系统的界面)后,可以实现设计的操作要求。
CANoe/CAPL 文件操作脚本是用于自动化处理CANoe环境中的配置、数据记录和分析的编程工具。CANoe是一款广泛应用于汽车电子系统的诊断、测试和测量的软件,而CAPL(CANoe Application Programming Language)是CANoe内...
在本教程中,我们将重点讨论如何通过JNI在Android应用中进行文件操作。 首先,要使用JNI,我们需要在Java类中声明native方法。例如,我们可以声明一个名为`readFileFromNative`的方法: ```java public class ...
使用文件来模拟外存,进行数据结构设计和操作算法的设计,实现一个文件系统并实现基本的文件操作(为了简便文件系统,不考虑文件共享,文件系统安全以及管道文件与设备文件等特殊内容)。要求: 1、对程序的每一部分...
8. **应用场景**:此控件可以广泛应用于需要内置文件管理功能的应用程序中,如文本编辑器、图片查看器、音乐播放器等,让开发者不必从零开始构建文件操作功能。 综上所述,"C# 文件浏览操作控件"是一个强大的工具,...
在这个项目中,学生们被要求构建一个模拟的文件系统,该系统具备基本的文件操作功能,如读取、写入、创建和删除文件。这个设计不仅锻炼了学生的编程能力,也提升了他们对操作系统内核中文件系统机制的理解。 文件...
为了确保数据安全,文件系统需要具备基本的文件操作功能,如创建(create)、删除(delete)、打开(open)、关闭(close)、读取(read)和写入(write)。以下是这些功能的详细介绍以及系统设计的一些关键点: 1....
总的来说,这个大作业旨在通过实际编程,让学生掌握文件管理系统的基本概念、设计原则以及C++中进行文件操作的技巧,提升对操作系统核心功能的理解和应用能力。完成这样的项目不仅能够提升编程技能,也有助于理论...
杭电 操作系统课程设计 简单文件系统的实现 杭电 操作系统课程设计 简单文件系统的实现 杭电 操作系统课程设计 简单文件系统的实现
将一系列图片文件存储到MongoDB中 java操作mongodb存储文件
通过对具体的文件存储空间的管理、文件的物理结构、目录结构和文件操作的实现,加深对文件系统内部功能和实现过程的理解。 要求: 1.在内存中开辟一个虚拟磁盘空间作为文件存储器,在其上实现一个简单的单用户文件...
此外,还可以打开选择的 Excel 文件、设置当前工作表、读取各个单元格的数据、获得 sheet 表的数量、获得 sheet 表的名字、释放资源和关闭 Excel 等操作。 COM 接口是 Microsoft 的一项技术,允许不同的应用程序...
在VC++环境中进行PDF文件操作是一项常见的任务,尤其在开发桌面应用程序时,可能需要读取、编辑或生成PDF文档。本篇文章将详细讲解如何在VC++中实现这些功能,主要涉及的技术点包括PDF文件的基本概念、PDF库的使用...