Jump to content

Problem using Java Filereader/FileWriter


Zcelo12

Recommended Posts

Hello,

I'm trying to edit the configuration of another mod using FileReader/FileWriter in Java.

My problem is that it only add the line i've edited, the other text in the configuration is gone.

Can someone help me?

 

private class CheckDynamicsConfig extends Thread
    {
        @Override
        public void run()
        {            
                
                try {
            		BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\abt\\Desktop\\forge804\\mcp\\jars\\config\\DynamicLights_thePlayer.cfg"));
            		String line = null;
            		while ((line = in.readLine()) != null) {
            			if (line.contains("S:LightItems"))
                        {
            			if (!line.contains("4020"))
                           {
            				BufferedWriter out = new BufferedWriter(new FileWriter("C:\\Users\\abt\\Desktop\\forge804\\mcp\\jars\\config\\DynamicLights_thePlayer.cfg"));
            				String id = ",4020=10";
            				out.write(line+id);
            				out.close();
                           }
                        }
            		}
            		in.close();
            	} 
                catch (IOException e) {
            		e.printStackTrace();
                }
        }
    }

Link to comment
Share on other sites

FileWriter has a constructor with a boolean to disable overwriting the entire file.

 

When I set the boolean to true, it adds the line at the end of the file. So i've the files twice and in the configuration it isn't among general { ... } . Is there a way to override only the line and set it to the position?

I hope you undertand me.

Link to comment
Share on other sites

There is one problem left. This mod should work universal. When i put in the path the full path with C:\Users... it works, but how can i make it universal like \config\bla.cfg. This can't work because the jar is in the folder versions. Maybe someone can help me

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.