AAA = 111
BBB = 222
CCC = 333
想把指定的BBB后面的值修改为444 用JAVA如何做?
现在只想到逐行读取找到BBB 但不知道如何修改值。
我想到了bufferedWriter和FileWriter,以及Properties,但不知道如何实现,请指教!
6 个解决方案
#1
用断去ini文件的jar包就可以
#2
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
public class ProTest {
public static void main(String[] args) throws FileNotFoundException, IOException {
Properties pro = new Properties();
InputStream is = new FileInputStream("f:/temp/test.ini");
pro.load(is);
showPro(pro);
pro.setProperty("BBB", "264");
OutputStream os = new FileOutputStream("f:/temp/test.ini");
pro.store(os,null);
pro.clear();
is.close();
is = new FileInputStream("f:/temp/test.ini");
pro.load(is);
showPro(pro);
os.close();
}
public static void showPro(Properties pro)
{
Set<Entry<Object, Object>> entrys = pro.entrySet();
for (Entry<Object, Object> entry : entrys) {
System.out.println(entry.getKey()+"="+entry.getValue());
}
}
}
#3
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
public class ProTest {
public static void main(String[] args) throws FileNotFoundException, IOException {
Properties pro = new Properties();
InputStream is = new FileInputStream("f:/temp/test.ini");
pro.load(is);
showPro(pro);
pro.setProperty("BBB", "264");
OutputStream os = new FileOutputStream("f:/temp/test.ini");
pro.store(os,null);
pro.clear();
is.close();
is = new FileInputStream("f:/temp/test.ini");
pro.load(is);
showPro(pro);
is.close();
os.close();
}
public static void showPro(Properties pro)
{
Set<Entry<Object, Object>> entrys = pro.entrySet();
for (Entry<Object, Object> entry : entrys) {
System.out.println(entry.getKey()+"="+entry.getValue());
}
}
}
忘记关is了
#4
不好意思,内容现在不是Properties类型的=分隔的内容了,
ini文件中有一行
COM2=WSDDR=//10.121.4.2:10000
我是想把其中的IP地址修改成其他的,不能用Properties做了吧,怎么办呢 ?
ini文件中有一行
COM2=WSDDR=//10.121.4.2:10000
我是想把其中的IP地址修改成其他的,不能用Properties做了吧,怎么办呢 ?
#5
它认第一个=号,COM2为key,WSDDR=//10.121.4.2:10000为value,没什么问题
#6
1.import java.io.BufferedWriter;
2.import java.io.File;
3.import java.io.FileInputStream;
4.import java.io.FileWriter;
5.import java.util.Properties;
6.
7.public class IniRead {
8.private static Properties ini = null;
9. //设置INI文件
10. static File file=new File("src/Config.ini");
11. static {
12. try {
13. ini = new Properties ();
14. //加载文件
15. ini.load (new FileInputStream (file));
16. }catch (Exception ex) {
17. ex.printStackTrace();
18. }
19. }
20. private IniRead() {
21. }
22. /**
23. * 读取INI信息
24. * */
25. public static String getIniKey (String key) {
26. if(!ini.containsKey (key)) {
27. return "";
28. }
29. return ini.get(key).toString ();
30. }
31. /**
32. * 修改INI信息
33. * */
34. public static void setIniKey (String key, String value) {
35. if(!ini.containsKey (key)) {
36. return;
37. }
38. ini.put (key, value);
39. }
40. /**
41. * 保存INI信息
42. * */
43. public static void saveIni (String k[]) {
44. try {
45. FileWriter fw = new FileWriter (file);
46. BufferedWriter bw = new BufferedWriter (fw);
47. for (int i = 0; i < k.length; i++) {
48. bw.write (k[i] + "=" + getIniKey (k[i]));
49. bw.newLine ();
50. }
51. bw.close ();
52. fw.close ();
53. }catch (Exception ex) {
54. ex.printStackTrace();
55. }
56. }
57.}
2.import java.io.File;
3.import java.io.FileInputStream;
4.import java.io.FileWriter;
5.import java.util.Properties;
6.
7.public class IniRead {
8.private static Properties ini = null;
9. //设置INI文件
10. static File file=new File("src/Config.ini");
11. static {
12. try {
13. ini = new Properties ();
14. //加载文件
15. ini.load (new FileInputStream (file));
16. }catch (Exception ex) {
17. ex.printStackTrace();
18. }
19. }
20. private IniRead() {
21. }
22. /**
23. * 读取INI信息
24. * */
25. public static String getIniKey (String key) {
26. if(!ini.containsKey (key)) {
27. return "";
28. }
29. return ini.get(key).toString ();
30. }
31. /**
32. * 修改INI信息
33. * */
34. public static void setIniKey (String key, String value) {
35. if(!ini.containsKey (key)) {
36. return;
37. }
38. ini.put (key, value);
39. }
40. /**
41. * 保存INI信息
42. * */
43. public static void saveIni (String k[]) {
44. try {
45. FileWriter fw = new FileWriter (file);
46. BufferedWriter bw = new BufferedWriter (fw);
47. for (int i = 0; i < k.length; i++) {
48. bw.write (k[i] + "=" + getIniKey (k[i]));
49. bw.newLine ();
50. }
51. bw.close ();
52. fw.close ();
53. }catch (Exception ex) {
54. ex.printStackTrace();
55. }
56. }
57.}
#1
用断去ini文件的jar包就可以
#2
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
public class ProTest {
public static void main(String[] args) throws FileNotFoundException, IOException {
Properties pro = new Properties();
InputStream is = new FileInputStream("f:/temp/test.ini");
pro.load(is);
showPro(pro);
pro.setProperty("BBB", "264");
OutputStream os = new FileOutputStream("f:/temp/test.ini");
pro.store(os,null);
pro.clear();
is.close();
is = new FileInputStream("f:/temp/test.ini");
pro.load(is);
showPro(pro);
os.close();
}
public static void showPro(Properties pro)
{
Set<Entry<Object, Object>> entrys = pro.entrySet();
for (Entry<Object, Object> entry : entrys) {
System.out.println(entry.getKey()+"="+entry.getValue());
}
}
}
#3
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
public class ProTest {
public static void main(String[] args) throws FileNotFoundException, IOException {
Properties pro = new Properties();
InputStream is = new FileInputStream("f:/temp/test.ini");
pro.load(is);
showPro(pro);
pro.setProperty("BBB", "264");
OutputStream os = new FileOutputStream("f:/temp/test.ini");
pro.store(os,null);
pro.clear();
is.close();
is = new FileInputStream("f:/temp/test.ini");
pro.load(is);
showPro(pro);
is.close();
os.close();
}
public static void showPro(Properties pro)
{
Set<Entry<Object, Object>> entrys = pro.entrySet();
for (Entry<Object, Object> entry : entrys) {
System.out.println(entry.getKey()+"="+entry.getValue());
}
}
}
忘记关is了
#4
不好意思,内容现在不是Properties类型的=分隔的内容了,
ini文件中有一行
COM2=WSDDR=//10.121.4.2:10000
我是想把其中的IP地址修改成其他的,不能用Properties做了吧,怎么办呢 ?
ini文件中有一行
COM2=WSDDR=//10.121.4.2:10000
我是想把其中的IP地址修改成其他的,不能用Properties做了吧,怎么办呢 ?
#5
它认第一个=号,COM2为key,WSDDR=//10.121.4.2:10000为value,没什么问题
#6
1.import java.io.BufferedWriter;
2.import java.io.File;
3.import java.io.FileInputStream;
4.import java.io.FileWriter;
5.import java.util.Properties;
6.
7.public class IniRead {
8.private static Properties ini = null;
9. //设置INI文件
10. static File file=new File("src/Config.ini");
11. static {
12. try {
13. ini = new Properties ();
14. //加载文件
15. ini.load (new FileInputStream (file));
16. }catch (Exception ex) {
17. ex.printStackTrace();
18. }
19. }
20. private IniRead() {
21. }
22. /**
23. * 读取INI信息
24. * */
25. public static String getIniKey (String key) {
26. if(!ini.containsKey (key)) {
27. return "";
28. }
29. return ini.get(key).toString ();
30. }
31. /**
32. * 修改INI信息
33. * */
34. public static void setIniKey (String key, String value) {
35. if(!ini.containsKey (key)) {
36. return;
37. }
38. ini.put (key, value);
39. }
40. /**
41. * 保存INI信息
42. * */
43. public static void saveIni (String k[]) {
44. try {
45. FileWriter fw = new FileWriter (file);
46. BufferedWriter bw = new BufferedWriter (fw);
47. for (int i = 0; i < k.length; i++) {
48. bw.write (k[i] + "=" + getIniKey (k[i]));
49. bw.newLine ();
50. }
51. bw.close ();
52. fw.close ();
53. }catch (Exception ex) {
54. ex.printStackTrace();
55. }
56. }
57.}
2.import java.io.File;
3.import java.io.FileInputStream;
4.import java.io.FileWriter;
5.import java.util.Properties;
6.
7.public class IniRead {
8.private static Properties ini = null;
9. //设置INI文件
10. static File file=new File("src/Config.ini");
11. static {
12. try {
13. ini = new Properties ();
14. //加载文件
15. ini.load (new FileInputStream (file));
16. }catch (Exception ex) {
17. ex.printStackTrace();
18. }
19. }
20. private IniRead() {
21. }
22. /**
23. * 读取INI信息
24. * */
25. public static String getIniKey (String key) {
26. if(!ini.containsKey (key)) {
27. return "";
28. }
29. return ini.get(key).toString ();
30. }
31. /**
32. * 修改INI信息
33. * */
34. public static void setIniKey (String key, String value) {
35. if(!ini.containsKey (key)) {
36. return;
37. }
38. ini.put (key, value);
39. }
40. /**
41. * 保存INI信息
42. * */
43. public static void saveIni (String k[]) {
44. try {
45. FileWriter fw = new FileWriter (file);
46. BufferedWriter bw = new BufferedWriter (fw);
47. for (int i = 0; i < k.length; i++) {
48. bw.write (k[i] + "=" + getIniKey (k[i]));
49. bw.newLine ();
50. }
51. bw.close ();
52. fw.close ();
53. }catch (Exception ex) {
54. ex.printStackTrace();
55. }
56. }
57.}