###質問内容
Arraylistを用いて,他ディレクトリにある.txtの内容を一行ずつ格納しようとしているのですが,現状だと何も格納できていません.何が原因でこのようになっているのかを教えていただきたいです.
###.txtから文を取得する関数
Java
1private static ArrayList<String> Qstring = new ArrayList<>(); 2 3public void loadQstrings(String QuestionFilePath) throws FileNotFoundException { 4 System.out.println(QuestionFilePath); 5 Responser.Qstring.add(""); 6 Scanner s = new Scanner(new File(QuestionFilePath)); 7 Responser.Qstring.clear(); 8 while (s.hasNext()){ 9 Responser.Qstring.add(s.nextLine()); 10 } 11 s.close(); 12 }
###関数の呼び出し
Java
1responser.loadQstrings("data/questions/hard.txt");
###ディレクトリ
###格納できていないことの確認
次の関数を実行するときに中身を見ました.
Java
1public void sendGameData() throws IOException { 2 try { 3 ServerSocket s = new ServerSocket(8080); // make socket 4 Socket socket = s.accept(); //wait setting demand of connection 5 6 try { 7 ObjectOutputStream objectOutput = new ObjectOutputStream(socket.getOutputStream()); 8 //send question 9 objectOutput.writeObject(Responser.Qstring); 10 11 System.out.println(Responser.Qstring.size()); 12 for(int i = 0; i < Responser.Qstring.size(); i++){ 13 System.out.println(Responser.Qstring.get(i)); 14 } 15 16 //send timelist 17 objectOutput.writeObject(Responser.timeList); 18 } catch (IOException e) { 19 e.printStackTrace(); 20 } 21 s.close(); 22 } catch (Exception e) { 23 e.printStackTrace(); 24 } 25 26 }
結果
data/questions/hard.txt 0
###hard.txt
まおうがくいんのふてきごうしゃ とあるまじゅつのいんでっくす いたいのはいやなのでぼうぎょりょくにきょくふりしたいとおもいます せいしゅうぶたやろうはばにーがーるせんぱいのゆめをみない そーどあーとおんらいんおるたなてぃぶがんげいるおんらいん にじがさきがくえんすくーるあいどるどうこうかい まほうかこうこうのれっとうせい やはりおれのせいしゅんらぶこめはまちがっている ゆうきゆうなはゆうしゃである わんだーえっぐぷらいおりてぃ
###追記
パスでの指定が駄目なのかと考え.ディレクトリ構造を次のようにし,以下のようにコードを変更したのですが改善しませんでした.
呼び出し
java
1responser.loadQstrings("hard.txt");
読み込み
Java
1 public void loadQstrings(String QuestionFilePath) throws FileNotFoundException { 2 System.out.println(QuestionFilePath); 3 Responser.Qstring.add(""); 4 Scanner s = new Scanner(new File(QuestionFilePath)); 5 Responser.Qstring.clear(); 6 while (s.hasNext()){ 7 System.out.println("add"); 8 Responser.Qstring.add(s.nextLine()); 9 } 10 s.close(); 11 }
結果
hard.txt 0
###Responser.java
Java
1import java.net.*; 2import java.util.*; 3import java.util.Map.Entry; 4import java.io.*; 5 6//loadlyrycslist->loadQstrings 7//lyricslist -> Qstring 8//lylicsFilePat -> QuestionFilePath 9 10//EASY = 1; 11//HARD = 2; 12 13public class Responser{ 14 private static ArrayList<String> Qstring = new ArrayList<>(); //question 15 private static ArrayList<Long> timeList = new ArrayList<>(); //waiting list 16 17 public int loadDifficulty() throws IOException{ 18 ServerSocket s = new ServerSocket(8080); // make socket 19 Socket socket = s.accept(); // wait setting demand of connection 20 BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 21 s.close(); 22 return Integer.parseInt(in.readLine()); //accept difficulty 23 } 24 25 //load Qstringsdata from arraylist to file 26 public void loadQstrings(String QuestionFilePath) throws FileNotFoundException { 27 System.out.println(QuestionFilePath); 28 Responser.Qstring.add(""); 29 Scanner s = new Scanner(new File(QuestionFilePath)); 30 Responser.Qstring.clear(); 31 while (s.hasNext()){ 32 System.out.println("add"); 33 Responser.Qstring.add(s.nextLine()); 34 } 35 s.close(); 36 } 37 38 //load timelist from arraylist to file 39 public void loadTimeList(String timeFilePath) throws FileNotFoundException { 40 Responser.timeList.add(0L); 41 Scanner s = new Scanner(new File(timeFilePath)); 42 Responser.timeList.clear(); 43 while (s.hasNext()){ 44 Responser.timeList.add(s.nextLong()); 45 } 46 s.close(); 47 } 48 49 //send gamedata in response to client requests 50 public void sendGameData() throws IOException { 51 try { 52 ServerSocket s = new ServerSocket(8080); // make socket 53 Socket socket = s.accept(); //wait setting demand of connection 54 55 try { 56 ObjectOutputStream objectOutput = new ObjectOutputStream(socket.getOutputStream()); 57 //send question 58 objectOutput.writeObject(Responser.Qstring); 59 60 System.out.println(Responser.Qstring.size()); 61 for(int i = 0; i < Responser.Qstring.size(); i++){ 62 System.out.println(Responser.Qstring.get(i)); 63 } 64 65 //send timelist 66 objectOutput.writeObject(Responser.timeList); 67 } catch (IOException e) { 68 e.printStackTrace(); 69 } 70 s.close(); 71 } catch (Exception e) { 72 e.printStackTrace(); 73 } 74 75 } 76 77 78 //accept score and register it to ranking 79 public void updateRanking() throws IOException { 80 ServerSocket s = new ServerSocket(8080); // make socket 81 Socket socket = s.accept(); // wait setting demand of connection 82 BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 83 String p_name = in.readLine(); //accept name 84 String p_score = in.readLine(); //accept score 85 int p_mode = Integer.parseInt(in.readLine()); //accept mode 86 87 //System.out.println(p_mode); 88 89 String rankingFilePath; 90 if(p_mode == 1) rankingFilePath = "data/ranking/easyranking.txt"; 91 else rankingFilePath = "data/ranking/hardranking.txt"; 92 93 //key=ID, value=score 94 Map<Integer,Integer> scores = new HashMap<Integer,Integer>(); 95 ArrayList<String> playerNameList = new ArrayList<String>(); 96 97 Scanner s2 = new Scanner(new File(rankingFilePath)); 98 int count = 1; 99 100 int id = -1, score = -1; 101 String name = ""; 102 while (s2.hasNext()){ 103 if(count % 3 == 1){ 104 id = Integer.parseInt(s2.next()); 105 }else if(count % 3 == 2){ 106 score = Integer.parseInt(s2.next()); 107 }else{ 108 name = s2.next(); 109 scores.put(id,score); 110 playerNameList.add(name); 111 } 112 count++; 113 } 114 115 try{ 116 int nextId = playerNameList.size()+1; 117 File file = new File(rankingFilePath); 118 FileWriter filewriter = new FileWriter(file, true); 119 filewriter.write(nextId + " " + p_score + " " + p_name + "\n"); 120 filewriter.close(); 121 scores.put(nextId, Integer.parseInt(p_score)); 122 playerNameList.add(p_name); 123 }catch(IOException e){ 124 System.out.println(e); 125 } 126 127 // make entry list of scorelist 128 List<Entry<Integer, Integer>> list_entries = new ArrayList<Entry<Integer, Integer>>(scores.entrySet()); 129 130 System.out.println("Sort start"); 131 //sort scorelist 132 Collections.sort(list_entries, new Comparator<Entry<Integer, Integer>>() { 133 public int compare(Entry<Integer, Integer> obj1, Entry<Integer, Integer> obj2) { 134 return obj2.getValue().compareTo(obj1.getValue()); 135 } 136 }); 137 138 139 PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); //setting of sending buffer 140 141 //send ranking data to client 142 String[] ordinalNumber = {" 1st", " 2nd", " 3rd", " 4th", " 5th"}; 143 out.println("--- Rank | Player | Score ---"); 144 for(int i=0; i<5; i++){ 145 out.println(ordinalNumber[i] + " " + playerNameList.get(list_entries.get(i).getKey()-1) + "\t " + list_entries.get(i).getValue()); 146 //System.out.println(ordinalNumber[i] + ", " + playerNameList.get(list_entries.get(i).getKey()-1) + ", " + list_entries.get(i).getValue()); 147 } 148 try{ 149 Thread.sleep(1000); 150 }catch (Exception e){ 151 e.printStackTrace(); 152 } 153 s.close(); 154 socket.close(); 155 } 156 157}
回答1件
あなたの回答
tips
プレビュー