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

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(); } }本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录