I need to overwrite the value in property file
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
My property file contains 3 properties I need to overwrite the value for thirdOne. How do I load the property file from class path in my java code and overwrite it..
my property file Location packagName->resource->folderName->.propertyFile
property File: I need to overwrite the value for "epochFromTime":
FILE_PATH=C:\Users\pda\Desktop\JsonOutput\DataExtract
epochFilename=C:\Users\pda\Desktop\JsonOutput\epochTime.txt
epochFromTime=1545329531862
Java Code:
try {
Properties config = new Properties();
config.load(ClassLoader.getSystemResourceAsStream(PROPERTIES_PATH));
String epochFromTimeChanged= Long.toString(epoch_to2);
config.setProperty("epochFromTime",epochFromTimeChanged);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
java
add a comment |
My property file contains 3 properties I need to overwrite the value for thirdOne. How do I load the property file from class path in my java code and overwrite it..
my property file Location packagName->resource->folderName->.propertyFile
property File: I need to overwrite the value for "epochFromTime":
FILE_PATH=C:\Users\pda\Desktop\JsonOutput\DataExtract
epochFilename=C:\Users\pda\Desktop\JsonOutput\epochTime.txt
epochFromTime=1545329531862
Java Code:
try {
Properties config = new Properties();
config.load(ClassLoader.getSystemResourceAsStream(PROPERTIES_PATH));
String epochFromTimeChanged= Long.toString(epoch_to2);
config.setProperty("epochFromTime",epochFromTimeChanged);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
java
5
What have you tried and what isn't working?
– Zephyr
Jan 4 at 15:47
No it is not working. Can u please guide how do I make a use of FileOutputStream to do this?
– Bilbo
Jan 4 at 15:52
There are many examples on the usage of OutputStream on SO. I'm sure you can easily find them. For properties, if you cna use external libraries, Apache Commons Configuration is made speciffcally to manage them.
– jhamon
Jan 4 at 15:56
3
Just saying "it is not working" does not help us in any way. Please check out the How to Ask article and post a Minimal, Complete, and Verifiable example that demonstrates the exact problem you're facing...
– Zephyr
Jan 4 at 15:56
You are only loading your properties from a file, then change the value in memory. Your code does not save anything to a file and there is no reason to assume this just magically happens. You have to try something, not say "it's not working".
– f1sh
Jan 4 at 15:59
add a comment |
My property file contains 3 properties I need to overwrite the value for thirdOne. How do I load the property file from class path in my java code and overwrite it..
my property file Location packagName->resource->folderName->.propertyFile
property File: I need to overwrite the value for "epochFromTime":
FILE_PATH=C:\Users\pda\Desktop\JsonOutput\DataExtract
epochFilename=C:\Users\pda\Desktop\JsonOutput\epochTime.txt
epochFromTime=1545329531862
Java Code:
try {
Properties config = new Properties();
config.load(ClassLoader.getSystemResourceAsStream(PROPERTIES_PATH));
String epochFromTimeChanged= Long.toString(epoch_to2);
config.setProperty("epochFromTime",epochFromTimeChanged);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
java
My property file contains 3 properties I need to overwrite the value for thirdOne. How do I load the property file from class path in my java code and overwrite it..
my property file Location packagName->resource->folderName->.propertyFile
property File: I need to overwrite the value for "epochFromTime":
FILE_PATH=C:\Users\pda\Desktop\JsonOutput\DataExtract
epochFilename=C:\Users\pda\Desktop\JsonOutput\epochTime.txt
epochFromTime=1545329531862
Java Code:
try {
Properties config = new Properties();
config.load(ClassLoader.getSystemResourceAsStream(PROPERTIES_PATH));
String epochFromTimeChanged= Long.toString(epoch_to2);
config.setProperty("epochFromTime",epochFromTimeChanged);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
java
java
asked Jan 4 at 15:45
BilboBilbo
1115
1115
5
What have you tried and what isn't working?
– Zephyr
Jan 4 at 15:47
No it is not working. Can u please guide how do I make a use of FileOutputStream to do this?
– Bilbo
Jan 4 at 15:52
There are many examples on the usage of OutputStream on SO. I'm sure you can easily find them. For properties, if you cna use external libraries, Apache Commons Configuration is made speciffcally to manage them.
– jhamon
Jan 4 at 15:56
3
Just saying "it is not working" does not help us in any way. Please check out the How to Ask article and post a Minimal, Complete, and Verifiable example that demonstrates the exact problem you're facing...
– Zephyr
Jan 4 at 15:56
You are only loading your properties from a file, then change the value in memory. Your code does not save anything to a file and there is no reason to assume this just magically happens. You have to try something, not say "it's not working".
– f1sh
Jan 4 at 15:59
add a comment |
5
What have you tried and what isn't working?
– Zephyr
Jan 4 at 15:47
No it is not working. Can u please guide how do I make a use of FileOutputStream to do this?
– Bilbo
Jan 4 at 15:52
There are many examples on the usage of OutputStream on SO. I'm sure you can easily find them. For properties, if you cna use external libraries, Apache Commons Configuration is made speciffcally to manage them.
– jhamon
Jan 4 at 15:56
3
Just saying "it is not working" does not help us in any way. Please check out the How to Ask article and post a Minimal, Complete, and Verifiable example that demonstrates the exact problem you're facing...
– Zephyr
Jan 4 at 15:56
You are only loading your properties from a file, then change the value in memory. Your code does not save anything to a file and there is no reason to assume this just magically happens. You have to try something, not say "it's not working".
– f1sh
Jan 4 at 15:59
5
5
What have you tried and what isn't working?
– Zephyr
Jan 4 at 15:47
What have you tried and what isn't working?
– Zephyr
Jan 4 at 15:47
No it is not working. Can u please guide how do I make a use of FileOutputStream to do this?
– Bilbo
Jan 4 at 15:52
No it is not working. Can u please guide how do I make a use of FileOutputStream to do this?
– Bilbo
Jan 4 at 15:52
There are many examples on the usage of OutputStream on SO. I'm sure you can easily find them. For properties, if you cna use external libraries, Apache Commons Configuration is made speciffcally to manage them.
– jhamon
Jan 4 at 15:56
There are many examples on the usage of OutputStream on SO. I'm sure you can easily find them. For properties, if you cna use external libraries, Apache Commons Configuration is made speciffcally to manage them.
– jhamon
Jan 4 at 15:56
3
3
Just saying "it is not working" does not help us in any way. Please check out the How to Ask article and post a Minimal, Complete, and Verifiable example that demonstrates the exact problem you're facing...
– Zephyr
Jan 4 at 15:56
Just saying "it is not working" does not help us in any way. Please check out the How to Ask article and post a Minimal, Complete, and Verifiable example that demonstrates the exact problem you're facing...
– Zephyr
Jan 4 at 15:56
You are only loading your properties from a file, then change the value in memory. Your code does not save anything to a file and there is no reason to assume this just magically happens. You have to try something, not say "it's not working".
– f1sh
Jan 4 at 15:59
You are only loading your properties from a file, then change the value in memory. Your code does not save anything to a file and there is no reason to assume this just magically happens. You have to try something, not say "it's not working".
– f1sh
Jan 4 at 15:59
add a comment |
2 Answers
2
active
oldest
votes
This is pretty easy. First do read a property file to Properties
. Then update it and save it afterwards. Do not forget to close resources.
public static void updateProperties(File file, Consumer<Properties> consumer) throws IOException {
Properties properties = new Properties();
try (Reader reader = new BufferedReader(new FileReader(file))) {
properties.load(reader);
}
consumer.accept(properties);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
properties.store(writer, "comment");
}
}
Client code could look like this:
updateProperties(
new File("application.properties"),
properties -> {
properties.setProperty("two", "two_two_two");
properties.setProperty("three", "three_three");
});
application.properties
before update
two=two_two
one=one_one
after update
#comment
#Fri Jan 04 18:59:12 MSK 2019
two=two_two_two
one=one_one
three=three_three
add a comment |
Use Properties.store()
to write the changed value back to the file:
String PROPERTIES_PATH = "...";
try {
File f = new File(PROPERTIES_PATH);
FileInputStream in = new FileInputStream(f);
Properties config = new Properties();
config.load(in);
String epochFromTimeChanged= Long.toString(epoch_to2);
config.setProperty("epochFromTime",epochFromTimeChanged);
// get or create the file
File f = new File(PROPERTIES_PATH);
OutputStream out = new FileOutputStream(f);
config.store(out, "My properties file comment");
} catch (FileNotFoundException e1) {
log.info("{} does not exist", PROPERTIES_PATH);
} catch (IOException e) {
log.error("Cannot access {}", PROPERTIES_PATH, e);
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54042114%2fi-need-to-overwrite-the-value-in-property-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is pretty easy. First do read a property file to Properties
. Then update it and save it afterwards. Do not forget to close resources.
public static void updateProperties(File file, Consumer<Properties> consumer) throws IOException {
Properties properties = new Properties();
try (Reader reader = new BufferedReader(new FileReader(file))) {
properties.load(reader);
}
consumer.accept(properties);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
properties.store(writer, "comment");
}
}
Client code could look like this:
updateProperties(
new File("application.properties"),
properties -> {
properties.setProperty("two", "two_two_two");
properties.setProperty("three", "three_three");
});
application.properties
before update
two=two_two
one=one_one
after update
#comment
#Fri Jan 04 18:59:12 MSK 2019
two=two_two_two
one=one_one
three=three_three
add a comment |
This is pretty easy. First do read a property file to Properties
. Then update it and save it afterwards. Do not forget to close resources.
public static void updateProperties(File file, Consumer<Properties> consumer) throws IOException {
Properties properties = new Properties();
try (Reader reader = new BufferedReader(new FileReader(file))) {
properties.load(reader);
}
consumer.accept(properties);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
properties.store(writer, "comment");
}
}
Client code could look like this:
updateProperties(
new File("application.properties"),
properties -> {
properties.setProperty("two", "two_two_two");
properties.setProperty("three", "three_three");
});
application.properties
before update
two=two_two
one=one_one
after update
#comment
#Fri Jan 04 18:59:12 MSK 2019
two=two_two_two
one=one_one
three=three_three
add a comment |
This is pretty easy. First do read a property file to Properties
. Then update it and save it afterwards. Do not forget to close resources.
public static void updateProperties(File file, Consumer<Properties> consumer) throws IOException {
Properties properties = new Properties();
try (Reader reader = new BufferedReader(new FileReader(file))) {
properties.load(reader);
}
consumer.accept(properties);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
properties.store(writer, "comment");
}
}
Client code could look like this:
updateProperties(
new File("application.properties"),
properties -> {
properties.setProperty("two", "two_two_two");
properties.setProperty("three", "three_three");
});
application.properties
before update
two=two_two
one=one_one
after update
#comment
#Fri Jan 04 18:59:12 MSK 2019
two=two_two_two
one=one_one
three=three_three
This is pretty easy. First do read a property file to Properties
. Then update it and save it afterwards. Do not forget to close resources.
public static void updateProperties(File file, Consumer<Properties> consumer) throws IOException {
Properties properties = new Properties();
try (Reader reader = new BufferedReader(new FileReader(file))) {
properties.load(reader);
}
consumer.accept(properties);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
properties.store(writer, "comment");
}
}
Client code could look like this:
updateProperties(
new File("application.properties"),
properties -> {
properties.setProperty("two", "two_two_two");
properties.setProperty("three", "three_three");
});
application.properties
before update
two=two_two
one=one_one
after update
#comment
#Fri Jan 04 18:59:12 MSK 2019
two=two_two_two
one=one_one
three=three_three
answered Jan 4 at 16:01
oleg.cherednikoleg.cherednik
7,21921219
7,21921219
add a comment |
add a comment |
Use Properties.store()
to write the changed value back to the file:
String PROPERTIES_PATH = "...";
try {
File f = new File(PROPERTIES_PATH);
FileInputStream in = new FileInputStream(f);
Properties config = new Properties();
config.load(in);
String epochFromTimeChanged= Long.toString(epoch_to2);
config.setProperty("epochFromTime",epochFromTimeChanged);
// get or create the file
File f = new File(PROPERTIES_PATH);
OutputStream out = new FileOutputStream(f);
config.store(out, "My properties file comment");
} catch (FileNotFoundException e1) {
log.info("{} does not exist", PROPERTIES_PATH);
} catch (IOException e) {
log.error("Cannot access {}", PROPERTIES_PATH, e);
}
add a comment |
Use Properties.store()
to write the changed value back to the file:
String PROPERTIES_PATH = "...";
try {
File f = new File(PROPERTIES_PATH);
FileInputStream in = new FileInputStream(f);
Properties config = new Properties();
config.load(in);
String epochFromTimeChanged= Long.toString(epoch_to2);
config.setProperty("epochFromTime",epochFromTimeChanged);
// get or create the file
File f = new File(PROPERTIES_PATH);
OutputStream out = new FileOutputStream(f);
config.store(out, "My properties file comment");
} catch (FileNotFoundException e1) {
log.info("{} does not exist", PROPERTIES_PATH);
} catch (IOException e) {
log.error("Cannot access {}", PROPERTIES_PATH, e);
}
add a comment |
Use Properties.store()
to write the changed value back to the file:
String PROPERTIES_PATH = "...";
try {
File f = new File(PROPERTIES_PATH);
FileInputStream in = new FileInputStream(f);
Properties config = new Properties();
config.load(in);
String epochFromTimeChanged= Long.toString(epoch_to2);
config.setProperty("epochFromTime",epochFromTimeChanged);
// get or create the file
File f = new File(PROPERTIES_PATH);
OutputStream out = new FileOutputStream(f);
config.store(out, "My properties file comment");
} catch (FileNotFoundException e1) {
log.info("{} does not exist", PROPERTIES_PATH);
} catch (IOException e) {
log.error("Cannot access {}", PROPERTIES_PATH, e);
}
Use Properties.store()
to write the changed value back to the file:
String PROPERTIES_PATH = "...";
try {
File f = new File(PROPERTIES_PATH);
FileInputStream in = new FileInputStream(f);
Properties config = new Properties();
config.load(in);
String epochFromTimeChanged= Long.toString(epoch_to2);
config.setProperty("epochFromTime",epochFromTimeChanged);
// get or create the file
File f = new File(PROPERTIES_PATH);
OutputStream out = new FileOutputStream(f);
config.store(out, "My properties file comment");
} catch (FileNotFoundException e1) {
log.info("{} does not exist", PROPERTIES_PATH);
} catch (IOException e) {
log.error("Cannot access {}", PROPERTIES_PATH, e);
}
answered Jan 4 at 16:04
Markus PscheidtMarkus Pscheidt
3,55033153
3,55033153
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54042114%2fi-need-to-overwrite-the-value-in-property-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
What have you tried and what isn't working?
– Zephyr
Jan 4 at 15:47
No it is not working. Can u please guide how do I make a use of FileOutputStream to do this?
– Bilbo
Jan 4 at 15:52
There are many examples on the usage of OutputStream on SO. I'm sure you can easily find them. For properties, if you cna use external libraries, Apache Commons Configuration is made speciffcally to manage them.
– jhamon
Jan 4 at 15:56
3
Just saying "it is not working" does not help us in any way. Please check out the How to Ask article and post a Minimal, Complete, and Verifiable example that demonstrates the exact problem you're facing...
– Zephyr
Jan 4 at 15:56
You are only loading your properties from a file, then change the value in memory. Your code does not save anything to a file and there is no reason to assume this just magically happens. You have to try something, not say "it's not working".
– f1sh
Jan 4 at 15:59