找回密碼 或 安全提問
 註冊
|註冊|登錄

伊莉討論區

搜索
尊貴會員無限使用任何功能伊莉需要你的贊助和支持儲值後自動升級用戶組
神奇寶貝mega 無無碼流出銀魂鬼滅之刃無碼 ge
4330669帝都棄少ktfactor美少女fc239776台灣溫泉 北

休閒聊天興趣交流學術文化旅遊交流飲食交流家庭事務PC GAMETV GAME
熱門線上其他線上感情感性寵物交流家族門派動漫交流貼圖分享BL/GL
音樂世界影視娛樂女性頻道潮流資訊BT下載區GB下載區下載分享短片
電腦資訊數碼產品手機交流交易廣場網站事務長篇小說體育運動時事經濟
上班一族博彩娛樂

[BD繁]劇場版 少女與

✡ 斗破蒼穹 年番/鬥

斗破蒼穹 年番 鬥破蒼

[繁]女王的手術刀09-

[繁]異修羅10-

[繁]藥師少女的獨語22
C & C++ 語言C# 語言Visual Basic 語言PHP 語言JAVA 語言
查看: 8599|回復: 7

[問題] java讀入txt檔,中文無法顯示[複製鏈接]

rtj197 該用戶已被刪除
發表於 2016-1-4 04:24 PM|顯示全部樓層
  1. // Import Statements
  2. import java.io.*;
  3. import java.util.*;

  4. /**
  5. * This class is used to read a text file, pick 14 random questions and an expert and store all the data in an Array.
  6. *
  7. * Questions Levels in File
  8. * Level 1 - # 1 - 225      Game Questions # 1 & 2
  9. * Level 2 - # 226 - 375    Game Questions # 3 - 5
  10. * Level 3 - # 376 - 592    Game Questions # 6 & 7
  11. * Level 4 - # 593 - 709    Game Questions # 8 - 10
  12. * Level 5 - # 710 - 857    Game Questions # 11 & 12
  13. * Level 6 - # 858 - 892    Game Questions # 13 - 15
  14. *
  15. * Data Stored in Array (ORDER)
  16. * Question #, Category, Difficulty, Question, Option A, Option B, Option C, Option D, Answer, Expert Response (after the $1000 question)
  17. *
  18. * @author (Taranveer Virk)
  19. * @version (v.1 10JAN2010)
  20. */

  21. public class QuestionReader extends Millionaire     // name of the class
  22. {
  23.     // Variables Declared
  24.     private static int [] numbers = new int [15];
  25.    
  26.     private static int min, max, tempNum, a, q;
  27.    
  28.     private static String temp;

  29.     static String info [] = new String [9];

  30.     /**
  31.      * The main method of the QuestionReaader class. This method automatically generates the appropriate number of questions for each Level (easiest to hardest) and
  32.      * stores them in an Array. An expert is also selected and his/her responses are stored in the Array as well.
  33.      *
  34.      * @return (questions)      Returns a Double Array Holding the Data from the File
  35.      */
  36.     public static void main () throws IOException, NullPointerException
  37.     {
  38.         a = 0;      // default value

  39.         for (int x=0; x<=1; x++)        // Loop runs to generate Lv. 1, 3, 5 questions.
  40.         {
  41.             numGen (1, 225);
  42.             a++;
  43.             numGen (376, 592);
  44.             a++;
  45.             numGen (710, 857);
  46.             a++;
  47.         }

  48.         for (int x=0; x<=2; x++)        // Loop runs to generate Lv. 2, 4, 6 questions.
  49.         {
  50.             numGen (226, 375);
  51.             a++;
  52.             numGen (593, 709);
  53.             a++;
  54.             numGen (858, 892);
  55.             a++;
  56.         }

  57.         Arrays.sort (numbers);          // Sorts the array Storing the questions number

  58.         expert = (int)((Math.random()*10));           // Generates an expert number
  59.         expertName = expNames[expert];

  60.         // opening a channel for input from the keyboard
  61.         BufferedReader c = new BufferedReader(new FileReader("Questions.txt"));
  62.         temp = null;        // default value;
  63.         q = 0;          // default value

  64.         //read each line of text file & store the ones to be used in an array
  65.         for (int x=1; x<=892; x++)
  66.         {
  67.             temp = c.readLine ();
  68.             if (q > 14)
  69.                 break;

  70.             else if (x == numbers [q])
  71.             {

  72.                 try
  73.                 {
  74.                     info = temp.split (";");
  75.                 }
  76.                 catch (NullPointerException r)
  77.                 {
  78.                     System.out.println ("OOOPS ... you just won a Million Dollars because of");
  79.                     System.out.println ("an error in the game.");
  80.                     System.out.println ("Congratulations, but please read on ...");
  81.                     System.out.println ();
  82.                     System.out.println ("ACTUALLY .... ");
  83.                     System.out.println ("There has been a little problem, ");
  84.                     System.out.println ("Sorry for any inconvenience but you should restart the ");
  85.                     System.out.println ("game to avoid more problems if you continue playing.");
  86.                     System.out.println ("                   OR");
  87.                     System.out.println ("Stop the Game you are playing (Use the Walk Away Button)");
  88.                     System.out.println ("And press play again.");
  89.                     System.out.println ();
  90.                     System.out.println ("Thankyou");
  91.                 }
  92.                 catch (ArrayIndexOutOfBoundsException t)
  93.                 {
  94.                     System.out.println ("OOOPS ... you just won a Million Dollars because of");
  95.                     System.out.println ("an error in the game.");
  96.                     System.out.println ("Congratulations, but please read on ...");
  97.                     System.out.println ();
  98.                     System.out.println ("ACTUALLY .... ");
  99.                     System.out.println ("There has been a little problem, ");
  100.                     System.out.println ("Sorry for any inconvenience but you should restart the ");
  101.                     System.out.println ("game to avoid more problems if you continue playing.");
  102.                     System.out.println ("                   OR");
  103.                     System.out.println ("Stop the Game you are playing (Use the Walk Away Button)");
  104.                     System.out.println ("And press play again.");
  105.                     System.out.println ();
  106.                     System.out.println ("Thankyou");
  107.                 }

  108.                 for (int y=0; y<=8; y++)
  109.                 {
  110.                     try
  111.                     {
  112.                         questions [q][y] = info [y];
  113.                     }
  114.                     catch (NullPointerException e)
  115.                     {
  116.                         System.out.println ("OOOPS ... you just won a Million Dollars because of");
  117.                         System.out.println ("an error in the game.");
  118.                         System.out.println ("Congratulations, but please read on ...");
  119.                         System.out.println ();
  120.                         System.out.println ("ACTUALLY .... ");
  121.                         System.out.println ("There has been a little problem, ");
  122.                         System.out.println ("Sorry for any inconvenience but you should restart the ");
  123.                         System.out.println ("game to avoid more problems if you continue playing.");
  124.                         System.out.println ("                   OR");
  125.                         System.out.println ("Stop the Game you are playing (Use the Walk Away Button)");
  126.                         System.out.println ("And press play again.");
  127.                         System.out.println ();
  128.                         System.out.println ("Thankyou");
  129.                     }
  130.                 }
  131.                 if (numbers [q] > 375)          // Stores the response of your expert.
  132.                     questions [q][9] = info [8+expert];
  133.                 q++;
  134.             }
  135.         }
  136.     }

  137.     /**
  138.      * This method generates an int between the specified values and if it is not already in the array, it stores it there.
  139.      *
  140.      * @param min           The smallest number that can be generated
  141.      * @param max           The largest number that can be generated
  142.      */
  143.     private static void numGen (int min, int max)
  144.     {
  145.         // This loop runs till an acceptable number has been generated
  146.         while (true)
  147.         {
  148.             tempNum = min + (int)(Math.random()*((max-min)+1));         // generates the random number between the specified values & stores it
  149.             if ((Arrays.binarySearch(numbers,tempNum))<0)  // Checks if the num generated is already in the Array, execute if it doesn't exist
  150.             {
  151.                 numbers [a] = (tempNum);     //   Add number to Array
  152.                 break;          // Stop the while loop
  153.             }
  154.         }   // While loop
  155.     }
  156. }
