MyFile .java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; public class MyFile { try { File file = new File(filename); //存放数组数据的文件 FileWriter out = new FileWriter(file); //文件写入流 try { getRecord(out,arr); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } out.close(); } catch (Exception ex) { ex.printStackTrace(); } } private static void getRecord(FileWriter out, int [][] arr) throws Exception { //将数组中的数据写入到文件中。每行各数据之间TAB间隔 for ( int i= 0 ;i<arr.length;i++){ for ( int j= 0 ;j<arr[ 0 ].length;j++){ out.write(arr[i][j]+ "\t" ); } out.write( "\r\n" ); } } public static void ReadFile(String filename, int [][] arr2){ try { File file = new File(filename); //存放数组数据的文件 BufferedReader in = new BufferedReader( new FileReader(file)); // try { readRecord(in,arr2); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } in.close(); } catch (Exception ex) { ex.printStackTrace(); } } private static void readRecord(BufferedReader in, int [][] arr2) throws Exception { String line; //一行数据 int row= 0 ; //逐行读取,并将每个数组放入到数组中 while ((line = in.readLine()) != null ){ String[] temp = line.split( "\t" ); for ( int j= 0 ;j<temp.length;j++){ // arr2[row][j] = Double.parseDouble(temp[j]); arr2[row][j] = Integer.parseInt(temp[j]); } row++; } } } |
使用:
1
2
|
public static int imagedate[ ][ ]; MyFile.SaveFile( "d:\\array.txt" ,imagedate); |
以上这篇浅谈java 数据处理(int[][]存储与读取)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。