已知一个文件是D:\data.txt,转换行列到D:\shuchu.txt。
D:\data.txt是这样的:
M1 23 86 70
M2 44 88 34
M3 99 67 29
M4 11 23 65
M5 45 32 84
D:\shuchu.txt是这样的:
M1 M2 M3 M4 M5
23 44 99 11 45
86 88 67 23 32
70 34 29 65 84
我的程序是这样的,但是不对,不知道怎么办了,呵呵。希望高手指点。
import java.io.*;
class Test16 {
public static void main(String[] args) throws Exception{
FileReader fr = new FileReader("D://data.txt");
//FileWriter fw = new FileWriter("D://shuchu.txt");
PrintWriter pw = new PrintWriter(new FileWriter("D://shuchu.txt"));
int v = 0;
while((v = fr.read()) != -1)
{ pw.write(v);
//pw.println(v); }
pw.flush();
pw.close();
fr.close();
//System.out.println();
}
}
D:\data.txt是这样的:
M1 23 86 70
M2 44 88 34
M3 99 67 29
M4 11 23 65
M5 45 32 84
D:\shuchu.txt是这样的:
M1 M2 M3 M4 M5
23 44 99 11 45
86 88 67 23 32
70 34 29 65 84
我的程序是这样的,但是不对,不知道怎么办了,呵呵。希望高手指点。
import java.io.*;
class Test16 {
public static void main(String[] args) throws Exception{
FileReader fr = new FileReader("D://data.txt");
//FileWriter fw = new FileWriter("D://shuchu.txt");
PrintWriter pw = new PrintWriter(new FileWriter("D://shuchu.txt"));
int v = 0;
while((v = fr.read()) != -1)
{ pw.write(v);
//pw.println(v); }
pw.flush();
pw.close();
fr.close();
//System.out.println();
}
}
然后用个中间变量传值
temp = array[i][j];
array[i][j] = array[j][i];
array[j][i] = temp;最后还原这个数组