java 文件操作分析

阿里 阅读:167 2021-06-15 12:05:35 评论:0

按行读取和写入

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); 
        } 
    } 
}

标签:java
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
KIKK导航

KIKK导航

排行榜
关注我们

一个IT知识分享的公众号