複製代碼
大家好,以上是程式碼。程式是一個百萬大富翁的遊戲。需要從另一個Questions.txt檔中讀入題目並顯示。

但是題目中有中文有英文,如果遇到中文就顯示不出來,請問是程式碼哪邊有問題嗎?
...
瀏覽完整內容,請先 註冊登入會員

使用道具檢舉

Rank: 1

帖子
70
積分
164 點
潛水值
12857 米
發表於 2016-1-4 05:51 PM|顯示全部樓層
Questions.txt檔案設為utf8編碼試看看
記事本存檔時選utf8或用notepad++
所有積分大於負-100的壞孩子,將可獲得重新機會成為懲罰生,權限跟幼兒生一樣。

使用道具檢舉

adarkair 該用戶已被刪除
發表於 2016-1-17 05:08 PM|顯示全部樓層
若有安裝色情守門員,可用無界、自由門等軟件瀏覽伊莉。或使用以下網址瀏覽伊莉: http://www.eyny.com:81/index.php
本帖最後由 adarkair 於 2016-1-17 05:09 PM 編輯

從reference看到:
FileReader的註解有提到這段
The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate. To specify these values yourself, construct an InputStreamReader on a FileInputStream.
大致上是說此類別是用預設的文字編碼來解讀這個檔案,所以有可能造成中文字解不出來,
...
瀏覽完整內容,請先 註冊登入會員
如果發覺自己無法使用一些功能或出現問題,請按重新整理一次,並待所有網頁內容完全載入後5秒才進行操作。

