いつもお世話になっております。
現在、eclipse4.2を利用して、windows上で動くアプリケーションをjavaを用いて作成しております。
アプリケーションの作成はほぼ完了しており、eclipseでエクスポートを行いjarファイルを作成しました。
そのアプリケーションの機能でiPadからデータが送られてきたら、そのデータをPCに保存するという機能があり、拡張子「dat」のファイルを保存します。
しかし、現在次のような問題があり、困っています。どうかご助力をお願いします。
[問題点]
ファイル保存時、データはUTF-8として保存されるが、jarで実行した場合に、文字化けする。
eclipseから実行した場合には問題なく表示される。
jarファイルから実行した場合でも文字化けを起こさないようにするにはどうすればいいでしょうか。
iPadから送られてくるデータはUTF-8になっています。
jarファイルで作成したファイルを以下の操作をすると次の画像のようになります。
メモ帳を開き、「ファイル」→「開く」から一度文字コードを「ANSI」で開く。その後、「ファイル」→「開く」から一度文字コードを「UTF-8」で開く。
また、環境は次のようになります。
java : 1.8.0_51
OS : windows7 64bit
以下がファイル作成部分のソースになります。
java
1BufferedWriter bw = null; 2BufferedReader br = null; 3 4String dirPath; 5try{ 6 dirPath = this.getDirPath(); //ディレクトリパスを取得 7}catch(IOException ioE){ 8 ioE.printStackTrace(); 9 dirPath = "."; 10} 11//ディレクトリ作成 12File dir = new File(dirPath); 13if(!dir.exists() || !dir.isDirectory()){ 14 dir.delete(); 15 dir.mkdirs(); 16} 17 18try{ 19 bw = new BufferedWriter(new OutputStreamWriter(this.outputStream)); 20 br = new BufferedReader(new InputStreamReader(this.inputStream)); 21 22 char c[] = new char[256]; 23 while(br.ready()){ 24 if((br.read(c, 0, c.length)) == -1){ 25 } 26 } 27 String kNo = new String(c); 28 String targetPath = dir.getCanonicalPath() + System.getProperty("file.separator") + kNo ; 29 targetPath = targetPath.replaceAll("\u0000", ""); 30 File uploadDir = new File(targetPath); 31 32 if(!uploadDir.exists() || !uploadDir.isDirectory()){ 33 uploadDir.delete(); 34 uploadDir.mkdirs(); 35 } 36 37 // 受信完了通知送信 38 bw.write("1"); 39 bw.flush(); 40 41 while(true){ 42 String fileName; 43 BufferedWriter fileBw; 44 int cLen = c.length; 45 for(int i = 0 ; i < cLen ; i++){ 46 // 配列cのクリア 47 c[i] = 0; 48 } 49 int len = -1; 50 51 // ファイル名受信 52 while(!br.ready()){ 53 } 54 if((len = br.read(c, 0, c.length)) != -1){ 55 // 受信完了通知送信 56 bw.write("1"); 57 bw.flush(); 58 fileName = new String(c); 59 if(len == 1 && c[0] == '.'){ 60 // ファイル名が「.」一文字だったらループ終了 61 System.out.println("exit"); 62 break; 63 } 64 65 String upFilePath = uploadDir.getCanonicalPath() + System.getProperty("file.separator") + fileName; 66 upFilePath = upFilePath.replaceAll("\u0000", ""); 67 File uploadFile = new File(upFilePath); 68 if(uploadFile.exists()){ 69 // ファイルが既に存在していた場合は削除しておく 70 uploadFile.delete(); 71 }else{ 72 uploadFile.createNewFile(); 73 } 74 fileBw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(uploadFile))); 75 // データ受信とファイルへの書込み 76 try{ 77 for(int i = 0 ; i < cLen ; i++){ 78 // 配列cのクリア 79 c[i] = 0; 80 } 81 while((len = br.read(c, 0, c.length)) != -1){ 82 if(len == 1 && c[0] == '.'){ 83 fileBw.write(""); 84 break; 85 } 86 fileBw.write(c, 0, len); 87 88 for(int i = 0 ; i < cLen ; i++){ 89 // 配列cのクリア 90 c[i] = 0; 91 } 92 93 if(!br.ready()){ 94 break; 95 } 96 } 97 fileBw.flush(); 98 }catch(IOException ioE){ 99 ioE.printStackTrace(); 100 }finally{ 101 fileBw.close(); 102 } 103 104 // 受信完了通知を送信する 105 bw.write("1"); 106 bw.flush(); 107 } 108 } 109 110}catch(IOException ioE){ 111 ioE.printStackTrace(); 112 113 StringWriter sw = new StringWriter(); 114 PrintWriter pw = new PrintWriter(sw); 115 ioE.printStackTrace(pw); 116}finally{ 117 // バッファ、入出力用ストリーム、ソケットを閉じる 118 try{ 119 if(bw != null){ 120 // 受信完了通知送信 121 bw.write("1"); 122 bw.flush(); 123 bw.close(); 124 } 125 if(br != null){ 126 br.close(); 127 } 128 if(this.inputStream != null){ 129 this.inputStream.close(); 130 } 131 if(this.outputStream != null){ 132 this.outputStream.close(); 133 } 134 if(this.clientSocket != null){ 135 this.clientSocket.close(); 136 } 137 }catch(IOException ioE){ 138 ioE.printStackTrace(); 139 } 140}
以上、よろしくお願いします。

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/07/28 05:23
2015/07/28 05:36
2015/07/28 05:46
2015/07/28 06:05
2015/07/28 06:16
2015/07/28 07:01
2015/07/28 07:10 編集
2015/07/28 07:12
2015/07/28 07:20
2015/07/28 08:52
2015/07/29 00:57