java se-作业4-九九乘法表输出文件-2016.7.22

来源:互联网 发布:分类目录源码帝国cms 编辑:程序博客网 时间:2024/05/21 00:45
package com.mashensoft.homework4;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;


public class IOStreamHomework {


    public void paintTable(String filePath){
        for(int i=1;i<=9;i=i+2){
                 for(int j=1;j<=i;j++){
                         int a=i*j;
                         if(a<10){
                         String str01=i+"*"+j+"="+a+"    ";
                         writeData(str01, filePath);
                         }else{
                     String str02=i+"*"+j+"="+a+"   ";
                     writeData(str02, filePath);
                             }
              }
             String str03="\r\n";
             writeData(str03,  filePath);
      }
}
public void writeData(String data,String filePath){
File file=new File(filePath);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
OutputStream os=null;
try {
os=new FileOutputStream(file,true);
byte[]bytes=data.getBytes();
os.write(bytes);
os.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(os!=null){
os.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
    
public static void main(String[] args) {
// TODO Auto-generated method stub
        IOStreamHomework ioStreamHomework=new IOStreamHomework();
        ioStreamHomework.paintTable("e:/test/aa.txt");
}


}
0 0