Javaのpropertiesファイルに関する質問です

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
senoa

Javaのpropertiesファイルに関する質問です

#1

投稿記事 by senoa » 11年前

javaでデータを読み書きするコードを書いているのですが上書き保存がされていないのか
初期に設定している値から変化しません。

引数のx,y,zをpropertiesファイルに書き込むメソッド

コード:

public boolean onItemUse(ItemStack itemStackIn, EntityPlayer playerIn, World p_77648_3_, int x, int y, int z, int Direction, float p_77648_8_, float p_77648_9_, float p_77648_10_){
		if(!p_77648_3_.isRemote)
		{
			  try {
		          //Propertiesオブジェクトを生成
		          Properties prop = new Properties();
		          // ファイルを読み込む
		          FileInputStream myConfFileIn = new FileInputStream("test.properties");
		          prop.load(myConfFileIn);

		          String X = String.valueOf(x);
		          String Y = String.valueOf(y);
		          String Z = String.valueOf(z);

		          // 値をセット
		          prop.setProperty("x", X);
		          prop.setProperty("y", Y);
		          prop.setProperty("z", Z);

			      //データの保存
		          FileOutputStream fos = new FileOutputStream(new File("test.properties"));
		          prop.store(fos, null);
		          fos.close();

		          } catch (Exception e) {
			  		System.out.println("Exception : " + e);
		      }
		}
		return true;
	}
propertiesファイルを読み込むメソッド

コード:

	protected void onImpact(MovingObjectPosition p_70184_1_) {
		// TODO 自動生成されたメソッド・スタブ
		try {
	          //Propertiesオブジェクトを生成
	          Properties prop = new Properties();

	          // ファイルを読み込む
	          prop.load(this.getClass().getResourceAsStream("../Items/test.properties"));

	          // 値を取得
	          String x = prop.getProperty("x");
	          String y = prop.getProperty("y");
	          String z = prop.getProperty("z");
	          telepox = Integer.parseInt(x);
	          telepoy = Integer.parseInt(y);
	          telepoz = Integer.parseInt(z);
	        } catch (Exception e) {
	          System.out.println("Exception : " + e);
	        }

       /*ここから下は関係ないです*/
		if(telepox!=0 && telepoy!=0 && telepoz!=0){
			if (p_70184_1_.entityHit != null)
	        {
	            p_70184_1_.entityHit.attackEntityFrom(DamageSource.causeThrownDamage(this, this.getThrower()), 0.0F);
	        }

	        for (int i = 0; i < 32; ++i)
	        {
	            this.worldObj.spawnParticle("portal", this.posX, this.posY + this.rand.nextDouble() * 2.0D, this.posZ, this.rand.nextGaussian(), 0.0D, this.rand.nextGaussian());
	        }

	        if (!this.worldObj.isRemote)
	        {
	            if (this.getThrower() != null && this.getThrower() instanceof EntityPlayerMP)
	            {
	                EntityPlayerMP entityplayermp = (EntityPlayerMP)this.getThrower();

	                if (entityplayermp.playerNetServerHandler.func_147362_b().isChannelOpen() && entityplayermp.worldObj == this.worldObj)
	                {
	                    EnderTeleportEvent event = new EnderTeleportEvent(entityplayermp, this.posX, this.posY, this.posZ, 0F);
	                    if (!MinecraftForge.EVENT_BUS.post(event))
	                    { // Don't indent to lower patch size
	                    if (this.getThrower().isRiding())
	                    {
	                        this.getThrower().mountEntity((Entity)null);
	                    }

	                    this.getThrower().setPositionAndUpdate(telepox, telepoy+1, telepoz);
	                    this.getThrower().fallDistance = 0.0F;
	                    this.getThrower().attackEntityFrom(DamageSource.fall, event.attackDamage);
	                    }
	                }
	            }
	            this.setDead();
	        }
		}
	}
クラスに書いてある全てのコードを書くと長くなってしまうので、必要なメソッド部分だけ書かせていただきました。
二つのメソッドは別クラスのものです。
上に書いてあるメソッドでx,y,zの値をpropertiesファイルに保存し、下に書いてあるコードでpropertiesファイルからx,y,zの値を参照するという感じです。
色々なサイトを見ながら作っているのですが、初期の値を参照するだけで書き込んだ祭の値を参照してくれません。

どなたか分かる方がいらっしゃいましたら回答よろしくお願い致します。

アバター
みけCAT
記事: 6734
登録日時: 15年前
住所: 千葉県
連絡を取る:

Re: Javaのpropertiesファイルに関する質問です

#2

投稿記事 by みけCAT » 11年前

とりあえず適当にコードを補完してコンパイルが通るようにしました。
まずItemsディレクトリに空のtest.propertiesファイルを作ってから、OnItemUseを実行しました。
その後、OnImpactを実行すると、

コード:

Exception : java.lang.NullPointerException
telepox = 0
telepoy = 0
telepoz = 0
と表示されました。
調査に入ります。

以下ソースコード(添付したファイルにも入っています)
► スポイラーを表示
添付ファイル
java_properties_fairu.zip
検証に使用したソース・バイナリ・データ
(11.72 KiB) ダウンロード数: 102 回
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

閉鎖

“C言語何でも質問掲示板” へ戻る