gwl4808306 发表于 2012-8-24 12:40:07

【源码分享】自己扩展的File类的方法

扩展了两个方法,一个read()一个是appendText(),主要是appendText我觉得挺有用,新手手敲,大家给意见:)
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileExtend extends File {

        private static final long serialVersionUID = -3641281297280978372L;

        public FileExtend(String pathname) {
                super(pathname);
        }

        /**
       * 从文本文件中读取所有字符串
       *
       * @return 读取到的所有字符串
       * @throws FileNotFoundException
       */
        public String read() throws FileNotFoundException {
                File file = new File(super.getPath());
                if(!file.exists())throw new FileNotFoundException();
                InputStream fis = null;
                try {
                        fis = new FileInputStream(file);
                        int len = 0;
                        byte[] ch = new byte[(int) file.length()];
                        len = fis.read(ch);
                        return new String(ch, 0, len);
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                } finally {
                        try {
                                if (fis != null) {
                                        fis.close();
                                }
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                }
                return null;
        }

        /**
       * 追加给定字符串到指定文本文件中
       *
       * @param appendStr
       *            待追加的字符串
       */
        public void appendText(String appendStr) {
                if (appendStr == null)
                        throw new NullPointerException();
                if (appendStr.trim().length() == 0)
                        throw new IllegalArgumentException("Append string too short");
                OutputStream fos = null;
                String path = super.getPath();// 获得父类的变量值
                try {
                        if (new File(path).exists()) {
                                String srcContent = read();// 获得文本文件的原内容
                                fos = new FileOutputStream(path);
                                String str = srcContent + appendStr;// 拼接出新的内容
                                byte[] bt = str.getBytes();
                                fos.write(bt);// 写入新内容
                        }
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                } finally {
                        if (fos != null) {
                                try {
                                        fos.close();
                                } catch (IOException e) {
                                        e.printStackTrace();
                                }
                        }
                }
        }

}

小野千帆 发表于 2012-8-24 14:24:20

本帖最后由 小野千帆 于 2012-8-24 14:29 编辑

好多try...catch......
我的话我会全部写在throws后面就不管了......
读取的话我一直用的thinking in java里的代码:
public class BufferedInputFile {
      public static String read(String filename) throws IOException{
                BufferedReader in=new BufferedReader(new FileReader(filename));
                String s;
                StringBuilder sb=new StringBuilder();
                while((s=in.readLine())!=null){
                        sb.append(s);
                }
                in.close();
                return sb.toString();
      }
}

ミ鸭仔仔 发表于 2012-8-24 14:30:46

`52`好厉害

gwl4808306 发表于 2012-8-24 16:29:25

小野千帆 发表于 2012-8-24 14:24 static/image/common/back.gif
好多try...catch......
我的话我会全部写在throws后面就不管了......
读取的话我一直用的thinking in java ...

是的,最好是想你这样写,BufferedReader读取文件的方法挺好的!我平常都按行读,懒:P

gwl4808306 发表于 2012-8-24 16:30:26

本帖最后由 gwl4808306 于 2012-8-25 00:29 编辑

再传一个:)见笑了
/**
         * 读取指定字节长度的文本
         * @param byteLen      字节长度
         * @return
         */
      public String read(int byteLen ) throws FileNotFoundException,IOException {
                InputStream fis=null;
                String path=super.getPath();
                fis=new FileInputStream(path);
                byte[] b=new byte;
                int len=0;
                len=fis.read(b);
                if(fis!=null){
                      fis.close();
                }
                return new String(b,0,len);
      }

crazy_lock 发表于 2012-9-17 17:49:32

fair_jm 发表于 2012-9-17 19:06:05

crazy_lock 发表于 2012-9-17 17:49 static/image/common/back.gif
弱弱地说(不是打击,,,),,,
FileWrite的构造函数就有一个是否追加内容的参数,....(估计FileOutputStream也有 ...
            fis = new FileInputStream(file);
            int len = 0;
            byte[] ch = new byte[(int) file.length()];
            len = fis.read(ch);
            return new String(ch, 0, len);
说的是这个?
.......很同意你的说法 java数组没长度限制的吧 还是Interger.MAX_VALUE-1???

crazy_lock 发表于 2012-9-17 19:34:45

fair_jm 发表于 2012-9-18 18:11:13

crazy_lock 发表于 2012-9-17 19:34 static/image/common/back.gif
有啊,Java虚拟机默认好像是2M(可以修改),android的虚拟机就是4M(也是好像,,,) ...

孩子..java的虚拟机实现多不同 你说java虚拟机哪个? IBM的? hotspot?还是阿里自己收购公司开发的那个?还是openJDK的?....2M...............java数组长度最大是2M?????......这句话什么意思? .........

crazy_lock 发表于 2012-9-19 00:33:03

cao884203 发表于 2012-9-20 09:30:15

crazy_lock 发表于 2012-9-19 00:33 static/image/common/back.gif
就是sun的jvm啊,默认都是说这个吧(难道我火星了?)
话说有这么多虚拟机的么!!明明虚拟机是闭源的!!!
2 ...

虚拟机必须JVM。。放心。。 楼上说的有道理,一次性读这么多会内存泄露的,因为你不知道gc什么时候清里垃圾

crazy_lock 发表于 2012-9-29 07:14:52

页: [1]
查看完整版本: 【源码分享】自己扩展的File类的方法