使用道具檢舉

Rank: 1

帖子
252
積分
187 點
潛水值
24460 米
發表於 2016-1-22 10:58 PM|顯示全部樓層
檔案有檔案encoding, java也有default encoding
讀檔時要確認檔案的編碼 這樣才不會亂掉

使用道具檢舉

iamyawu 該用戶已被刪除
發表於 2016-1-30 11:17 AM|顯示全部樓層
成為伊莉的版主,你將獲得更高級和無限的權限。把你感興趣的版面一步步地發展和豐盛,那種滿足感等著你來嚐嚐喔。
檔案encoding utf8編碼試看看




若有安裝色情守門員,可用無界、自由門等軟件瀏覽伊莉。或使用以下網址瀏覽伊莉: http://www.eyny.com:81/index.php

使用道具檢舉

帖子
0
積分
2 點
潛水值
150 米
發表於 2017-2-21 12:13 AM|顯示全部樓層
若對尊貴或贊助會員有任何疑問,歡迎向我們查詢。我們的即時通或MSN: admin@eyny.com
檔案弄成utf8試看看.....

使用道具檢舉

Rank: 3Rank: 3Rank: 3

帖子
1335
積分
1012 點
潛水值
30767 米
發表於 2017-2-21 05:46 PM|顯示全部樓層
所有積分大於負-100的壞孩子,將可獲得重新機會成為懲罰生,權限跟幼兒生一樣。
應該是文字檔案編碼,與你讀取時的編碼設定不同,
才會有這種狀況。

使用道具檢舉

帖子
46
積分
28 點
潛水值
15047 米
發表於 2020-4-4 11:48 PM|顯示全部樓層
若新密碼無法使用,可能是數據未更新。請使用舊密碼看看。
檔案的 Encoding 設定為 UTF-8 的編碼,試試看
如果你忘記伊莉的密碼,請在登入時按右邊出現的 '找回密碼'。輸入相關資料後送出,系統就會把密碼寄到你的E-Mail。

使用道具檢舉

您需要登錄後才可以回帖 登錄 | 註冊

Powered by Discuz!

© Comsenz Inc.

重要聲明:本討論區是以即時上載留言的方式運作,對所有留言的真實性、完整性及立場等,不負任何法律責任。而一切留言之言論只代表留言者個人意見,並非本網站之立場,用戶不應信賴內容,並應自行判斷內容之真實性。於有關情形下,用戶應尋求專業意見(如涉及醫療、法律或投資等問題)。 由於本討論區受到「即時上載留言」運作方式所規限,故不能完全監察所有留言,若讀者發現有留言出現問題,請聯絡我們。有權刪除任何留言及拒絕任何人士上載留言,同時亦有不刪除留言的權利。切勿上傳和撰寫 侵犯版權(未經授權)、粗言穢語、誹謗、渲染色情暴力或人身攻擊的言論,敬請自律。本網站保留一切法律權利。
回頂部