試試這個代碼:
int[][] arr = new int[3][3];
//fill array
for(int i = 0; i < arr.length; i++) {
for(int j = 0; j < arr[0].length; j++) {
arr[i][j] = j;
}
}
//create path
Path path = Paths.get("your\\path\\to\\file");
try {
//check if file exists; if not, create it
if(!(new File(path.toString()).exists())) {
Files.write(path, ("").getBytes());
}
int row = 1;
//iterate through the 2D array
for(int[] a : arr) {
//write the row number
Files.write(path, ("Row" + row + ": ").getBytes(), StandardOpenOption.APPEND);
for(int val : a) {
//write the ItemNo
Files.write(path, ("ItemNo" + val + " ").getBytes(), StandardOpenOption.APPEND);
}
row++;
//write a newline
Files.write(path, (System.getProperty("line.separator")).getBytes(), StandardOpenOption.APPEND);
}
} catch (Exception e) {
e.printStackTrace();
}