1、9*9乘法口诀打印到test2.txt中,代码如下
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class week {
publicstatic void main(String[] args) throws Exception {
File txt = newFile("d:"+File.separator+"test2.txt");
FileOutputStream h = newFileOutputStream("d:"+File.separator+"test2.txt");
if(!txt.exists())
{
txt.createNewFile();
}
String s = new String();
try
{
for(int i=1;i<=9;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(i+"*"+j+"="+(i*j)+"");
if(i==j)
{
s =(i+"*"+j+"="+(i*j)+"\r\n");
}
else
{
s =(i+"*"+j+"="+(i*j)+" ");
}
h.write(s.getBytes());
}
System.out.println();
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
h.close();
}
}
}
2、将test2.txt内的乘法口诀打印到屏幕上
import java.io.File;
import java.io.FileInputStream;
publicclassweek1 {
publicstaticvoid main(String[] args) throws Exception {
File txt = new File("d:"+File.separator+"test2.txt");
FileInputStream h = new FileInputStream("d:"+File.separator+"test2.txt");
try
{
byte[] buff = newbyte[(int)txt.length()];
h.read(buff);
String tmp = new String(buff);
System.out.println(tmp);
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
h.close();
}
}
}
代码写的不是很规范,仅供参考。
java使用I/O流实现9*9乘法口诀的写入文件和从文件打印到屏幕,布布扣,bubuko.com