zoukankan      html  css  js  c++  java
  • JAVA文件操作类

    最近开始在学习JAVA,从网上看了《JAVA自学之路》。

    JAVA自学之路
    1 一:J2SE 面向对象-封装、继承、多态
    2 内存的分析
    3 递归
    4 集合类、泛型、自动打包与解包、Annotation
    5 IO
    6 多线程、线程同步
    7 TCP/UDP
    8 AWT、事件模型、匿名类
    9 正则表达式
    10 反射机制
    11 2:数据库(Oracle或者MySQL)
    12 SQL语句
    13 多表连接,内外连接, 子查询等
    14 管理表、视图、索引、序列、约束等
    15 树状结构存储
    16 存储过程、触发器
    17 数据库设计三范式、
    18 3:JDBC
    19 JDBC基础
    20 连接池
    21 树状结构存储与展现
    22 DataSource & RowSet
    23 JDBC连接Oracle及MySQL
    24
    25 4:HTML_CSS_JAVASCRIPT
    26 html、css、javascript基础语法
    27 JavaScript Form判断
    28 Dom编程基础(事件处理等)
    29 JS常用效果如TreeView、下拉联动等
    30 JS学习方法
    31 JS调试方法
    32 DreamWeaver初步(建立HTML、Table、Form、CSS)等
    33 5:Servlet & JSP
    34 tomcat基础
    35 servlet基础
    36 web.xml配置基础
    37 web application的结构
    38 servlet生命周期
    39 request response等常用方法
    40 ServletContext类
    41 HTTP协议基础(GET POST)
    42 Cookie
    43 Session
    44 Application
    45 JSP的几种语法(包括JSTL等)注意在项目中练习,不要拘泥于语法细节而裹步不前。
    46 6:Struts
    47 多层架构理论
    48 Model 1 and Model 2
    49 Struts基本概念
    50 MVC
    51 Action与业务逻辑类的关系
    52 在Struts与JSP之间传递数据
    53 Struts处理流程(控制流)
    54 Struts TagLib(了解常用的)
    55 JSTL
    56 ActionForm
    57 字段收集
    58 上传文件
    59 类型转换
    60 DTO
    61 动态Action Form
    62 验证框架
    63 ActionForward 转发与重定向
    64 动态生成ActionForward
    65 全局与局部的ActionForward
    66 Action Forward Scope
    67 UnknownActionMapping
    68 Action的线程安全
    69 I18N
    70 如何切换语言环境
    71 Struts异常处理机制 程序处理 自动处理 自定义异常处理器
    72 Struts的多模块配置
    73
    74 7:XML
    75 (XML/XSL、XSLT/DTD、SCHEMA等基础的概念、关于Java的编程可以暂时扔在一边)
    76 8:Hibernate
    77 OR Mapping原理
    78 Hibernate基础开发步骤
    79 Hibernate基本接口(重点Session)
    80 普通属性映射
    81 关联关系映射
    82 Native SQL
    83 inverse lazy cascade
    84 继承关系映射
    85 HQL
    86 性能优化 一级缓存 二级缓存 查询缓存
    87 事务与并发 悲观锁、乐观锁
    88 OpenSessionInView
    89 CurrentSession
    90 (至于JTA、联合主键、自然主键、动态主键、Any类型 Creteria Queries Intercepter and Event 自定义类型等,可以暂时扔在一边)
    91 9:Spring
    92 IOC/DI
    93 Spring配置
    94 Spring架构
    95 AOP及Spring AOP
    96 声明式事务(AOP)
    97 Spring + Hibernate Spring支持Web
    98 Scope
    99 (其他的Spring模块对于自学来说可以暂时扔在一边)
    100 10:EJB3.0
    101 J2EE架构基础(JTA JMS等)
    102 EJB基础(地位及基本理论、分类等)
    103 Annotation
    104 Ant编译与部署EJB
    105 Session Bean
    106 EJB的依赖注入
    107 Persistence API
    108 (可以用JBoss学习EJB3.0)
    109
    110 11:至于SOA,对于自学的同学来说,暂时不用特别关注

    推荐把J2SE学通了,这个是学习J2ME、J2EE,甚至是学习Android的基础。

    下面是学习IO这章时写的文件操作类,主要也是参考了网上的资源http://zgqhyh.javaeye.com/blog/91333,博客里先存着以备后需。

    文件操作类
    1 package com.yuchao.filecopy;
    2
    3 import java.io.*;
    4 import java.util.StringTokenizer;
    5
    6 public class FileOperate {
    7 private String message;
    8 public FileOperate() {
    9 }
    10
    11 /**
    12 * 读取文本文件内容
    13 * @param filePathAndName 带有完整绝对路径的文件名
    14 * @param encoding 文本文件打开的编码方式
    15 * @return 返回文本文件的内容
    16 */
    17 public String readTxt(String filePathAndName,String encoding) throws IOException{
    18 encoding = encoding.trim();
    19 StringBuffer str = new StringBuffer("");
    20 String st = "";
    21 try{
    22 FileInputStream fs = new FileInputStream(filePathAndName);
    23 InputStreamReader isr;
    24 if(encoding.equals("")){
    25 isr = new InputStreamReader(fs);
    26 }else{
    27 isr = new InputStreamReader(fs,encoding);
    28 }
    29 BufferedReader br = new BufferedReader(isr);
    30 try{
    31 String data = "";
    32 while((data = br.readLine())!=null){
    33 str.append(data+" ");
    34 }
    35 }catch(Exception e){
    36 str.append(e.toString());
    37 }
    38 st = str.toString();
    39 }catch(IOException es){
    40 st = "";
    41 }
    42 return st;
    43 }
    44
    45 /**
    46 * 新建目录
    47 * @param folderPath 目录
    48 * @return 返回目录创建后的路径
    49 */
    50 public String createFolder(String folderPath) {
    51 String txt = folderPath;
    52 try {
    53 java.io.File myFilePath = new java.io.File(txt);
    54 txt = folderPath;
    55 if (!myFilePath.exists()) {
    56 myFilePath.mkdir();
    57 }
    58 }
    59 catch (Exception e) {
    60 message = "创建目录操作出错";
    61 }
    62 return txt;
    63 }
    64
    65 /**
    66 * 多级目录创建
    67 * @param folderPath 准备要在本级目录下创建新目录的目录路径 例如 c:myf
    68 * @param paths 无限级目录参数,各级目录以单数线区分 例如 a|b|c
    69 * @return 返回创建文件后的路径 例如 c:myfac
    70 */
    71 public String createFolders(String folderPath, String paths){
    72 String txts = folderPath;
    73 try{
    74 String txt;
    75 txts = folderPath;
    76 StringTokenizer st = new StringTokenizer(paths,"|");
    77 for(int i=0; st.hasMoreTokens(); i++){
    78 txt = st.nextToken().trim();
    79 if(txts.lastIndexOf("/")!=-1){
    80 txts = createFolder(txts+txt);
    81 }else{
    82 txts = createFolder(txts+txt+"/");
    83 }
    84 }
    85 }catch(Exception e){
    86 message = "创建目录操作出错!";
    87 }
    88 return txts;
    89 }
    90
    91
    92 /**
    93 * 新建文件
    94 * @param filePathAndName 文本文件完整绝对路径及文件名
    95 * @param fileContent 文本文件内容
    96 * @return
    97 */
    98 public void createFile(String filePathAndName, String fileContent) {
    99
    100 try {
    101 String filePath = filePathAndName;
    102 filePath = filePath.toString();
    103 File myFilePath = new File(filePath);
    104 if (!myFilePath.exists()) {
    105 myFilePath.createNewFile();
    106 }
    107 FileWriter resultFile = new FileWriter(myFilePath);
    108 PrintWriter myFile = new PrintWriter(resultFile);
    109 String strContent = fileContent;
    110 myFile.println(strContent);
    111 myFile.close();
    112 resultFile.close();
    113 }
    114 catch (Exception e) {
    115 message = "创建文件操作出错";
    116 }
    117 }
    118
    119
    120 /**
    121 * 有编码方式的文件创建
    122 * @param filePathAndName 文本文件完整绝对路径及文件名
    123 * @param fileContent 文本文件内容
    124 * @param encoding 编码方式 例如 GBK 或者 UTF-8
    125 * @return
    126 */
    127 public void createFile(String filePathAndName, String fileContent, String encoding) {
    128
    129 try {
    130 String filePath = filePathAndName;
    131 filePath = filePath.toString();
    132 File myFilePath = new File(filePath);
    133 if (!myFilePath.exists()) {
    134 myFilePath.createNewFile();
    135 }
    136 PrintWriter myFile = new PrintWriter(myFilePath,encoding);
    137 String strContent = fileContent;
    138 myFile.println(strContent);
    139 myFile.close();
    140 }
    141 catch (Exception e) {
    142 message = "创建文件操作出错";
    143 }
    144 }
    145
    146
    147 /**
    148 * 删除文件
    149 * @param filePathAndName 文本文件完整绝对路径及文件名
    150 * @return Boolean 成功删除返回true遭遇异常返回false
    151 */
    152 public boolean delFile(String filePathAndName) {
    153 boolean bea = false;
    154 try {
    155 String filePath = filePathAndName;
    156 File myDelFile = new File(filePath);
    157 if(myDelFile.exists()){
    158 myDelFile.delete();
    159 bea = true;
    160 }else{
    161 bea = false;
    162 message = (filePathAndName+"删除文件操作出错");
    163 }
    164 }
    165 catch (Exception e) {
    166 message = e.toString();
    167 }
    168 return bea;
    169 }
    170
    171
    172
    173 /**
    174 * 删除文件夹
    175 * @param folderPath 文件夹完整绝对路径
    176 * @return
    177 */
    178 public void delFolder(String folderPath) {
    179 try {
    180 delAllFile(folderPath); //删除完里面所有内容
    181 String filePath = folderPath;
    182 filePath = filePath.toString();
    183 java.io.File myFilePath = new java.io.File(filePath);
    184 myFilePath.delete(); //删除空文件夹
    185 }
    186 catch (Exception e) {
    187 message = ("删除文件夹操作出错");
    188 }
    189 }
    190
    191
    192 /**
    193 * 删除指定文件夹下所有文件
    194 * @param path 文件夹完整绝对路径
    195 * @return
    196 * @return
    197 */
    198 public boolean delAllFile(String path) {
    199 boolean bea = false;
    200 File file = new File(path);
    201 if (!file.exists()) {
    202 return bea;
    203 }
    204 if (!file.isDirectory()) {
    205 return bea;
    206 }
    207 String[] tempList = file.list();
    208 File temp = null;
    209 for (int i = 0; i < tempList.length; i++) {
    210 if (path.endsWith(File.separator)) {
    211 temp = new File(path + tempList[i]);
    212 }else{
    213 temp = new File(path + File.separator + tempList[i]);
    214 }
    215 if (temp.isFile()) {
    216 temp.delete();
    217 }
    218 if (temp.isDirectory()) {
    219 delAllFile(path+"/"+ tempList[i]);//先删除文件夹里面的文件
    220 delFolder(path+"/"+ tempList[i]);//再删除空文件夹
    221 bea = true;
    222 }
    223 }
    224 return bea;
    225 }
    226
    227
    228 /**
    229 * 复制单个文件
    230 * @param oldPathFile 准备复制的文件源
    231 * @param newPathFile 拷贝到新绝对路径带文件名
    232 * @return
    233 */
    234 public void copyFile(String oldPathFile, String newPathFile) {
    235 try {
    236 int bytesum = 0;
    237 int byteread = 0;
    238 File oldfile = new File(oldPathFile);
    239 if (oldfile.exists()) { //文件存在时
    240 InputStream inStream = new FileInputStream(oldPathFile); //读入原文件
    241 FileOutputStream fs = new FileOutputStream(newPathFile);
    242 byte[] buffer = new byte[1444];
    243 while((byteread = inStream.read(buffer)) != -1){
    244 bytesum += byteread; //字节数 文件大小
    245 System.out.println(bytesum);
    246 fs.write(buffer, 0, byteread);
    247 }
    248 inStream.close();
    249 }
    250 }catch (Exception e) {
    251 message = ("复制单个文件操作出错");
    252 }
    253 }
    254
    255
    256 /**
    257 * 复制整个文件夹的内容
    258 * @param oldPath 准备拷贝的目录
    259 * @param newPath 指定绝对路径的新目录
    260 * @return
    261 */
    262 public void copyFolder(String oldPath, String newPath) {
    263 try {
    264 new File(newPath).mkdirs(); //如果文件夹不存在 则建立新文件夹
    265 File a=new File(oldPath);
    266 String[] file=a.list();
    267 File temp=null;
    268 for (int i = 0; i < file.length; i++) {
    269 if(oldPath.endsWith(File.separator)){
    270 temp=new File(oldPath+file[i]);
    271 }else{
    272 temp=new File(oldPath+File.separator+file[i]);
    273 }
    274 if(temp.isFile()){
    275 FileInputStream input = new FileInputStream(temp);
    276 FileOutputStream output = new FileOutputStream(newPath + "/" +
    277 (temp.getName()).toString());
    278 byte[] b = new byte[1024 * 5];
    279 int len;
    280 while ((len = input.read(b)) != -1) {
    281 output.write(b, 0, len);
    282 }
    283 output.flush();
    284 output.close();
    285 input.close();
    286 }
    287 if(temp.isDirectory()){//如果是子文件夹
    288 copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);
    289 }
    290 }
    291 }catch (Exception e) {
    292 message = "复制整个文件夹内容操作出错";
    293 }
    294 }
    295
    296
    297 /**
    298 * 移动文件
    299 * @param oldPath
    300 * @param newPath
    301 * @return
    302 */
    303 public void moveFile(String oldPath, String newPath) {
    304 copyFile(oldPath, newPath);
    305 delFile(oldPath);
    306 }
    307
    308
    309 /**
    310 * 移动目录
    311 * @param oldPath
    312 * @param newPath
    313 * @return
    314 */
    315 public void moveFolder(String oldPath, String newPath) {
    316 copyFolder(oldPath, newPath);
    317 delFolder(oldPath);
    318 }
    319 public String getMessage(){
    320 return this.message;
    321 }
    322 }

  • 相关阅读:
    『Asp.Net 组件』第一个 Asp.Net 服务器组件:自己的文本框控件
    『Asp.Net 组件』Asp.Net 服务器组件 的开发优势和劣势
    『开源』简单的代码统计工具 开源啦[有图有真相]
    文件中的类都不能进行设计,因此未能为该文件显示设计器。设计器检查出文件中有以下类: FormMain --- 未能加载基类
    DB2:FETCH FIRST 1 ROWS ONLY
    IEnumerable的几个简单用法
    一个字符串中包含逗号个数
    字符串处理总结之一(C#String类)
    C# 中DateTime的各种使用
    C# 键值对类相关
  • 原文地址:https://www.cnblogs.com/yuchao/p/1962382.html
Copyright © 2011-2022 走看看