小弟刚学JAVA不久,碰到个文件读写的问题,题目如下:
写一个程序,从D盘读取一个Student的文件(里面的内容是POPOANTOT,如果D盘没有这个文件,自己建)把POPOANTOT中所有的O换成INT。望各位大哥帮忙解决下!!!

解决方案 »

  1.   

    /*
     * Created on 2006-9-26
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    package com.pss.test.sep;import java.io.File;
    import java.io.FileWriter;import com.pss.util.prints.Conica;/**
     * @author Administrator
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    public class Test1 { public static void main(String[] args) {
    String fileName = "d:/Student.txt";
    String content = "POPOANTOT";
    File file = new File(fileName);
    if(!file.exists()) {
    Conica.pl("Not exists, to write...");
    doWrite(fileName, content);
    Conica.pl("Write suecess!");
    } else {
    Conica.pl("Exists, to replace...");
    String value = content.replaceAll("O", "INT");
    doWrite(fileName, value);
    Conica.pl("Replace suecess!");
    }
    }

    public static void doWrite(String fileName, String content) {
    try {
    FileWriter fos = new FileWriter(fileName);
    fos.write(content);
    fos.close();
    } catch (Exception ex) {

    }
    }
    }