日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

本文介紹了如何用BufferedWriter將二維數(shù)組保存為文本文件?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我正在嘗試使用BufferedWriter將2D數(shù)組存儲(chǔ)在文本文件中,我還希望使用BufferedReader從文本文件中檢索2D數(shù)組并以其原始數(shù)組格式顯示。這兩種方法我都沒(méi)什么經(jīng)驗(yàn)。

要保存在txt文件中的預(yù)期結(jié)果為:

1 5 7 8 2 3 9 6 4
4 8 3 7 6 9 2 1 5
6 2 9 5 1 4 7 3 8
5 3 1 9 4 2 6 8 7
2 7 4 3 8 6 5 9 1
8 9 6 1 7 5 3 4 2
9 4 8 2 3 7 1 5 6
3 6 2 4 5 1 8 7 9
7 1 5 6 9 8 4 2 3

BufferedWriter:

// Instantiate a Date object
static Date date = new Date();

static int[][] board = {{0, 0, 5, 9, 7, 1, 8, 4, 6},
                        {0, 7, 1, 2, 8, 6, 9, 3, 5},
                        {0, 9, 6, 4, 3, 5, 2, 7, 1},
                        {0, 6, 8, 7, 4, 9, 5, 2, 3},
                        {0, 4, 9, 5, 2, 3, 1, 6, 8},
                        {0, 5, 2, 1, 6, 8, 4, 9, 7},
                        {0, 2, 4, 8, 1, 7, 3, 5, 9},
                        {0, 1, 3, 6, 5, 2, 7, 8, 4},
                        {0, 8, 7, 3, 9, 4, 6, 1, 2}}; 

try {

    File file = new File("/c:/sudoku" + date + ".txt");

    if (!file.exists()) {
        file.createNewFile();
    }

    FileWriter fw = new FileWriter(file.getAbsoluteFile());
    try (BufferedWriter bw = new BufferedWriter(fw)) {
        bw.write(board, 0, board.length());
    }

    System.out.println("Your Game was saved with success !");

} catch (IOException e) {

}

推薦答案

我建議(和Bob一樣)將其存儲(chǔ)為csv格式(逗號(hào)分隔值)

Date date = new Date();
int[][] board = {{0, 0, 5, 9, 7, 1, 8, 4, 6},
                        {0, 7, 1, 2, 8, 6, 9, 3, 5},
                        {0, 9, 6, 4, 3, 5, 2, 7, 1},
                        {0, 6, 8, 7, 4, 9, 5, 2, 3},
                        {0, 4, 9, 5, 2, 3, 1, 6, 8},
                        {0, 5, 2, 1, 6, 8, 4, 9, 7},
                        {0, 2, 4, 8, 1, 7, 3, 5, 9},
                        {0, 1, 3, 6, 5, 2, 7, 8, 4},
                        {0, 8, 7, 3, 9, 4, 6, 1, 2}}; 

StringBuilder builder = new StringBuilder();
for(int i = 0; i < board.length; i++)//for each row
{
   for(int j = 0; j < board.length; j++)//for each column
   {
      builder.append(board[i][j]+"");//append to the output string
      if(j < board.length - 1)//if this is not the last row element
         builder.append(",");//then add comma (if you don't like commas you can use spaces)
   }
   builder.append("
");//append new line at the end of the row
}
BufferedWriter writer = new BufferedWriter(new FileWriter("/c:/sudoku" + date + ".txt"));
writer.write(builder.toString());//save the string representation of the board
writer.close();

希望一切正常

編輯:
以下是如何重讀您的黑板:

String savedGameFile = /*...*/;
int[][] board = new int[9][9];
BufferedReader reader = new BufferedReader(new FileReader(savedGameFile));
String line = "";
int row = 0;
while((line = reader.readLine()) != null)
{
   String[] cols = line.split(","); //note that if you have used space as separator you have to split on " "
   int col = 0;
   for(String  c : cols)
   {
      board[row][col] = Integer.parseInt(c);
      col++;
   }
   row++;
}
reader.close();

這篇關(guān)于如何用BufferedWriter將二維數(shù)組保存為文本文件?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,

分享到:
標(biāo)簽:BufferedWriter 保存為 如何用 數(shù)組 文本文件
用戶無(wú)頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過(guò)答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定