在D盘下创建文件夹并在文件中写入数字

在D盘下创建文件夹javatest,并在其中创建文件java.txt,在文件中依次写入0至9共10个数字,每个数字占一行,并同时显示在屏幕上。

img

img

public static void main(String[] args) {
        StringBuffer fileBuf = new StringBuffer();

        String fileRequestPar = "D:/javatest" ;

                File myPathRequest = new File(fileRequestPar);

        if (!myPathRequest.exists()) { //若此目录不存在,则递归创建
            myPathRequest.mkdirs();
        }

        File myPathResponse = new File(fileRequestPar);

        if (!myPathResponse.exists()) {
            myPathResponse.mkdirs();
        }
        String filename = "java" + ".txt";

        try {
            FileWriter fwRequst = new FileWriter(fileRequestPar + "/" +
                    filename, true);
            for(int i=0;i<10;i++){
                fileBuf.append(i+"\n");
                System.out.println(i);
            }

            String requestTxt = fileBuf.toString() ;
            fwRequst.write(requestTxt);

            fwRequst.close();
         } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }