mashen08.09整理今日所学

本文介绍了一个简单的Java程序,该程序演示了如何将两个文本文件合并为一个新文件,并展示了如何生成并保存99乘法表到文本文件中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.将123.txt与456.txt这2个文件合并到第三个文件789.txt中

import java.io.*;
public class IO_Test {
public static void main(String[] args) throws IOException {
InputStream is = new FileInputStream("e:/123.txt");
byte[] b = new byte[200];
int length = 0;
String s = null;
//文件123的内容
StringBuffer sb1 = new StringBuffer();
//读取123文件内容
while (-1 != (length = is.read(b))) {
s = new String(b, 0, length);
sb1.append(s);
}
//读取文件456内容
InputStream ins = new FileInputStream("e:/456.txt");
byte[] by = new byte[200];
int len = 0;
//文件456内容
StringBuffer sb2 = new StringBuffer();
while (-1 != (len = ins.read(by))) {
String st = new String(by, 0, len);
sb2.append(st);
}

//创建文件789.txt
File file1 = new File("e:/789.txt");
if (!file1.exists()) {
file1.createNewFile();
}
sb1.append(sb2);
String context = sb1.toString();

OutputStream ops = new FileOutputStream(file1);

//写入合并后内容
ops.write(context.getBytes());
ops.flush();
ops.close();
ins.close();

}
}


2.将99乘法表放入a.txt中

public class TestTable {
public static void main(String[] args) throws IOException {
/*
  99乘法表放入文件a.txt中
*/
String s = null;
StringBuffer sb = new StringBuffer();
for(int x =1; x< 10; x++){
for(int y = 1; y<= x; y++){
s = new String(x+"*"+y+"="+x*y+"\t");
sb.append(s);
}
sb.append("\n");
}
String context = sb.toString();
File fl = new File("e:\\a.txt");
if(!fl.exists()){
fl.createNewFile();
}
OutputStream os = new FileOutputStream(fl);
os.write(context.getBytes());

}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值