可以参考为正确答案:
- /**
- *
- */
- package teacher.wangzengyang;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- /**
- * 题目:已知:本地D盘根目录下有2个文本文件a.txt,c.txt文本格式及内容如下图:
- a1 a2 a3 a4 1 2 3 4
- b1 b2 b3 b4 5 6 7 8
- c1 c2 c3 c4 9 10 11 12
- d1 d2 d3 d4 13 14 15 16
- 求:
- ①将a.txt文件中的所有奇数行(也就是第1、3行)字母跟数字互换(比如第一行,处理后为:1a 2a 3a 4a)
- ,偶数行顺序倒序排列(比如第二行,处理后为:b4 b3 b2 b1),然后将结果保存在b.txt中。
- ②将c.txt中的数字进行如下运算,第n行m列是第(n-1)行第m列的数字之和
- (提示:第一行不变,第二行变为:6 8 10 12,第三行变为:15 18 21 24,第四行变为28 32 36 40),
- 然后将计算结保存在d.txt中。
- * @author 旋风铭
- *
- */
- public class Test30 {
- public static void main(String[] args) throws IOException {
- BufferedReader br1=new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:/a.txt"))));
- BufferedWriter bw1=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("D:/b.txt"))));
- BufferedReader br2=new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:/c.txt"))));
- BufferedWriter bw2=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("D:/d.txt"))));
- int line=0;
- String str=null;
- String[] temp;
- StringBuilder result=new StringBuilder();
- while((str=br1.readLine())!=null){
- line++;
- temp=str.split(" ");
- result=new StringBuilder();
- for(int i=0;i<4;i++){
- if(line%2==1){
- result.append(new StringBuilder(temp[i]).reverse().toString());
- }else{
- result.append(temp[3-i]);
- }
- if(i!=3)
- result.append(" ");
- }
- bw1.write(result.toString());
- bw1.newLine();
- bw1.flush();
- }
- br1.close();
- bw1.close();
-
-
- int[] after=new int[4],now=new int[4];
- while((str=br2.readLine())!=null){
- temp=str.split(" ");
- result=new StringBuilder();
- for(int i=0;i<4;i++){
- after=now;
- now[i]=Integer.parseInt(temp[i])+after[i];
- result.append(now[i]);
- if(i!=3)
- result.append(" ");
- }
- bw2.write(result.toString());
- bw2.newLine();
- bw2.flush();
- }
- br2.close();
- bw2.close();
- }
- }
复制代码 |