- 投稿日:2019-06-25T23:40:35+09:00
Javaでiniを編集するためのあれこれ。
前回(https://qiita.com/haniokasai/items/336afb29060601e74a41 ) でiniを編集したら、結果として生成されるiniファイルがめちゃくちゃだったので、代替案を探す。
intellij-deps-ini4j
https://github.com/JetBrains/intellij-deps-ini4j
inieditor-java
https://github.com/nikhaldi/inieditor-java
FastIni
https://github.com/onlynight/FastIni
java-configparser
https://github.com/ASzc/java-configparser
別にどれでも構わないんだけど、使いやすそうな
IniEditor ( https://github.com/nikhaldi/inieditor-java ) 、君に決めた。Maven
<!-- https://mvnrepository.com/artifact/org.ini4j/ini4j --> <dependency> <groupId>com.nikhaldimann</groupId> <artifactId>inieditor</artifactId> <version>r6</version> </dependency>iniを読み出す
IniEditor ini = new IniEditor(); ini.load("sample.ini"); ini.get("セクション","オプション");//example //[セクション] //オプション=example ini.save("sample.ini");iniを書き込む
IniEditor ini = new IniEditor(); ini.load("sample.ini"); ini.set("セクション","オプション"); ini.save("sample.ini");iniのセクションを消す
IniEditor ini = new IniEditor(); ini.load("sample.ini"); ini.remove("section_name"); ini.save("sample.ini");その他
ブランクの行やコメントも追加できるよ。
- 投稿日:2019-06-25T23:40:35+09:00
Javaでiniを編集するためのあれこれ。:inieditor-java
前回(https://qiita.com/haniokasai/items/336afb29060601e74a41 ) でiniを編集したら、結果として生成されるiniファイルがめちゃくちゃだったので、代替案を探す。
注意
この記事のライブラリ、だめかもしれないので私の別記事を参照のこと。
ー>同一変数名の項目があるとおかしくなる。intellij-deps-ini4j
https://github.com/JetBrains/intellij-deps-ini4j
inieditor-java
https://github.com/nikhaldi/inieditor-java
FastIni
https://github.com/onlynight/FastIni
java-configparser
https://github.com/ASzc/java-configparser
別にどれでも構わないんだけど、使いやすそうな
IniEditor ( https://github.com/nikhaldi/inieditor-java ) 、君に決めた。Maven
<!-- https://mvnrepository.com/artifact/org.ini4j/ini4j --> <dependency> <groupId>com.nikhaldimann</groupId> <artifactId>inieditor</artifactId> <version>r6</version> </dependency>iniを読み出す
IniEditor ini = new IniEditor(); ini.load("sample.ini"); ini.get("セクション","オプション");//example //[セクション] //オプション=example ini.save("sample.ini");iniを書き込む
IniEditor ini = new IniEditor(); ini.load("sample.ini"); ini.set("セクション","オプション"); ini.save("sample.ini");iniのセクションを消す
IniEditor ini = new IniEditor(); ini.load("sample.ini"); ini.remove("section_name"); ini.save("sample.ini");その他
ブランクの行やコメントも追加できるよ。
- 投稿日:2019-06-25T23:29:15+09:00
【製作進捗】BlackJack:03
今回の進捗
前回からの続きです。
- プレイヤーの行動が済んだ後のディーラーの行動
- プレイヤーとディーラーの得点を比較して決着
前回と比較してあっさりになってしまいました。
では細かい内容に。内容
ディーラーの行動
基本的にはHitのみを行うように設計しています。
変わっている部分は、ディーラーの得点を参照して15点以上になるか、プレイヤーの得点を上回るまでWhile文でループさせるようにしています。BlackJack_main.javaSystem.out.println("ディーラーの2枚目のカードは["+handCard_dealer.get(1)+"]でした"); System.out.println("ディーラーの手札は"+handCard_dealer+"です"); System.out.println("ディーラーの得点は"+cardPoints_dealer+"点です"); //ディーラーの行動 while(cardPoints_dealer<15 || cardPoints_dealer<=cardPoints) { handCard_dealer.add(bills.get(0)); System.out.println("ディーラーの引いたカードは["+handCard_dealer.get(handCard_dealer.size()-1)+"]です"); cardPoints_dealer+=points.get(handCard_dealer.get(handCard_dealer.size()-1)); bills.remove(0); System.out.println("ディーラーの得点は"+cardPoints_dealer+"点です"); JUDGE.judge(cardPoints,cardPoints_dealer); if(JUDGE.result != "") { System.out.println("BlackJack終了!また遊んでね☆"); return; } }決着
簡単なif文で条件分岐させています。
blackJack_main.java//得点比較・勝敗 System.out.println("ディーラーの手札は"+handCard_dealer+"です"); System.out.println("あなたの得点は"+cardPoints+"点です"); if(cardPoints==cardPoints_dealer) { System.out.println("////引き分けです。////"); System.out.println("BlackJack終了!また遊んでね☆"); return; }else if(cardPoints>cardPoints_dealer) { System.out.println("----あなたの勝ちです----"); System.out.println("BlackJack終了!また遊んでね☆"); return; }else{ System.out.println("----あなたの負けです----"); System.out.println("BlackJack終了!また遊んでね☆"); return; }終わりに
Javaを始めてから、最初の製作物でした。
実質製作時間が15時間程になります。
オブジェクト指向設計についてまだまだ未熟なこともあって、コードが長くなってしまいましたが、伸びしろということにしておきたいと思います。
コメントしてくださった方々、Slack・Discordでアドバイスをくれた友達、ありがとうございます。なお、Gitに関してもまだ触れていないので、今回のPGはGitが利用できるようになった後にアップしようと考えています。
- 投稿日:2019-06-25T23:27:35+09:00
Frontend or Backend Development explained
I started as a back end developer and work on that for about 3 years. Then I went into management and worked on that for about 3 years and now I am a front end developer and has been doing that for 4 years.
Now that I’ve been doing FE for 4 years I can see in some projects that I’m currently working on how PHP and JAVA have evolved (from when I was a BE developer) and I wish I had more time to learn more and dive deeply into them (again), and specially work on real projects using them. I miss databases as well and I remember I loved to handle complex logic stuff in the BE.
On the other hand, I think that FE has evolved A LOT in the recent years and now there are a ton of things that you can do and that could be complex and interesting as well (like heavy logic SPAs, dynamic templating, using SASS power to handle things like theming in a nicer fashion, React, Babel, Gulp, TypeScript) and I enjoy it. And my previous experience as BE developer has helped me hugely to be a better FE developer.
So in my humble opinion, I think it depends on what you like more. But if you want to continue working with both, I guess full stack developer is the go to path. However, there’s a downsize to that and it is that I don’t think you can really get to know BE or FE very deeply unless you are extremely dedicated and spend a lot of time doing research and keeping you up to date with trending techniques, tools and technologies.
Another point is that if you choose a specific field you’ll be more likely specialized in that field after some time and you could earn more money because of that specialized knowledge you have.
I am personally thinking to start diving more in native mobile applications or machine learning. But who knows, at the end everything is just code and logic (at least to me) and the nice thing is that we have a lot of options in this field.
Related blog:
- 投稿日:2019-06-25T17:37:20+09:00
/ 区切りの ユーザID/パスワード のパスワードをマスクする
/ 区切りのパスワードをマスクする。でも / 区切りの日付はそのままにしたい。
FindUserPassword.javapackage playground; public class FindUserPassword { public static void main(String[] args) { String str6 = "D:\\ap\\cp\\0100.bat -id:249810 11 2019/05/24 guest/guest kf010 kfu0100.sh 10 1 249810"; String str5 = "D:\\ap\\cp\\0100.bat -id:249810 11 kf010 kfu0100.sh 10 1 249810"; String str4 = "D:\\ap\\cp\\0100.bat -id:249810 11 2019/05/24 guest/guest/ kf010 kfu0100.sh 10 1 249810"; String str3 = "D:\\ap\\cp\\0100.bat -id:249810 11 2019/05/24 guest/guest kf010 kfu0100.sh 10 1 249810"; String str2 = "D:\\ap\\cp\\01.bat -id:248669 1 20 12010033/19870420yt 0 tr010_bat -u cp 248669"; String str = "D:\\ap\\cp\\0100.bat -id:249810 11 2019/05/24 kf010 kfu0100.sh 10 1 249810"; System.out.println("str2: " + str2); String[] commands = str2.split(" "); StringBuffer out = new StringBuffer(); for (String command : commands) { //if ((command.indexOf("/") != command.lastIndexOf("/")) || (command.indexOf("/") < 0)) { if (command.matches("\\d{4}/\\d{2}/\\d{2}") || (command.indexOf("/") < 0)) { out.append(command).append(" "); } else { //out.append(command.replaceAll("/.+?", "/**********")).append(" "); int start = command.indexOf("/"); String replacestr = command.substring(start+1); //out.append(command.replace(replacestr, "**********")).append(" "); out.append(command.replace(replacestr, "/**********")).append(" "); } } System.out.println(out); } }以下、試行錯誤中(str6 をどうにかしたい)
FindUserPassword_renew.javapackage playground; public class FindUserPassword { String str6 = "D:\\ap\\cp\\0100.bat -id:249810 11 2019/05/24 guest/guest kf010 kfu0100.sh 10 1 249810"; String str5 = "D:\\ap\\cp\\0100.bat -id:249810 11 kf010 kfu0100.sh 10 1 249810"; String str4 = "D:\\ap\\cp\\0100.bat -id:249810 11 2019/05/24 guest/guest/ kf010 kfu0100.sh 10 1 249810"; String str3 = "D:\\ap\\cp\\0100.bat -id:249810 11 2019/05/24 guest/guest kf010 kfu0100.sh 10 1 249810"; String str2 = "D:\\ap\\cp\\01.bat -id:248669 1 20 12010033/19870420yt 0 tr010_bat -u cp 248669"; String str = "D:\\ap\\cp\\0100.bat -id:249810 11 2019/05/24 kf010 kfu0100.sh 10 1 249810"; public static void main(String[] args) { String[] commands = {str, str2, str3, str4, str5, str6}; for (String command : commands) { if (!command.matches(".+\\d{4}/\\d{2}/\\d{2}.+")) { System.out.println(command.replaceAll("/.+? ", "/********** ")); } else { System.out.println(command); } }
- 投稿日:2019-06-25T14:07:17+09:00
jenvを使って、複数バージョンのJavaを使えるようにする
jenv
現場の開発で、複数バージョンのJavaを使う機会が出てきたので、
切り替えながら使う方法を整理しました。インストールと設定
GitHubに公開されているので、もってきて設定します。
$ git clone https://github.com/gcuisinier/jenv.git ~/.jenv $ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile $ echo 'eval "$(jenv init -)"' >> ~/.bash_profileJava homeの登録
Javaは事前にインストールしておいてください。addで、Javaのホームディレクトリを登録していきます。
$ jenv add /usr/lib/jvm/java-11-openjdk-amd64
$ jenv add /opt/jdk1.7.0_80/
バージョンの確認
$ jenv versions system * 1.7 (set by /home/hogehoge/.jenv/version) 1.7.0.80 11.0 11.0.3 openjdk64-11.0.3 oracle64-1.7.0.80環境の切り替え全体のJavaバージョンの切り替え
$ jenv global oracle64-1.7.0.80所定のディレクトリは配下のJavaバージョンの切り替え
$ mkdir java7 $ cd java7 $ jenv local oracle64-1.7.0.80これでjava7の配下でのみJavaのバージョンが変わります
- 投稿日:2019-06-25T07:58:28+09:00
ComparableとComparator(オブジェクトの順序付け)
とても紛らわしい
ソートしたければHashSetではなくTreeSetを用いればよいが、自分のオリジナルの順序付けを施したいことがある。そんなときにComparatorやComparableが有効である。
Comparable
java.lang.Comparable インターフェース「比較可能」
(1)TreeSetクラスは、内部で要素を並べ替える時にComparable型にキャストする処理を行なっている。
(2)標準ライブラリのStringクラスやIntegerクラスなどの値クラスはComparableインターフェースを実装している。Myclass.javaclass MyClass implements Comparable<Myclass>{ //抽象メソッドの実装 public int compareTo(Myclass obj){ return this.id - obj.id; } } //昇順Comparator
java.util.Comparatorインターフェース 「比較器」
Comparatorが利用されるモチベーションは、TreeSetやTreeMapなどに格納される要素の並べ替え順序をデフォルトとは別の順序に変更したいような時である。Myclass.javaclass Myclass implements Comparator<String>{ @Override public int compare(String obj1, String obj2){ return obj1.length() - obj2.length(); } } //昇順
コンストラクタ 説明 TreeSet(Comparator<? super E> comparator) 指定されたコンパレータにしたがってソートされた新しい空のツリーセットを作成する TreeSetにはComparatorオブジェクトを引数にとるコンストラクタが宣言されている(OraDocsに記載されている)ので任意の並べ替え順序を適用できる。
まとめ
Comparableは「自分と他のオブジェクトを比較」
Comparatorは「他の2つのオブジェクトを比較」
(備忘録:ComparableやComparatorが紛らわしいというよりかは、TreeSetやTreeMapの定義があやふやなのかも)
- 投稿日:2019-06-25T01:24:42+09:00
JavaでCSVファイルを読み込んでCSVファイルに出力する処理
JavaでCSVファイルを読み込んでCSVファイルに出力する処理
タイトル通りです。
忘備録として残しておきます。ソース
App.javaimport java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.Properties; public class App { public static void main(String[] args) { // プロパティファイル読み込み Properties properties = new Properties(); readPropertyFile(properties); // プロパティファイルの値をもとにパスの指定 Path inputFile = Paths.get(properties.getProperty("inputFile")); Path developOutputFile = Paths.get(properties.getProperty("developOutputFile")); Path salesOutputFile = Paths.get(properties.getProperty("salesOutputFile")); // CSVファイル読み込み List<String> tmpOutputList = new ArrayList<>(); readInputFile(inputFile, tmpOutputList); // 部門判定 List<String> developOutputList = new ArrayList<>(); List<String> salesOutputList = new ArrayList<>(); checkDepartment(tmpOutputList, developOutputList, salesOutputList); // ファイル出力 writeOutputFile(developOutputFile, salesOutputFile, developOutputList, salesOutputList); } /** * ファイルパスを指定したプロパティファイルを読み込む処理 * * @param properties * 空のプロパティ */ protected static void readPropertyFile(Properties properties) { String filePath = "C:\\work\\CSVReaderSample\\resources\\settings\\filePath.properties"; try { properties.load(Files.newBufferedReader(Paths.get(filePath), StandardCharsets.UTF_8)); } catch (IOException e) { e.printStackTrace(); } } /** * ファイルの内容を全行読み込み一時的なListに格納する処理 * * @param inputFile * インプットとなるCSVファイルのパス * @param tmpOutputList * CSVファイルの内容を一時的に格納するList */ protected static void readInputFile(Path inputFile, List<String> tmpOutputList) { try (BufferedReader br = Files.newBufferedReader(inputFile, StandardCharsets.UTF_8)) { String line; while ((line = br.readLine()) != null) { tmpOutputList.add(line); } } catch (IOException e) { e.printStackTrace(); } } /** * 一時的なListを各部門Listに振り分ける処理 * * @param tmpOutputList * CSVファイルの内容を一時的に格納するList * @param developOutputList * 開発部門の内容を格納するList * @param salesOutputList * 営業部門の内容を格納するList */ protected static void checkDepartment(List<String> tmpOutputList, List<String> developOutputList, List<String> salesOutputList) { for (String list : tmpOutputList) { // 先頭行(見出し)をListに追加 if (developOutputList.size() == 0) { developOutputList.add(list); } if (salesOutputList.size() == 0) { salesOutputList.add(list); } String department = list.split(",")[0]; switch (department) { case "開発": developOutputList.add(list); break; case "営業": salesOutputList.add(list); break; } } } /** * 部署Listをもとにファイルに出力する処理 * * @param developOutputFile * 開発部門の内容を出力するCSVファイルのパス * @param salesOutputFile * 営業部門の内容を出力するCSVファイルのパス * @param developOutputList * 開発部門の内容を格納するList * @param salesOutputList * 営業部門の内容を格納するList */ protected static void writeOutputFile(Path developOutputFile, Path salesOutputFile, List<String> developOutputList, List<String> salesOutputList) { try (BufferedWriter devbr = Files.newBufferedWriter(developOutputFile, StandardCharsets.UTF_8); BufferedWriter salbr = Files.newBufferedWriter(salesOutputFile, StandardCharsets.UTF_8)) { for (String list : developOutputList) { devbr.write(list); devbr.newLine(); } for (String list : salesOutputList) { salbr.write(list); salbr.newLine(); } } catch (IOException e) { e.printStackTrace(); } } }インプットファイル
namelist.csvdepartment,no,name,age,boss 開発,1,鈴木太郎13,40,田中太郎 営業,2,鈴木太郎2,32,山田花子 開発,3,鈴木太郎20,23,山田健司 開発,4,鈴木太郎3,22,田中太郎 営業,5,鈴木太郎18,21,山田花子 開発,6,鈴木太郎5,46,本田剛 営業,7,鈴木太郎6,78,田中太郎 営業,8,鈴木太郎17,46,田中太郎 開発,9,鈴木太郎7,89,山田健司 開発,10,鈴木太郎8,23,山田花子 開発,11,鈴木太郎9,46,田中太郎 営業,12,鈴木太郎11,51,山田花子 営業,13,鈴木太郎1,26,田中太郎 開発,14,鈴木太郎12,28,本田剛 営業,15,鈴木太郎16,31,山田健司 開発,16,鈴木太郎14,32,山田健司 営業,17,鈴木太郎4,34,本田剛 営業,18,鈴木太郎15,39,本田剛 営業,19,鈴木太郎10,46,本田剛 開発,20,鈴木太郎19,28,山田健司設定ファイル
filePath.properties#ファイルパスを指定するためのプロパティ inputFile=C:\\work\\CSVReaderSample\\resources\\input\\namelist.csv developOutputFile=C:\\work\\CSVReaderSample\\resources\\output\\developList.csv salesOutputFile=C:\\work\\CSVReaderSample\\resources\\output\\salesList.csv出力結果
developList.csvdepartment,no,name,age,boss 開発,1,鈴木太郎13,40,田中太郎 開発,3,鈴木太郎20,23,山田健司 開発,4,鈴木太郎3,22,田中太郎 開発,6,鈴木太郎5,46,本田剛 開発,9,鈴木太郎7,89,山田健司 開発,10,鈴木太郎8,23,山田花子 開発,11,鈴木太郎9,46,田中太郎 開発,14,鈴木太郎12,28,本田剛 開発,16,鈴木太郎14,32,山田健司 開発,20,鈴木太郎19,28,山田健司salesList.csvdepartment,no,name,age,boss 営業,2,鈴木太郎2,32,山田花子 営業,5,鈴木太郎18,21,山田花子 営業,7,鈴木太郎6,78,田中太郎 営業,8,鈴木太郎17,46,田中太郎 営業,12,鈴木太郎11,51,山田花子 営業,13,鈴木太郎1,26,田中太郎 営業,15,鈴木太郎16,31,山田健司 営業,17,鈴木太郎4,34,本田剛 営業,18,鈴木太郎15,39,本田剛 営業,19,鈴木太郎10,46,本田剛その他
ディレクトリ構成.txtCSVReaderSample ├─resources │ ├─input │ │ namelist.csv │ │ │ ├─output │ │ developList.csv │ │ salesList.csv │ │ │ └─settings │ filePath.properties │ └─src App.java課題
処理の単位でメソッドに切り出すことはできたが、部門に変更あった場合(追加/削除)に修正すべき箇所が多いので、できるだけ変更の必要性がないようにしたい。