欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java文件操作,java文件

来源: javaer 分享于  点击 10036 次 点评:48

java文件操作,java文件


package com.wnk.fitvogue.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 文件操作
 *
 * @author liuxm
 *
 */
public class HandleFileUtil {
 /**
  * 说明:复制文件
  *
  * @param srcFile
  * @param detFile
  * @throws IOException
  */

 public static void copyFile(File srcFile, File detFile) throws IOException {
  BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
    srcFile));
  BufferedOutputStream bos = new BufferedOutputStream(
    new FileOutputStream(detFile));
  int b = 0;
  while ((b = bis.read()) != -1) {
   bos.write(b);
  }
  bis.close();
  bos.close();
 }
 /**
  *
  *  @Enclosing_Method: readFile
  *  @Description:  一次读一个字节读文件
  *  @Written by: liuxmi
  *  @Creation Date: Jul 14, 2010 3:56:47 AM
  *  @version: v1.00
  *  @param srcFile
  *  @return
  *  @throws IOException
  *  @return String
  *
  */
 public static String readFile(String fileName) throws IOException {
  File srcFile = new File(fileName);
  BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
    srcFile));
  StringBuilder context = new StringBuilder();
  int b = 0;
  while ((b = bis.read()) != -1) {
   context.append(b);
   System.out.println(b);
  }
  bis.close();
  return context.toString();
 }
 
/**
 *
 *  @Enclosing_Method: readFileForList
 *  @Description:  读取十六进制文件
 *  @Written by: liuxmi
 *  @Creation Date: Jul 19, 2010 6:23:50 AM
 *  @version: v1.00
 *  @param fileName
 *  @return
 *  @throws IOException
 *  @return String
 *
 */
 public static String readHexFile(String fileName) throws IOException {
  InputStream is = new FileInputStream(fileName);
        byte[] b = new byte[is.available()];
        is.read(b);
        is.close();
  return byte2hex(b);
 }
 public static String byte2hex(byte[] b) {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }
        return hs.toUpperCase();
        }
/**
 *
 *  @Enclosing_Method: readFileByLines
 *  @Description:  以行为单位读取文件,常用于读面向行的格式化文件
 *  @Written by: liuxmi
 *  @Creation Date: Jul 14, 2010 3:57:18 AM
 *  @version: v1.00
 *  @param fileName 文件名
 *  @return void
 *
 */
 public static String readFileByLines(String fileName) {
  File file = new File(fileName);
  BufferedReader reader = null;
  StringBuilder context = new StringBuilder();
  try {
   System.out.println(" 以行为单位读取文件内容,一次读一整行:");
   reader = new BufferedReader(new FileReader(file));   
   String tempString = null;
   int line = 1;
   // 一次读入一行,直到读入null为文件结束
   while ((tempString = reader.readLine()) != null) {
    // 显示行号
    context.append(tempString);
    line++;
   }
   reader.close();
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   if (reader != null) {
    try {
     reader.close();
    } catch (IOException e1) {
    }
   }
  }
  return context.toString();
 }

}

相关文章

    暂无相关文章
相关栏目:

用户点评