java 文件操作

按行读取和写入

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class JavaFile {
      public static void main(String[] args) {
            try {
            // read file content from file
            StringBuffer sb= new StringBuffer("");
            FileReader reader = new FileReader("c://test.txt");
            BufferedReader br = new BufferedReader(reader);

            String str = null;
            while((str = br.readLine()) != null) {
                  sb.append(str+"/n");
                  System.out.println(str);
            }
            br.close();
            reader.close();
            // write string to file
            FileWriter writer = new FileWriter("c://test2.txt");
            BufferedWriter bw = new BufferedWriter(writer);
            bw.write(sb.toString());
            bw.close();
            writer.close();
      }catch(FileNotFoundException e) {
            e.printStackTrace();
      }catch(IOException e) {
            e.printStackTrace();
      }
   }
}

 

package com.file.test;
import java.io.FileWriter; public class FileMain { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub FileManager a = new FileManager("a.txt",new char[]{'\n'}); FileManager b = new FileManager("b.txt",new char[]{'\n'}); FileWriter c = new FileWriter("c.txt"); String aWord = null; String bWord = null; while((aWord = a.nextWord()) != null){ c.write(aWord+"\n"); bWord = b.nextWord(); if(bWord != null) c.write(bWord + "\n"); } while((bWord = b.nextWord())!= null){ c.write(bWord + "\n"); } c.close(); } }
package com.file.test;
import java.io.File; import java.io.FileReader; public class FileManager { String[] words = null; int pos = 0; public FileManager(String filename,char[] seperators) throws Exception{ File f = new File(filename); FileReader reader = new FileReader(f); char[] buf = new char[(int)f.length()]; int len = reader.read(buf); String results = new String(buf,0,len); String regex = null; if(seperators.length > 1){ regex = "" + seperators[0] + "|" +seperators[1]; }else{ regex = "" + seperators[0]; } words = results.split(regex); } public String nextWord(){ if(pos == words.length) return null; return words[pos++]; } }

 

package com.file.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.InputStream;
import java.io.OutputStream;

public class Jad2Java {
    public static void main(String[] args) throws Exception{
        File srcDir = new File("java");
        if(srcDir.exists()&&srcDir.isDirectory()){
            throw new Exception("目录不存在");
        }
        File[] files = srcDir.listFiles(
                new FilenameFilter(){
                    public boolean accept(File dir,String name){
                        return name.endsWith(".java");
                    }
                }
        );
        System.out.println(files.length);
        File destDir = new File("jad");
        if(!destDir.exists()){
            destDir.mkdir();
        }
        for(File f:files){
            FileInputStream fis = new FileInputStream(f);
            String destFilename = f.getName().replaceAll("\\.java$", ".jad");
            FileOutputStream fos = new FileOutputStream(new File(destDir,destFilename));
            copy(fis,fos);
            fis.close();
            fos.close();
        }
    }
    private static void copy(InputStream ips,OutputStream ops) throws Exception{
        int len = 0;
        byte[] buf = new byte[1024];
        while((len = ips.read(buf))!= -1){
            ops.write(buf,0,len);
        }
    }
}

 

posted @ 2013-03-19 18:10  残星  阅读(243)  评论(0编辑  收藏  举报