在:
writer.write(str);
后加:
writer.flush();

解决方案 »

  1.   

    你没有加flush,也就是你们发字符串流写到文件中,另外你的程序不可能执行记事本程序,你需要在exec中加入notepad.exe +f.getAbsolutePath()
      

  2.   

    import java.io.*;
    import java.io.File.*;
    import  java.util.*;public class TestCommand{
        public static void main(String[] args){
            try{
                String str="happy year";
                File f=File.createTempFile("ssss",".txt");
                FileWriter writer=new FileWriter(f);
                writer.write(str);
                writer.flush();
                Runtime rt=Runtime.getRuntime();
                rt.exec("cmd /c start notepad.exe "  + f.getAbsolutePath());
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }