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); //存放數(shù)組數(shù)據(jù)的文件 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 { //將數(shù)組中的數(shù)據(jù)寫入到文件中。每行各數(shù)據(jù)之間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); //存放數(shù)組數(shù)據(jù)的文件 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; //一行數(shù)據(jù) int row= 0 ; //逐行讀取,并將每個(gè)數(shù)組放入到數(shù)組中 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 數(shù)據(jù)處理(int[][]存儲(chǔ)與讀取)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。