日期:2014-05-20  浏览次数:21057 次

在文件的每一段下面插入一个空行?
package aa.com;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class InsertEnter {
public static void main(String[] args) {
getFile(new File("C:\\Users\\Administrator\\Desktop\\aa.txt"),new File("C:\\Users\\Administrator\\Desktop\\bb.txt"));
}
private static void getFile(File f,File fi) {
try {
FileInputStream in=new FileInputStream(f);
BufferedReader read=new BufferedReader(new InputStreamReader(in));
FileOutputStream out=new FileOutputStream(fi);
PrintWriter print=new PrintWriter(out);
String mm=null;
do{
mm=read.readLine();
if(null!=mm&&mm.length()>0){
System.out.println("oijoij");
print.println(mm);
print.println("\n");
}
}while(null!=mm);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
谁能帮我看一下,代码有什么问题,谢谢

------解决方案--------------------
应该是文件没有关闭的原因吧
Java code

package aa.com;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class InsertEnter {
    public static void main(String[] args) {
        getFile(new File("aa.txt"),new File("bb.txt"));
    }
    
    private static void getFile(File f,File fi) {
        try {
            FileInputStream in=new FileInputStream(f);
            BufferedReader read=new BufferedReader(new InputStreamReader(in));
            FileOutputStream out=new FileOutputStream(fi);
            PrintWriter print=new PrintWriter(out);
            String mm=null;
            do{
                mm=read.readLine();
                if(null!=mm&&mm.length()>0){
                    System.out.println("oijoij");
                    print.println(mm);
                    print.println("\n");
                }
            } while(null!=mm);
            print.close();//关闭文件流
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}